将JavaPairRdd写入Csv [英] Write JavaPairRdd to Csv
问题描述
JavaPairRdd具有saveAsTextfile
功能,您可以使用该功能以文本格式保存数据.
JavaPairRdd has saveAsTextfile
function, with which you can save data in a text format.
但是,我需要将数据另存为CSV文件,因此以后可以在Neo4j中使用它.
However what I need is to save the data as CSV file, so I can use it later with Neo4j.
我的问题是:
如何以CSV格式保存JavaPairRdd的数据?还是有一种方法可以将rdd转换为:
How to save the JavaPairRdd 's data in CSV format? Or is there a way to transform the rdd from :
Key Value
Jack [a,b,c]
收件人:
Key value
Jack a
Jack b
Jack c
推荐答案
您应该在JavaPairRdd上使用flatMapValues
函数:Pass each value in the key-value pair RDD through a flatMap function without changing the keys; this also retains the original RDD's partitioning.
You should use the flatMapValues
function on your JavaPairRdd: Pass each value in the key-value pair RDD through a flatMap function without changing the keys; this also retains the original RDD's partitioning.
只需返回值,它就会在输入列表中为每个元素创建一行并保留键.
Just by returning the value it will create a line per element in the input lists preserving the keys.
// In Java
JavaPairRDD<Object, List<String>> input = ...;
JavaPairRDD<Object, String> output = input.flatMapValues((Function<List<String>, Iterable<String>>) Functions.identity());
这篇关于将JavaPairRdd写入Csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!