分享

分布式缓存

feature09 发表于 2017-11-20 17:31:36 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 2 4429
本帖最后由 feature09 于 2017-11-20 17:59 编辑

事情是这样的。两张表。一个数据表,一个字典表。我想通过分布式缓存的方式,将数据表中的第二列数字(标红色的),转换成字典表中的男、女(蓝色的),这样的数据进行输出。也就是结果表那样的显示。
//数据表data.txt
//one     1       two     qqq
//two    2       two     ccc

//字典表zidian.txt
//1                1        sex
//2                2        sex
//3        未知        0        sex
//4        结婚        1        marry
//5        未婚        2        marry
//6        未知        0        marry


结果数据应是:
//
//


代码部分:

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.toString()));
                                while ((a = br.readLine()) != null) {
                                        //以数据作为key,文字作为value
                                        sexMap.put(a.split("\t")[2], a.split("\t")[1]);
                                }
                                br.close();
                        }
                }

                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.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("1"));

                }
        }

        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);
        }

}


我最后出来的结果,最终结果是空的,求大神指点一下哈


执行过程中没有报错的。

已有(2)人评论

跳转到指定楼层
feature09 发表于 2017-11-20 17:57:58
找到了,map的context写错地方了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条