如何使用spark将数据加载到配置单元外部表中? [英] How to load data into hive external table using spark?

查看:384
本文介绍了如何使用spark将数据加载到配置单元外部表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试使用spark将数据加载到配置单元外部表中。
请在此帮助我,如何使用scala代码或java将数据加载到配置单元中



提前致谢

解决方案

假设已经使用类似的方法创建了hive外部表,

  CREATE EXTERNAL TABLE external_parquet(c1 INT,c2 STRING,c3 TIMESTAMP)
作为PARQUET位置存储'/ user / etl / destination'; - 位置是HDFS上的某个目录

你在Spark中有一个现有的dataFrame / RDD,



$ p $ import sqlContext.implicits._
val rdd = sc.parallelize(List((1, a,new Date),(2,b,new Date),(3,c,new Date)))
val df = rdd.toDF(c1,c2, c3)//你的数据框的列名
df.write.mode(SaveMode.Overwrite).parquet(/ user / etl / destination)//如果你想覆盖现有的数据集(完全重新导入如果您不想覆盖数据集中的现有数据...



$ b

/ p>

  df.write.mode(SaveMode.Append).parquet(/ user / etl / destination)//如果你想要附加到现有数据集(增量导入)


I want to try to load data into hive external table using spark. please help me on this, how to load data into hive using scala code or java

Thanks in advance

解决方案

Assuming that hive external table is already created using something like,

CREATE EXTERNAL TABLE external_parquet(c1 INT, c2 STRING, c3 TIMESTAMP) 
    STORED AS PARQUET LOCATION '/user/etl/destination';   -- location is some directory on HDFS

And you have an existing dataFrame / RDD in Spark, that you want to write.

import sqlContext.implicits._
val rdd = sc.parallelize(List((1, "a", new Date), (2, "b", new Date), (3, "c", new Date)))
val df = rdd.toDF("c1", "c2", "c3")  //column names for your data frame
df.write.mode(SaveMode.Overwrite).parquet("/user/etl/destination") // If you want to overwrite existing dataset (full reimport from some source)

If you don't want to overwrite existing data from your dataset...

df.write.mode(SaveMode.Append).parquet("/user/etl/destination")  // If you want to append to existing dataset (incremental imports)

这篇关于如何使用spark将数据加载到配置单元外部表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆