json - Spark.RDD take(n) 返回元素为 n 的数组,n 次

标签 json scala hadoop apache-spark

我正在使用 https://github.com/alexholmes/json-mapreduce 中的代码将多行 json 文件读入 RDD。

var data = sc.newAPIHadoopFile( 
    filepath, 
    classOf[MultiLineJsonInputFormat], 
    classOf[LongWritable], 
    classOf[Text], 
    conf)

我打印了前 n 个元素来检查它是否正常工作。

data.take(n).foreach { p => 
  val (line, json) = p
  println
  println(new JSONObject(json.toString).toString(4))
}

但是,当我尝试查看数据时,从 take 返回的数组似乎不正确。

而不是返回以下形式的数组

[ data[0], data[1], ... data[n] ] 

其形式为

[ data[n], data[n], ... data[n] ]

这是我创建的 RDD 的问题,还是我尝试打印它的方式的问题?

最佳答案

我明白了为什么take它返回一个具有重复值的数组。

正如 API 中提到的:

Note: Because Hadoop's RecordReader class re-uses the same Writable object 
for each record, directly caching the returned RDD will create many 
references to the same object. If you plan to directly cache Hadoop 
writable objects, you should first copy them using a map function.

因此,在我的例子中,它重复使用相同的 LongWritable 和 Text 对象。例如,如果我这样做:

val foo = data.take(5)
foo.map( r => System.identityHashCode(r._1) )

输出是:

Array[Int] = Array(1805824193, 1805824193, 1805824193, 1805824193, 1805824193)

因此,为了防止它这样做,我简单地将重用对象映射到它们各自的值:

val data = sc.newAPIHadoopFile(
    filepath,
    classOf[MultiLineJsonInputFormat],
    classOf[LongWritable],
    classOf[Text],
    conf ).map(p => (p._1.get, p._2.toString))

关于json - Spark.RDD take(n) 返回元素为 n 的数组,n 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25310985/

相关文章:

apache - 为什么配置单元中的桶数应等于 reducer 数?

Hadoop - 有没有办法在作业之间共享自定义计数器?

sorting - 我可以为reducer中的记录分配序列号吗

json - 使用 Alamofire JSOn 和 HTML 创建通用 getter

c# - C# 中的 Foreach Json 对象

scala - 无法设置类型安全控制台

scala - 简化Scala表达式计算比例

javascript - 如何使用 Highcharts 从数据库传入一系列数据来制作多个 Y 轴

javascript - 使用nodeJS返回Json

javascript - 带有粗箭头的数组上的 forEach 循环