星火 - RDD保存到多个文件作为输出 [英] Spark - save RDD to multiple files as output
问题描述
我有一个 JavaRDD<产品型号>
,我需要把它写成一个以上的文件,不同的布局[在RDD一个或两个字段之间会不同不同的布局。
I have a JavaRDD<Model>
, which i need to write it as more than one file with different layout [one or two fields in the RDD will be different between different layout].
当我使用 saveAsTextFile()
它调用的toString()模型
方法,这意味着同样的布局将写为输出
When i use saveAsTextFile()
its calling the toString()
method of Model, it means same layout will be written as output.
目前我在做什么是使用地图
变换方法迭代RDD并返回与其他布局不同的模型,这样我就可以使用saveAsTextFile()动作来写不同输出文件。
Currently what i am doing is iterate the RDD using map
transformation method and return the different model with other layout, so i can use saveAsTextFile() action to write as different output file.
由于只有一个或两个领域都不同,我需要再次重复整个RDD,创造新的RDD然后将其保存为输出文件。
Just because of one or two fields are different , i need to iterate the entire RDD again and create new RDD then save it as output file.
例如:
当前RDD与字段:
RoleIndicator,姓名,年龄,地址,部门
RoleIndicator, Name, Age, Address, Department
输出文件1:
姓名,年龄,地址
输出文件2:
RoleIndicator,姓名,年龄,系
RoleIndicator, Name, Age, Department
有没有什么最优解此?
问候,
尚卡尔
Regards, Shankar
推荐答案
您想要使用的foreach
,而不是收集
。
您应该定义功能扩展 VoidFunction
实际命名的类。这两个文件的创建实例变量,并添加即关闭文件的close()
方法。你的()调用
实施将写任何你所需要的。
You should define your function as an actual named class that extends VoidFunction
. Create instance variables for both files, and add a close()
method that closes the files. Your call()
implementation will write whatever you need.
别忘了拨打的close()
大功告成后,你的函数对象。
Remember to call close()
on your function object after you're done.
这篇关于星火 - RDD保存到多个文件作为输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!