将java RDD结果写入Hive表中
来源:互联网 发布:淘宝产品详情页素材 编辑:程序博客网 时间:2024/06/06 03:14
情况一:只需插入一列
JavaRDD<String> titleParticiple = ....;
/**
* 将分词结果保存到Hive表,供数据探查使用* */
HiveContext hiveCtx = new HiveContext(jsc);
SQLContext sqlCtx = new SQLContext(jsc);
/**
* 在RDD的基础上创建类型为Row的RDD,
*/
JavaRDD<Row> brandRDD = titleParticiple.map(new Function<String, Row>() {
private static final long serialVersionUID = 1L;
public Row call( String line )
throws Exception {
return RowFactory.create(line);
}
});
/**
*1、 动态的构建DataFrame的元数据,一般而言,有多少列以及酶类的具体类型可能来源于JSON文件或者数据库
*/
List<StructField> structFields = new ArrayList<StructField>();
//structFields.add(DataTypes.createStructField( "id", DataTypes.IntegerType, true ));
structFields.add(DataTypes.createStructField( "brand", DataTypes.StringType, true ));
/**
* 2、构建StructType用于DataFrame 元数据的描述
*
*/
StructType structType = DataTypes.createStructType( structFields );
/**
* 3、基于MeataData以及RDD<Row>来构造DataFrame
*/
Dataset<Row> personsDF = sqlCtx.createDataFrame(brandRDD,structType);
/**
* 4、注册成为临时表以供后续的SQL查询操作
*/
personsDF.registerTempTable("brands");
hiveCtx.sql("use sousuo"); //使用sousuo数据库
hiveCtx.sql("drop table if exists sousuo.temp_yeqingyun_20170913");//删除原来的表
hiveCtx.sql("CREATE TABLE IF NOT EXISTS sousuo.temp_yeqingyun_20170913 (brand STRING)");//创建表
hiveCtx.sql("insert into sousuo.temp_yeqingyun_20170913 select brand from brands");//将brands表中的内容全部拷贝到temp_yeqingyun_20170913表中
情况二:需要插入多列,且插入的类型有int和String:
JavaPairRDD<String, String> brandTypeGoodsPair = “...”;
HiveContext hiveCtx = new HiveContext(jsc);
SQLContext sqlCtx = new SQLContext(jsc);
JavaRDD<Row> brandRDD = brandTypeGoodsPair.map(new Function<Tuple2<String, String>, Row>() {
private static final long serialVersionUID = 1L;
int i=0;
public Row call( Tuple2<String, String> pair) throws Exception {
i++;
String[] valueArray = pair._2().split(":");
String value0 = valueArray[0];
int value1 = Integer.parseInt(valueArray[1]);
return RowFactory.create(i, pair._1, value0, value1);
}
});
List<StructField> structFields = new ArrayList<StructField>();
structFields.add(DataTypes.createStructField( "id", DataTypes.IntegerType, true ));
structFields.add(DataTypes.createStructField( "directory3", DataTypes.StringType, true ));
structFields.add(DataTypes.createStructField( "brandItemModel", DataTypes.StringType, true ));
structFields.add(DataTypes.createStructField( "num", DataTypes.IntegerType, true ));
StructType structType = DataTypes.createStructType( structFields );
Dataset<Row> brandDF = sqlCtx.createDataFrame(brandRDD,structType);
brandDF.registerTempTable("brands_test2");
hiveCtx.sql("use sousuo");
hiveCtx.sql("drop table if exists sousuo.temp_yeqingyun_test2_20170913");
hiveCtx.sql("CREATE TABLE IF NOT EXISTS sousuo.temp_yeqingyun_test2_20170913 (id INT, directory3 STRING, brandItemModel STRING, num INT)");
hiveCtx.sql("insert into sousuo.temp_yeqingyun_test2_20170913 select id,directory3,brandItemModel,num from brands_test2");
- 将java RDD结果写入Hive表中
- 将java RDD结果写入Hive表中
- 在Spark中使用UDF对HIVE表进行查询,再将查询结果RDD写入另一个HIVE表
- hive应用实例 将查询结果写入原表中
- Java实现把测试结果写入Excel表中
- Java实现把测试结果写入Excel表中
- flume 模拟将日志内容写入到 hive中
- SqlServer--将存储过程的结果集写入一张表中
- spark将计算结果写入到mysql中
- Spark将计算结果写入到Mysql中
- Spark将计算结果写入到Mysql中
- 将Hive统计分析结果导入到MySQL数据库表中(二)——使用Hive和MySQL JDBC驱动
- 将Hive统计分析结果导入到MySQL数据库表中(三)——使用Hive UDF或GenericUDF
- [Hive]MapReduce将数据写入Hive分区表
- dataframe 写入hive中
- Spark RDD(DataFrame) 写入到HIVE的代码实现
- Spark RDD(DataFrame) 写入到HIVE的代码实现
- java将字符串写入到文件中
- ubuntu16.04+cuda8.0+cudnn5.1+caffe 安装(基于独立主机&集成显卡>X1080Ti)
- [LeetCode]139. Word Break
- jsp出现The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- 结构型模式之适配器模式
- hrbust 1609 Windows安全警报
- 将java RDD结果写入Hive表中
- Android 常用工具类
- 模糊查询的存储过程
- listview
- MySQL5.6.25升级MySQL5.7.15
- list、set、map
- C语言实现单链表面试题---基础篇
- NodeMCU(ESP8266)开发笔记
- Xlistview加载更多