【问题标题】:R+Hadoop: How to read CSV file from HDFS and execute mapreduce?R+Hadoop:如何从 HDFS 读取 CSV 文件并执行 mapreduce?
【发布时间】:2013-08-08 05:20:54
【问题描述】:

在以下示例中:

  small.ints = to.dfs(1:1000)
  mapreduce(
    input = small.ints, 
    map = function(k, v) cbind(v, v^2))

mapreduce 函数的数据输入是一个名为 small.ints 的对象,它引用 HDFS 中的块。

现在我有一个 CSV 文件已经存储在 HDFS 中了

"hdfs://172.16.1.58:8020/tmp/test_short.csv"

如何为它获取一个对象?

据我所知(这可能是错误的),如果我想要 CSV 文件中的数据作为 mapreduce 的输入,我必须首先在 R 中生成一个表,其中包含 CSV 文件中的所有值。我确实有这样的方法:

data=from.dfs("hdfs://172.16.1.58:8020/tmp/test_short.csv",make.input.format(format="csv",sep=","))
mydata=data$val

用这个方法获取mydata,然后做object=to.dfs(mydata)好像还可以,但是问题是test_short.csv文件很大,大约TB大小,内存放不下from.dfs 的输出!!

实际上,我想知道我是否直接使用“hdfs://172.16.1.58:8020/tmp/test_short.csv”作为 mapreduce 输入,并且在 map 函数内部执行 from.dfs() 的事情,我可以吗获取数据块?

请给我一些建议,无论如何!

【问题讨论】:

    标签: r hadoop rhadoop


    【解决方案1】:

    mapreduce(input = path, input.format = make.input.format(...), map ...)

    from.dfs 用于小数据。在大多数情况下,您不会在 map 函数中使用 from.dfs。参数已经保存了一部分输入数据

    【讨论】:

    • 哦,我没有注意到我可以将输入格式放在 mapreduce() 参数中!我读过你的 wiki,上面说 to.dfs 和 from.dfs 仅用于小数据和测试。感谢您的帮助!
    【解决方案2】:

    您可以执行以下操作:

    r.file <- hdfs.file(hdfsFilePath,"r")
    from.dfs(
        mapreduce(
             input = as.matrix(hdfs.read.text.file(r.file)),
             input.format = "csv",
             map = ...
    ))
    

    请给点分数,希望有人觉得有用。

    注意:详情参考stackoverflow帖子:

    How to input HDFS file into R mapreduce for processing and get the result into HDFS file

    【讨论】:

      猜你喜欢
      • 2013-06-13
      • 2015-04-28
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多