我对Java和Hadoop都是新手。我正在尝试一个非常简单的程序来获得频繁的配对。
例如:
Input: My name is Foo. Foo is student.
Intermediate Output:
Map:
(my, name): 1
(name ,is): 1
(is, Foo): 2 // (is, Foo) = (Foo, is)
(is, student)所以最后它应该给出频繁的配对是(is ,Foo)。
伪代码如下所示:
Map(Key: line_num, value: line)
words = split_words(line)
for each w in words:
for each neighbor x:
emit((w, x)), 1)这里我的密钥不是一个,它是对的。在阅读文档时,我读到了每个我们必须实现WritableComparable的新密钥。
所以我对此感到困惑。如果有人能解释一下这个课程,那就太好了。不确定这是不是真的。然后我就可以自己找出怎么做了!
我不想要任何代码,既不需要映射器,也不想要任何东西...我只是想了解一下这个WritableComparable是做什么的?WritableComparable的哪种方法实际上比较了键?我可以看到equals和compareTo,但我找不到任何关于它的解释。请不要写代码!谢谢
编辑1:在compareTo中,当对(a,b) = (b,a)时,我返回0,但它仍然不会返回相同的reducer,在compareTo方法中,我是否有办法将密钥(b,a)重置为(a,b)或生成全新的密钥?
编辑2:我不知道用于生成新密钥,但在compareTo更改逻辑中,它工作得很好!谢谢大家!
发布于 2012-09-28 03:13:40
WritableComparable是一个接口,它使实现它的类成为两件事:Writable,这意味着它可以通过序列化等方式写入和读取您的网络。如果您打算将其用作键或值,以便可以在Hadoop节点之间发送,则这是必要的。和Comparable,这意味着必须提供显示给定类的一个对象如何与另一个对象进行比较的方法。这在Reducer按关键点组织时使用。
当你想创建你自己的对象作为key时,这个接口是必要的。而且,您需要创建自己的InputFormat,而不是使用Hadoop附带的And。这可能是相当困难的(根据我的经验),特别是如果您是Java和Hadoop的新手。
因此,如果我是你,我不会费心去做这个,因为有一个简单得多的方法。我会使用TextInputFormat,它既是默认的InputFormat,也很容易使用和理解。您可以简单地将每个键作为一个Text对象发出,这非常类似于一个字符串。但是有一个警告;就像您提到的,"is Foo"和"Foo is"需要被评估为相同的键。因此,对于提取出的每一对单词,在使用String.compareTo方法将它们作为键传递之前,请按字母顺序对它们进行排序。这样你就可以保证不会有重复。
发布于 2012-09-28 15:54:29
这是针对您的问题的映射器类,未实现频繁词对逻辑。我猜你不是在找那个。
public class MR {
public static class Mapper extends org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, LongWritable>
{
public static int check (String keyCheck)
{
// logig to check key is frequent or not ?
return 0;
}
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
Map< String, Integer> keyMap=new HashMap<String, Integer>();
String line=value.toString();
String[] words=line.split(" ");
for(int i=0;i<(words.length-1);i++)
{
String mapkeyString=words[i]+","+words[i+1];
// Logic to check is mapKeyString is frequent or not .
int count =check(mapkeyString);
keyMap.put(mapkeyString, count);
}
Set<Entry<String,Integer>> entries=keyMap.entrySet();
for(Entry<String, Integer> entry:entries)
{
context.write(new Text(entry.getKey()), new LongWritable(entry.getValue()));
}
}
}
public static class Reduce extends Reducer<Text, LongWritable, Text, Text>
{
protected void reduce(Text key, Iterable<LongWritable> Values,
Context context)
throws IOException, InterruptedException {
}
}
public static void main(String[] args) {
Configuration configuration=new Configuration();
try {
Job job=new Job(configuration, "Word Job");
job.setMapperClass(Mapper.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(TextInputFormat.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}https://stackoverflow.com/questions/12625669
复制相似问题