数据导入(二):MapReduce

package test091201;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;

public class Demo3 {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        //设置hbase表名称
        conf.set(TableOutputFormat.OUTPUT_TABLE, "waln2");
        //将该值改大,防止hbase超时退出
        conf.set("dfs.socket.timeout", "180000");
        conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");
        conf.set("hbase.zookeeper.quorum", "ncst");

        Job job = Job.getInstance(conf);
        job.setJarByClass(Demo3.class);

        job.setMapperClass(Demo3Mapper.class);
        job.setReducerClass(Demo3Reduce.class);

        //设置map的输出,不设置reduce的输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setInputFormatClass(TextInputFormat.class);
        //不再设置输出路径,而是设置输出格式类型
        job.setOutputFormatClass(TableOutputFormat.class);

        FileInputFormat.setInputPaths(job, new Path("hdfs://10.16.17.182:9000/test/wal_log"));
        job.waitForCompletion(true);
    }

    //map
    public static class Demo3Mapper extends Mapper<LongWritable, Text, Text, Text>{
        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String[] splited = value.toString().split("\t");

            SimpleDateFormat dataformat = new SimpleDateFormat("yyyyMMddHHmmss");
            String string = splited[0];
            long parseLong = Long.parseLong(string.trim());
            Date date = new Date(parseLong);
            String format = dataformat.format(date);

            //Key=>TelNum:Date Value=>Line
            context.write(new Text(splited[1]+":"+format), value);
        }
    }

    //reduce
    public static class Demo3Reduce extends TableReducer<Text, Text, NullWritable>{
        @Override
        protected void reduce(Text key, Iterable<Text> v2s, Context context)
                throws IOException, InterruptedException {
            Put put = new Put(key.toString().getBytes());
            for (Text text : v2s) {
                String[] splited = text.toString().split("\t");
                put.add("info".getBytes(), "date".getBytes(), splited[0].getBytes());
                put.add("info".getBytes(), "tel".getBytes(), splited[1].getBytes());
                put.add("info".getBytes(), "upPack".getBytes(), splited[6].getBytes());
                put.add("info".getBytes(), "downPack".getBytes(), splited[7].getBytes());
                put.add("info".getBytes(), "upPay".getBytes(), splited[8].getBytes());
                put.add("info".getBytes(), "downPay".getBytes(), splited[9].getBytes());                
            }
            context.write(NullWritable.get(), put);
        }
    }
}

 

转载于:https://www.cnblogs.com/skyl/p/4849222.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值