将 Spark RDD 保存到 Hive 表 [英] Save Spark RDD to Hive Table
问题描述
在 spark 中,我想将 RDD 对象保存到 hive 表中.我正在尝试使用 createDataFrame 但这正在抛出
In spark I want to save RDD objects to hive table. I am trying to use createDataFrame but that is throwing
线程main"中的异常java.lang.NullPointerException
Exception in thread "main" java.lang.NullPointerException
val products=sc.parallelize(evaluatedProducts.toList);
//here products are RDD[Product]
val productdf = hiveContext.createDataFrame(products, classOf[Product])
我使用的是 Spark 1.5 版本.
I am using Spark 1.5 version.
推荐答案
如果你的 Product 是一个类(不是案例类),我建议你在创建 DataFrame 之前将你的 rdd 转换为 RDD[Tuple]:
If your Product is a class (not a case class), I suggest you transform your rdd to RDD[Tuple] before creating the DataFrame:
import org.apache.spark.sql.hive.HiveContext
val hiveContext = new HiveContext(sc)
import hiveContext.implicits._
val productDF = products
.map({p: Product => (p.getVal1, p.getVal2, ...)})
.toDF("col1", "col2", ...)
通过这种方法,您将在 DataFrame 中将 Product 属性作为列.
With this approach, you will have the Product attributes as columns in the DataFrame.
然后你可以创建一个临时表:
Then you can create a temp table with:
productDF.registerTempTable("table_name")
或具有以下内容的物理表:
or a physical table with:
productDF.write.saveAsTable("table_name")
这篇关于将 Spark RDD 保存到 Hive 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!