zoukankan      html  css  js  c++  java
  • Mapreduce+hive练习

    要求与说明:

    Result文件数据说明:

    Ip106.39.41.166,(城市)

    Date10/Nov/2016:00:01:02 +0800,(日期)

    Day10,(天数)

    Traffic: 54 ,(流量)

    Type: video,(类型:视频video或文章article

    Id: 8701(视频或者文章的id

    测试要求:

    1、 数据清洗:按照进行数据清洗,并将清洗后的数据导入hive数据库中

    两阶段数据清洗:

    1)第一阶段:把需要的信息从原始日志中提取出来

    ip:    199.30.25.88

    time:  10/Nov/2016:00:01:03 +0800

    traffic:  62

    文章: article/11325

    视频: video/3235

    2)第二阶段:根据提取出来的信息做精细化操作

    ip--->城市 cityIP

    date--> time:2016-11-10 00:01:03

    day: 10

    traffic:62

    type:article/video

    id:11325

    Result.txt文件:

    第一阶段源代码:

    package hivetest;
    
    import java.io.IOException;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.LongWritable;
    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 Hivetest1 {
    
        //  static ClassService service=new ClassService();
        public static class MyMapper extends Mapper<LongWritable, Text, Text/*map对应键类型*/, Text/*map对应值类型*/>
        {
             protected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException
             {
                  String[] strNlist = value.toString().split(",");//如何分隔
                  //LongWritable,IntWritable,Text等
                  
                  context.write(new Text(strNlist[0])/*map对应键类型*/,new Text(strNlist[1]+","+strNlist[2]+","+strNlist[3]+","+strNlist[4]+","+strNlist[5])/*map对应值类型*/);
             }
        }
        public static class MyReducer extends Reducer<Text/*map对应键类型*/, Text/*map对应值类型*/, Text/*reduce对应键类型*/, Text/*reduce对应值类型*/>
        {
            // static No1Info info=new No1Info();
             protected void reduce(Text key, Iterable<Text/*map对应值类型*/> values,Context context)throws IOException, InterruptedException
             {
                 for (/*map对应值类型*/Text init : values)
                 {
                     context.write( key/*reduce对应键类型*/, new Text(init)/*reduce对应值类型*/);
                 }
             }
        }
        
        public static void main(String[] args) throws Exception {
            Configuration conf = new Configuration();        
            Job job = Job.getInstance();
            job.setJarByClass(Hivetest1.class);
            job.setMapperClass(MyMapper.class);
            job.setMapOutputKeyClass(/*map对应键类型*/Text.class);
            job.setMapOutputValueClass( /*map对应值类型*/Text.class);    
            // TODO: specify a reducer
            job.setReducerClass(MyReducer.class);
            job.setOutputKeyClass(/*reduce对应键类型*/Text.class);
            job.setOutputValueClass(/*reduce对应值类型*/Text.class);
    
            // TODO: specify input and output DIRECTORIES (not files)
            FileInputFormat.setInputPaths(job, new Path("hdfs://localhost:9000/user/hive/warehouse/test/result.txt"));
            FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/user/hive/warehouse/result"));
    
            boolean flag = job.waitForCompletion(true);
            System.out.println("完成!");    //任务完成提示
            System.exit(flag ? 0 : 1);
            System.out.println();
        }
    
    }

    运行结果:

     第二阶段代码:

    import java.lang.String;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.LongWritable;
    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 sjqx {
         public static final SimpleDateFormat FORMAT = new SimpleDateFormat("d/MMM/yyyy:HH:mm:ss", Locale.ENGLISH); //原时间格式
         public static final SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//现时间格式
       private static Date parseDateFormat(String string) {         //转换时间格式
            Date parse = null;
            try {
                parse = FORMAT.parse(string);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return parse;
        }
        public static class MyMapper extends Mapper<LongWritable, Text, Text/*map对应键类型*/, Text/*map对应值类型*/>
        {
             protected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException
             {
                  String[] strNlist = value.toString().split(",");//如何分隔
                  //LongWritable,IntWritable,Text等
                  Date date = parseDateFormat(strNlist[1]);
                  context.write(new Text(strNlist[0])/*map对应键类型*/,new Text(dateformat1.format(date)+" "+strNlist[2]+" "+strNlist[3]+" "+strNlist[4]+" "+strNlist[5])/*map对应值类型*/);
             }
        }
        public static class MyReducer extends Reducer<Text/*map对应键类型*/, Text/*map对应值类型*/, Text/*reduce对应键类型*/, Text/*reduce对应值类型*/>
        {
    //        static No1Info info=new No1Info();
             protected void reduce(Text key, Iterable<Text/*map对应值类型*/> values,Context context)throws IOException, InterruptedException
             {
                 for (/*map对应值类型*/Text init : values)
                 {
    
                     context.write( key/*reduce对应键类型*/, new Text(init)/*reduce对应值类型*/);
                 }
             }
        }
        
        public static void main(String[] args) throws Exception {
            Configuration conf = new Configuration();    
            Job job = Job.getInstance();
            //job.setJar("MapReduceDriver.jar");
            job.setJarByClass(sjqx.class);
            // TODO: specify a mapper
            job.setMapperClass(MyMapper.class);
            job.setMapOutputKeyClass(/*map对应键类型*/Text.class);
            job.setMapOutputValueClass( /*map对应值类型*/Text.class);
            
            // TODO: specify a reducer
            job.setReducerClass(MyReducer.class);
            job.setOutputKeyClass(/*reduce对应键类型*/Text.class);
            job.setOutputValueClass(/*reduce对应值类型*/Text.class);
    
            // TODO: specify input and output DIRECTORIES (not files)
            FileInputFormat.setInputPaths(job, new Path("hdfs://localhost:9000/test/in/result"));
            FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/test/out"));
    
            boolean flag = job.waitForCompletion(true);
            System.out.println("SUCCEED!"+flag);    //任务完成提示
            System.exit(flag ? 0 : 1);
            System.out.println();
        }
    }

    运行结果:

  • 相关阅读:
    js rsa sign使用笔记(加密,解密,签名,验签)
    金额的计算
    常用js方法集合
    sourceTree 的使用
    node-- express()模块
    详细讲解vue.js里的父子组件通信(props和$emit)
    Vue -- vue-cli webpack打包开启Gzip 报错
    es6函数的rest参数和拓展运算符(...)的解析
    js中判断对象数据类型的方法
    vue学习之vue基本功能初探
  • 原文地址:https://www.cnblogs.com/lover995/p/11852178.html
Copyright © 2011-2022 走看看