gpt4 book ai didi

java - 如何将映射器中的内容写入文件 - Hadoop

转载 作者:行者123 更新时间:2023-12-01 13:51:06 24 4
gpt4 key购买 nike

如何将mapper的内容写入文件。这样可以吗。

public class MyMapper extends
Mapper<Object, Text, Text, MatrixWritable > {
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {

Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);

Path inputfile = new Path("in/map");
BufferedWriter getdatabuffer = new BufferedWriter(new OutputStreamWriter(fs.create(inputfile)));
if(value.toString()!= null){
getdatabuffer.write(value.toString());
}
getdatabuffer.close();

如果我的输入文件被分割,上面的代码是否可以正常工作?

在 reducer 中,我组合了所有映射器数据。

编辑

        Path inputfile = new Path("in/map");
FSDataOutputStream out = fs.create(inputfile);
if(value.toString()!= null){
out.writeBytes(value.toString());
}
out.close();

最佳答案

Mapper 任务在 Hadoop 集群中的多个节点上并发运行。你用普通的Java Writer类来写的方法是行不通的,因为你需要使用HDFS API来写数据。

而是在map方法中使用context.write()将数据写入HDFS文件。

关于java - 如何将映射器中的内容写入文件 - Hadoop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19946399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com