Hadoop map reduce映射程序编程 import java.io.IOException; 导入org.apache.hadoop.io.LongWritable; 导入org.apache.hadoop.io.Text; 导入org.apache.hadoop.mapred.MapReduceBase; 导入org.apache.hadoop.mapred.Mapper; 导入org.apache.hadoop.mapred.OutputCollector; 导入org.apache.hadoop.mapred.Reporter; 公共类ADDMapper扩展MapReduceBase实现Mapper {@覆盖 公共void映射(LongWritable键、文本值、OutputCollector输出、Reporter r)引发IOException { 字符串s=value.toString(); char[]words=s.toCharArray(); int-wno=0; int ino=0; 对于(inti=0;i
Hi Shivendra),我编写了下面的映射器逻辑,它将帮助您找到每个字符串的索引以及排序输出。 此代码的输出是带有索引的排序字符串,然后您可以在此输出上运行reducerHadoop map reduce映射程序编程 import java.io.IOException; 导入org.apache.hadoop.io.LongWritable; 导入org.apache.hadoop.io.Text; 导入org.apache.hadoop.mapred.MapReduceBase; 导入org.apache.hadoop.mapred.Mapper; 导入org.apache.hadoop.mapred.OutputCollector; 导入org.apache.hadoop.mapred.Reporter; 公共类ADDMapper扩展MapReduceBase实现Mapper {@覆盖 公共void映射(LongWritable键、文本值、OutputCollector输出、Reporter r)引发IOException { 字符串s=value.toString(); char[]words=s.toCharArray(); int-wno=0; int ino=0; 对于(inti=0;i,java,hadoop,elastic-map-reduce,Java,Hadoop,Elastic Map Reduce,Hi Shivendra),我编写了下面的映射器逻辑,它将帮助您找到每个字符串的索引以及排序输出。 此代码的输出是带有索引的排序字符串,然后您可以在此输出上运行reducer import java.io.IOException; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapred.MapReduceBase; import org.
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;
}
}
}
}
String str=value.toString();
String[]tokens=str.split(“”;//拆分为单词
//为唯一单词创建hashmap
Map uniqueString=newhashmap();
对于(int i=0;i=0){
collect(新文本((字符串)entry.getKey()),新的LongWritable(索引));
index=str.indexOf((String)entry.getKey(),index+1);
}
}
此逻辑的输出:
上午: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
这可能会对您有所帮助。请运行下面的代码,它运行正常,并给出您的预期输出
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
public class IndexCount {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String str=value.toString();
String[] tokens = str.split(" "); //split into words
//create hashmap for unique word
HashMap<String,Integer> uniqueString = new HashMap<String,Integer>();
for(int i=0;i<tokens.length;i++){
uniqueString.put(tokens[i],1);
}
//for sorting create TreeMap from above hash map
TreeMap<String, Integer> map = new TreeMap<String,Integer>(uniqueString);
for (Entry<String, Integer> entry : map.entrySet()) {
int index=0;
//find the index of the word
index = str.indexOf((String)entry.getKey());
while (index >= 0) {
output.collect(new Text((String)entry.getKey()),new IntWritable(index));
index = str.indexOf((String)entry.getKey(), index + 1);
}
}
}
}
public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
while (values.hasNext()) {
output.collect(key, new IntWritable(values.next().get()));
}
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("indexfinder");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}
在命令行参数中提供输入和输出路径。(参数[0],参数[1])
import java.io.IOException;
导入java.util.*;
导入java.util.Map.Entry;
导入org.apache.hadoop.fs.Path;
导入org.apache.hadoop.io.*;
导入org.apache.hadoop.mapred.*;
公共类索引计数{
公共静态类映射扩展MapReduceBase实现映射器{
公共void映射(LongWritable键、文本值、OutputCollector输出、Reporter报告器)引发IOException{
字符串str=value.toString();
String[]tokens=str.split(“”;//拆分为单词
//为唯一单词创建hashmap
HashMap uniqueString=新HashMap();
对于(int i=0;i=0){
collect(新文本((字符串)entry.getKey()),新IntWritable(索引));
index=str.indexOf((String)entry.getKey(),index+1);
}
}
}
}
公共静态类Reduce扩展MapReduceBase实现Reducer{
公共void reduce(文本键、迭代器值、OutputCollector输出、Reporter报告器)引发IOException{
while(values.hasNext()){
collect(key,newintwriteable(values.next().get());
}
}
}
公共静态void main(字符串[]args)引发异常{
JobConf conf=newjobconf(WordCount.class);
conf.setJobName(“indexfinder”);
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
setInputPath(conf,新路径(args[0]);
setOutputPath(conf,新路径(args[1]);
runJob(conf);
}
}
请运行以下代码,给出预期输出
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
public class IndexCount {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String str=value.toString();
String[] tokens = str.split(" "); //split into words
//create hashmap for unique word
HashMap<String,Integer> uniqueString = new HashMap<String,Integer>();
for(int i=0;i<tokens.length;i++){
uniqueString.put(tokens[i],1);
}
//for sorting create TreeMap from above hash map
TreeMap<String, Integer> map = new TreeMap<String,Integer>(uniqueString);
for (Entry<String, Integer> entry : map.entrySet()) {
int index=0;
//find the index of the word
index = str.indexOf((String)entry.getKey());
while (index >= 0) {
output.collect(new Text((String)entry.getKey()),new IntWritable(index));
index = str.indexOf((String)entry.getKey(), index + 1);
}
}
}
}
public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
while (values.hasNext()) {
output.collect(key, new IntWritable(values.next().get()));
}
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("indexfinder");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}
import java.io.IOException;
导入java.util.*;
导入java.util.Map.Entry;
导入org.apache.hadoop.fs.Path;
导入org.apache.hadoop.conf.*;
导入org.apache.hadoop.io.*;
导入org.apache.hadoop.mapreduce.*;
导入org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
导入org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
导入org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
导入org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
公共类索引{
公共静态类映射扩展映射器{
公共void映射(LongWritable键、文本值、上下文上下文)引发IOException、InterruptedException{
字符串str=value.toString();
String[]tokens=str.split(“”;//拆分为单词
//为唯一单词创建hashmap
HashMap uniqueString=新HashMap();
对于(int i=0;i=0){
指数+=strIndex;
write(新文本((字符串)entry.getKey()),新IntWritable(索引));
index=str.indexOf((String)entry.getKey(),index+1);
}
}
conf.setInt(“索引”,strIndex+str.length());
}
}
公共静态类Reduce扩展Reducer{
公共void reduce(文本键、Iterable值、上下文)
抛出IOException、InterruptedException{
for(可写入值:值){
write(key,newintwriteable(val.get());
}
}
}
公共静态void main(字符串[]args)引发异常{
Configuration conf=新配置();
conf.setInt(“索引”,0);
作业作业=新作业(配置,“索引”);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
setInputFormatClass(TextInputFormat.class);
setOutputFormatClass(TextOutputFormat.class);
addInputPath(作业,新路径(“输入”));
setOutputPath(作业,新路径(“输出”));
job.waitForCompletion(true);
}
}
您能否(a)正确设置代码格式,以及(b)请不要用大写字母提问?另外,请阅读更多提示。您的问题实际上不会得到多少答案。事实上,我是map reducer的新手,所以我有点困难
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class Index {
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String str=value.toString();
String[] tokens = str.split(" "); //split into words
//create hashmap for unique word
HashMap<String,Integer> uniqueString = new HashMap<String,Integer>();
for(int i=0;i<tokens.length;i++){
uniqueString.put(tokens[i],1);
}
//for sorting create TreeMap from above hash map
TreeMap<String, Integer> map = new TreeMap<String,Integer>(uniqueString);
Configuration conf=context.getConfiguration();
int strIndex = 0;
for (Entry<String, Integer> entry : map.entrySet()) {
//int index=0;
strIndex=conf.getInt("index", 0);
//find the index of the word
int index = str.indexOf((String)entry.getKey());
while (index >= 0) {
index+=strIndex;
context.write(new Text((String)entry.getKey()),new IntWritable(index));
index = str.indexOf((String)entry.getKey(), index + 1);
}
}
conf.setInt("index", strIndex+str.length());
}
}
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
for (IntWritable val : values) {
context.write(key, new IntWritable(val.get()));
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.setInt("index", 0);
Job job = new Job(conf, "index");
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path("input"));
FileOutputFormat.setOutputPath(job, new Path("output"));
job.waitForCompletion(true);
}
}