hadoop - 每个 mapper 中的 Hashmap 应该在单个 reducer 中使用

标签 hadoop dictionary reduce

在我的一个类(class)中,我使用 HashMap。我在我的映射器中调用该类(class)。所以现在每个 mapper 都有自己的 HashMap。现在我可以将所有 HashMap 用于单个 reducer 吗?实际上,我的 HashMap 包含 Key 作为我的文件名,值是 Set。因此每个 HashMap 都包含一个文件名和一个 Set。现在我想使用所有包含相同文件名的 HashMap 并想合并所有值(集),然后将该 HashMap 写入我的 Hdfs 文件

最佳答案

是的,你可以做到。如果您的映射器以 hashmap 的形式提供输出,那么您可以使用 Hadoop 的 MapWritable 作为映射器的值。 例如

public class MyMapper extends Mapper<LongWritable, Text, Text, MapWritable>

你必须将你的Hashmap转换成MapWritable格式:

MapWritable mapWritable = new MapWritable();
for (Map.Entry<String,String> entry : yourHashMap.entrySet()) {
    if(null != entry.getKey() && null != entry.getValue()){
       mapWritable.put(new Text(entry.getKey()),new Text(entry.getValue()));
    }
}

然后将 mapwritable 提供给您的上下文:

ctx.write(new Text("my_key",mapWritable);

对于 Reducer 类,您将 MapWritable 作为您的输入值

public class MyReducer extends Reducer<Text, MapWritable, Text, Text>

public void reduce(Text key, Iterable<MapWritable> values, Context ctx) throws IOException, InterruptedException

然后遍历 map 并按照您想要的方式提取值。例如:

for (MapWritable entry : values) {
  for (Entry<Writable, Writable> extractData: entry.entrySet()) {
      //your logic for the data will go here.
   }                    
}

关于hadoop - 每个 mapper 中的 Hashmap 应该在单个 reducer 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31264246/

相关文章:

javascript - 使用 "map"函数返回多个数组

Hadoop 似乎在对给定 reduce 调用的值进行迭代期间修改了我的关键对象

python - Hadoop Map中最高/最低温度的python mapreduce示例

hadoop - 如果 INSERT OVERWRITE 的 SELECT 查询不返回任何结果,是否有办法防止 Hive 表被覆盖

sql - hadoop操作只写一行?

c - A* 在 C 中搜索 : implementation and performance on graph map

swift - Mapreduce调用不明确

hadoop - Hadoop作业输出中不需要的字符

python - 在 python 中映射 csv

python - 如何检查字典是否是另一个复杂字典的子集