Java MapReduce-可写可比文件
我对Java和Hadoop都是新手。我正在尝试一个非常简单的程序来获得频繁配对 e、 g 所以最后它应该给出频繁对isJava MapReduce-可写可比文件,java,mapreduce,word-count,Java,Mapreduce,Word Count,我对Java和Hadoop都是新手。我正在尝试一个非常简单的程序来获得频繁配对 e、 g 所以最后它应该给出频繁对is(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) 这里我的钥匙不是一把,而是一对。在阅读文档时,我看到了我们必须实现的每个新密钥
(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方法实际比较键?我可以看到相等和比较,但我找不到任何解释。请不要输入密码!谢谢
编辑1:
相比之下,我为对(a,b)=(b,a)返回0,但它仍然不会返回到同一个减缩器,在比较方法中,有没有办法将键(b,a)重置为(a,b)或生成全新的键
编辑2:
我不知道如何生成新密钥,但与更改逻辑相比,它工作得很好。。!谢谢大家
writeablecomparable
是一个接口,它使实现它的类成为两件事:writeable
,这意味着它可以通过序列化等方式写入网络或从网络读取。如果要将它用作键或值,以便可以在Hadoop节点之间发送,这是必要的。和Comparable
,这意味着必须提供方法来显示给定类的一个对象如何与另一个对象进行比较。当减速器按键组织时使用此选项
当您希望创建自己的对象作为密钥时,此接口是必需的。您需要创建自己的InputFormat
,而不是使用Hadoop附带的一种格式。根据我的经验,这可能会变得相当困难,特别是如果您对Java和Hadoop都不熟悉的话
所以,如果我是你,我就不会为此烦恼,因为有一个更简单的方法。我会使用
TextInputFormat
,它是默认的InputFormat
并且非常容易使用和理解。您可以简单地将每个键作为一个与字符串非常相似的文本
对象发出。不过有一个警告;如您所述,“is-Foo”
和“Foo-is”
需要计算为相同的键。因此,对于您提取的每一对单词,在使用String.compareTo
方法将它们作为键传递之前,请按字母顺序对它们进行排序。这样你就可以保证没有重复。这里是解决你问题的mapper类,
未实现频繁词对逻辑。我想你不是在找那个
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();
}
}
}
公共类MR{
公共静态类映射器扩展org.apache.hadoop.mapreduce.Mapper
{
公共静态整数检查(字符串键检查)
{
//登录以检查密钥是否频繁?
返回0;
}
@凌驾
受保护的void映射(可长写键、文本值、上下文)
抛出IOException、InterruptedException{
MapkeyMap=newhashmap();
字符串行=value.toString();
String[]words=line.split(“”);
对于(int i=0;i不需要任何代码,既不需要mapper也不需要任何东西…只想了解这个WritableComparable做什么?我是新手,但这就是我想学习的原因!所以没关系!我实现了自己的键..但仍然想知道,它的哪个方法实际上比较键:我找到了Equals和compareTo。它们的名字表明,我也应该这么做是一对吗?equals
是从Object继承的,而compareTo
是从Comparable
继承的。所以compareTo
应该是在键比较中使用的,否则您就不需要实现Comparable
了。谢谢@Eric,compareTo返回int,所以如果我返回1 for对(a,b)=(a,b)和(a,b)=(b,a)…它将转到一个减缩器,是真的吗?根据“负整数、零或正整数,因为此对象小于、等于或大于指定对象”。所以如果它们相等,则返回0。我还没有尝试过你在我个人输入格式中提出的这个特定案例,但这应该会使它们转到同一个减速机,是的。感谢Eric的所有回复!但是对于(a,b)=(a,b)和(a,b)=(b,a)我返回0,但它不会转到同一个减速机!这个()文章说,我们必须使用分区器来确保哪个(键、值)指向哪个减速机,有什么想法吗?谢谢!
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();
}
}
}