【问题标题】:Write JavaPairRdd to Csv将 JavaPairRdd 写入 Csv
【发布时间】:2018-12-17 05:15:30
【问题描述】:

JavaPairRdd 有saveAsTextfile 函数,可以将数据保存为文本格式。

不过,我需要将数据保存为 CSV 文件,以便稍后在 Neo4j 中使用。

我的问题是:

如何将 JavaPairRdd 的数据保存为 CSV 格式?或者有没有办法将rdd从:

Key   Value
Jack  [a,b,c]

到:

Key  value
 Jack  a
 Jack  b
 Jack  c

【问题讨论】:

  • This 可能感兴趣。

标签: java apache-spark rdd key-value java-pair-rdd


【解决方案1】:

您应该在您的 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.

只需返回值,它就会在输入列表中为每个元素创建一行,并保留键。

  // In Java
  JavaPairRDD<Object, List<String>> input = ...;
  JavaPairRDD<Object, String> output = input.flatMapValues((Function<List<String>, Iterable<String>>) Functions.identity());

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-05-21
  • 2017-04-15
  • 2017-12-03
  • 2016-10-26
  • 2016-07-17
  • 2017-03-29
  • 2021-01-01
  • 1970-01-01
相关资源
最近更新 更多