使用Spark Scala的HiveContext向Hive表中插入数据

4
我可以使用HiveContext在我的Spark代码中将数据插入到Hive表中,如下所示。
   val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc)
   sqlContext.sql("CREATE TABLE IF NOT EXISTS e360_models.employee(id INT, name STRING, age INT) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'")

   sqlContext.sql("insert into table e360_models.employee select t.* from (select 1210, 'rahul', 55) t")
   sqlContext.sql("insert into table e360_models.employee select t.* from (select 1211, 'sriram pv', 35) t")
   sqlContext.sql("insert into table e360_models.employee select t.* from (select 1212, 'gowri', 59) t")

   val result = sqlContext.sql("FROM e360_models.employee SELECT id, name, age")
   result.show()

但是,这种方法会为每次插入创建一个在仓库中的独立文件,如下所示。
part-00000
part-00000_copy_1
part-00000_copy_2
part-00000_copy_3

有没有办法避免这种情况,只将新数据追加到单个文件中,或者有没有其他更好的方法从Spark向Hive插入数据?

你解决了这个问题吗?我做了一些研究,但没有运气! - pedram bashiri
1个回答

1

这些单独的文件会占用64MB(默认块大小)* 3(副本因子)的空间,还是只有实际文件大小 * 副本因子? - yAsH
1
我认为你可以在这里找到答案:https://dev59.com/kGcs5IYBdhLWcg3wPxnN - mgaido

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接