如何将Spark中的DataFrame转换为HadoopRDD [英] How to convert DataFrame in spark to HadoopRDD

查看:105
本文介绍了如何将Spark中的DataFrame转换为HadoopRDD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢将Spark中的数据帧写入dynamodb。
所以我正在使用 rdd.saveAsHadoopDataset(JobConf)。但是rdd类型不匹配,它需要hadoopRDD类型的rdd,所以我想转换数据帧我曾经使用 df.rdd 这给了我rdd,但没有给我hadoopRDD。我正在使用spark-scala API。如果有更好的方法将Spark从Dataframe写入Dyanmodb会有所帮助。

I like to write dataframe in spark to dynamodb. So I am using rdd.saveAsHadoopDataset(JobConf).But the rdd type is mismatch.It needs the rdd of type hadoopRDD.So I like to convert the dataframe to rdd.I had used df.rdd which gives me rdd but not of hadoopRDD. I am using spark-scala API.If is there any better way of writing Dataframe to Dyanmodb from spark that will help.

推荐答案

您无需转换RDD。

由于Hadoop API是围绕键值对构造的,因此Spark会自动将PairRDDFunctions(添加了其他功能)包装在数据存储在 Tuple2 对象。因此,您只需要将数据放入 RDD [(T,V)] ,那么您将拥有 saveAsHadoopDataset

Because the Hadoop API structured around key-value pairs, Spark automatically wraps the PairRDDFunctions (which adds additional functionality) around RDDs where the data is stored in Tuple2 objects. So you only need to put your data into an RDD[(T,V)], then you'll have saveAsHadoopDataset method available.

这里是一个示例:

import org.apache.hadoop.mapred.JobConf
val tupleRDD : RDD[(Int, Int)] = sc.parallelize(Array((1,2), (3,4), (5,6)))
val jobConf = new JobConf()

设置任何需要的设置。

tupleRDD.saveAsHadoopDataset(jobConf)

这篇关于如何将Spark中的DataFrame转换为HadoopRDD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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