mapjoin操作

mapjoin操作
    思路:商品加载到内存中 然后数据在map数据段输出前,进行替换
    public class Cachemapper extends Mapper<LongWritable, Text, Text, NullWritable>{
        Hashmap<String,String> pdmap = new Hashmap<>();

        //1、商品表加载到内存
        protected void setup(Context context) throws Exception{
            
            //加载缓存文件
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("pd.txt"),"UTF-8"));
            String line;
            while(StringUtils.isNotEmpty(line=br.readLine())){
                String[] fields = line.split("\t");
                pdMap.put(fields[0],fields[1]);
            }

            br.close();
        }
        

        //2、map
        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptException{
            //获取数据
            String line = value.toString();

            //切割
            String[] fields = line.split("\t");

            //获取订单中的商品id
            String pid = fields[1];

            //根据id获取商品名
            String pname = pdMap.get(pid);

            //拼接数据并输出
            line = line +"\t"+ pname;
            context.write(new Text(line), NullWritable.get());
        }
    }

    public class FileDriver{
        public static void main(String[] args) throws IOException, ClassNotFoundException,InterruptException{
            Configuration conf = new Configuration();
            Job job = Job.getInstance();

            job.setJarByClass(FileDriver.class);

            job.setMapperClass(Cachemapper.class);
            

            job.setMapOutputValueClass(Text.class);
            job.setMapOutputKeyClass(NullWritable.class);

            FileInputFormat.setInputPaths(job, new Path("C:/in"));
            FileOutputFormat.setOutputPath(job, new Path("c:/out"));

            //加载缓存商品数据
            job.addCacheFile(new URI("file:///c:/inputcache/pd.txt"));

            //设置一下reducetask的数量,默认为1
            job.setNumReduceTask(0);

            job.waitForCompletion(true);
        }

        //生成的结果文件part-r-0000,经过了reduce
        //part-m-0000,只有map,没有reduce

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值