您好,登錄后才能下訂單哦!
這個坑踩了好長。結(jié)果卻是map方法中的context寫錯位置,導(dǎo)致錯誤。
源數(shù)據(jù)內(nèi)容。就是想數(shù)據(jù)表中的第二列替換成字典表中的第二列。即字典表中的紅色,換成字典表的藍(lán)色。
//數(shù)據(jù)表data.txt
//one 1 two qqq
//two 2 two ccc
//字典表zidian.txt
//1男1sex
//2女2sex
//3未知0sex
//4結(jié)婚1marry
//5未婚2marry
//6未知0marry
想要的結(jié)果就是
男
女
附上代碼:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class Cache {
public static class Mapall extends Mapper<Object, Text, Text, Text> {
private Map<String, String> sexMap = new HashMap<String, String>();
private Path[] localFiles;
// 先做分布式緩存處理,將數(shù)據(jù)換成到內(nèi)存中
public void setup(Context context) throws IOException {
Configuration conf = context.getConfiguration();
localFiles = DistributedCache.getLocalCacheFiles(conf);
for(int i = 0;i<localFiles.length;i++) {
String a ;
BufferedReader br = new BufferedReader(new FileReader(localFiles[i].toString()));
while ((a = br.readLine()) != null && a.split("\t")[3].equals("sex")) {
//以數(shù)據(jù)作為key,文字作為value
sexMap.put(a.split("\t")[2], a.split("\t")[1]);
}
br.close();
}
}
@SuppressWarnings("unlikely-arg-type")
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
// 獲取sex字段,是1,2這樣的數(shù)據(jù)
String sex = value.toString().split("\t")[1];
// 如果key部分有1,2這種形式,就替換成男、女這樣的內(nèi)容
if (sexMap.keySet().equals(sex)) {
}
context.write(new Text(sexMap.get(sex)), new Text(""));
//就是這里,坑我好久的時間。
}
}
public static class Reduce extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterator<Text> values, Context context) throws IOException, InterruptedException {
context.write(key, new Text(""));
}
}
public static void main(String[] args)
throws URISyntaxException, IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
DistributedCache.addCacheFile(new URI("hdfs://192.168.20.39:8020/qpf/zidian.txt"), conf);
Job job = Job.getInstance(conf, "get cache file");
job.setJarByClass(Cache.class);
job.setMapperClass(Mapall.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("hdfs://192.168.20.39:8020/qpf/data.txt"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.20.39:8020/qpf/data_out"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
很簡單的一個數(shù)據(jù)替換的小例子。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。