java Hadoop mapreduce映射程序编程
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
public class ADDMapper extends MapReduceBase implements Mapper<LongWritable,
Text,Text,LongWritable>
{ @Override
public void map(LongWritable key, Text value,OutputCollector<Text, LongWritable> output, Reporter r)throws IOException
{
String s=value.toString();
char[] words=s.toCharArray();
int wno=0;
int ino=0;
for(int i=0;i<words.length;i++)
{
String temp="";
for(int j=ino;j<words.length;j++)
{
if(words[j]!=' ')
{ temp+=words[j];
}
else
{
wno=j;
if(temp!="")
{
ino=ino + key; //////POINT OF ERROR
output.collect(new Text(temp),new LongWritable(ino));
}
temp="";
ino=wno+1;
break;
}
}
}
}
}
我想得到每个字符串的索引值,按字符串排序
上面的代码既没有给出索引值,也没有洗牌字符串。
允许
输入文件:
你好
嗨,我是对的。
你的工作怎么样。
你好,你还好吗
输出: 我50岁 是7,33 你好,0,30,44 3,14怎么样 .
# 1 楼答案
请运行下面的代码,给出预期的输出
# 2 楼答案
请运行下面的代码,它运行良好,并给出您的预期输出
在命令行参数中提供输入和输出路径。(参数[0],参数[1])
# 3 楼答案
Hi Shivendra,我编写了下面的映射器逻辑,它将帮助您找到每个字符串的索引和排序输出。 此代码的输出是带有索引的排序字符串,然后您可以在此输出上运行reducer
此逻辑的输出: 上午:20,, 是:7,, 是:50,, 嗨:0,, 你好:15,, 你好:47,, 方式:3,, 时间:30, 一:1,, i:16, i:18, i:24, i:34, i:48, is:34, 工作:42, 好 啊。:58, 正确的。:23, 你:11,, 你:37,, 你:54,, 你的电话号码:37
这可能对你有帮助