将Spark RDD保存到Hive表 [英] Save Spark RDD to Hive Table

查看:2553
本文介绍了将Spark RDD保存到Hive表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在spark中,我想将RDD对象保存到配置单元表中。我试图使用createDataFrame,但是抛出
$ b


线程main中的异常java.lang.NullPointerException




  val products = sc.parallelize(evaluateProducts.toList); 
//这里的产品是RDD [Product]
val productdf = hiveContext.createDataFrame(products,classOf [Product])

我正在使用Spark 1.5版本。如果你的产品是一个类(不是案例类),我建议你先将RDD转换为RDD [元组]创建DataFrame:

  import org.apache.spark.sql.hive.HiveContext 

val hiveContext = new HiveContext(sc)
import hiveContext.implicits._
$ b $ val productDF = products
.map({p:Product =>(p.getVal1,p.getVal2 ,...)})
.toDF(col1,col2,...)

使用这种方法,您将在DataFrame中将产品属性设置为列。然后,您可以创建一个临时表:

  productDF.registerTempTable(table_name)

或物理表:

  productDF.write.saveAsTable(table_name)


In spark I want to save RDD objects to hive table. I am trying to use createDataFrame but that is throwing

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])

I am using Spark 1.5 version.

解决方案

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", ...)

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屋!

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