将Java列表从Mapper传递到Reducer
我试图编写Map Reduce代码,其中mapper使用逗号分隔的记录值获取文件输入,我基于逗号拆分行,并为每行创建一组,最后将每组添加到列表集合中 我不确定如何将其写入上下文并传递给reducer,或者更具体地说,不确定Map Reduce中Java列表集合使用的数据类型。我听说过数组是可写的,但无法在代码中实现它。 请参见下面的代码:将Java列表从Mapper传递到Reducer,java,hadoop,collections,mapreduce,Java,Hadoop,Collections,Mapreduce,我试图编写Map Reduce代码,其中mapper使用逗号分隔的记录值获取文件输入,我基于逗号拆分行,并为每行创建一组,最后将每组添加到列表集合中 我不确定如何将其写入上下文并传递给reducer,或者更具体地说,不确定Map Reduce中Java列表集合使用的数据类型。我听说过数组是可写的,但无法在代码中实现它。 请参见下面的代码: public static class Map extends Mapper<LongWritable,Text,Text,IntWritable>
public static class Map extends Mapper<LongWritable,Text,Text,IntWritable>
{
List<HashSet> listA=new CopyOnWriteArrayList<HashSet>();
Set<Set> finalset=new HashSet();
Set<String> hs3;
public void map(LongWritable key, Text value,Context context) throws IOException,InterruptedException
{
String line = value.toString();
String[] str=line.split(",");
HashSet<String> hs=new HashSet<>();
for(int i=0;i<str.length;i++)
{
hs.add(str[i]);
}
listA.add(hs);
context.write(key, listtobepassed); //listA in this case
}
}
更新问题:我试图通过输入上的Map Reduce运行一个传递闭包程序;
例子:
输入
输出应如下所示:
Ref1, Ref2, Ref3,Ref4,Ref5,Ref6
Ref7, Ref8, Ref9
Ref10, Ref11, Ref12
公共输入值合并在一起
我的Java代码看起来像
公共类测试列表{
public static void main(String[] args) throws IOException
{
List<HashSet> ls=new CopyOnWriteArrayList<HashSet>();
BufferedReader br=new BufferedReader(new FileReader("/inputfilepath"));
String Data;
while((Data=br.readLine())!=null)
{
String[] DataLine=Data.split(",");
HashSet<String> hs =new HashSet();
for(int i=0;i<DataLine.length;i++)
{
hs.add(DataLine[i]);
}
ls.add(hs);
}
br.close();
//System.out.println(ls.iterator().next());
Iterator<HashSet> itr=ls.iterator();
HashSet<String> hs2=null;
while(itr.hasNext())
{
HashSet<String> ele=itr.next();
for (HashSet<String> hs1 : ls)
{
if(!Collections.disjoint(hs1, ele))
{
hs2=new HashSet<String>(hs1);
hs2.addAll(ele);
ls.remove(ele);
ls.remove(hs1);
}
}
ls.add(hs2);
}
int counter=0;
BufferedWriter bw=new BufferedWriter(new FileWriter("transitiveoutput.txt"));
for(Set s: ls)
{
bw.write(s.toString().replace("[","").replace("]", "").trim());
bw.newLine();
System.out.println(s.toString().replace("[","").replace("]", "").trim());
}
System.out.println("Transitive Closure comepleted.....");
bw.close();
}
publicstaticvoidmain(字符串[]args)引发IOException
{
List ls=新的CopyOnWriteArrayList();
BufferedReader br=新的BufferedReader(新文件读取器(“/inputfilepath”);
字符串数据;
而((Data=br.readLine())!=null)
{
String[]DataLine=Data.split(“,”);
HashSet hs=新的HashSet();
对于(int i=0;i您期望的输出是什么?您可以将其添加到问题中吗?检查此项以获取有关ArrayWritable的帮助,我无法理解前3行如何转换为“Ref1,Ref2,Ref3,Ref4,Ref5,Ref6”。在您的代码中,您只是在逗号(,)上拆分行把元素放在集合中,然后发射。不确定,你到底想做什么achieve@Manjunath是的,我将根据逗号拆分它们,并将每个元素添加到哈希集,最后将哈希集添加到列表。接下来,我将克隆列表并比较每个元素(集合)如果两个集合之间存在交集,则将它们放在一起。请参阅下面的Java代码:公共类测试{我知道了。我能够执行独立程序。让我看看,我们是否可以找到MapReduce解决方案。如果找到解决方案,我将回答这个问题。
Ref1, Ref2, Ref3,Ref4,Ref5,Ref6
Ref7, Ref8, Ref9
Ref10, Ref11, Ref12
public static void main(String[] args) throws IOException
{
List<HashSet> ls=new CopyOnWriteArrayList<HashSet>();
BufferedReader br=new BufferedReader(new FileReader("/inputfilepath"));
String Data;
while((Data=br.readLine())!=null)
{
String[] DataLine=Data.split(",");
HashSet<String> hs =new HashSet();
for(int i=0;i<DataLine.length;i++)
{
hs.add(DataLine[i]);
}
ls.add(hs);
}
br.close();
//System.out.println(ls.iterator().next());
Iterator<HashSet> itr=ls.iterator();
HashSet<String> hs2=null;
while(itr.hasNext())
{
HashSet<String> ele=itr.next();
for (HashSet<String> hs1 : ls)
{
if(!Collections.disjoint(hs1, ele))
{
hs2=new HashSet<String>(hs1);
hs2.addAll(ele);
ls.remove(ele);
ls.remove(hs1);
}
}
ls.add(hs2);
}
int counter=0;
BufferedWriter bw=new BufferedWriter(new FileWriter("transitiveoutput.txt"));
for(Set s: ls)
{
bw.write(s.toString().replace("[","").replace("]", "").trim());
bw.newLine();
System.out.println(s.toString().replace("[","").replace("]", "").trim());
}
System.out.println("Transitive Closure comepleted.....");
bw.close();
}