您好,登錄后才能下訂單哦!
這個坑踩了好長。結果卻是map方法中的context寫錯位置,導致錯誤。
源數據內容。就是想數據表中的第二列替換成字典表中的第二列。即字典表中的紅色,換成字典表的藍色。
//數據表data.txt
//one 1 two qqq
//two 2 two ccc
//字典表zidian.txt
//1男1sex
//2女2sex
//3未知0sex
//4結婚1marry
//5未婚2marry
//6未知0marry
想要的結果就是
男
女
附上代碼:
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;
// 先做分布式緩存處理,將數據換成到內存中
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")) {
//以數據作為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這樣的數據
String sex = value.toString().split("\t")[1];
// 如果key部分有1,2這種形式,就替換成男、女這樣的內容
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);
}
}
很簡單的一個數據替換的小例子。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。