• Map Reduce数据清洗及Hive数据库操作


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

    两阶段数据清洗:

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

    ip:    199.30.25.88

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

    traffic:  62

    文章: article/11325

    视频: video/3235

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

    ip--->城市 city(IP)

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

    day: 10

    traffic:62

    type:article/video

    id:11325

    (3)hive数据库表结构:

    create table data(  ip string,  time string , day string, traffic bigint,

    type string, id   string )

    由于很长时间没有进行编程,今天只能完成 第一阶段的文件数据处理

    package com.test.dao;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;  
    import org.apache.hadoop.io.IntWritable;  
    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.input.TextInputFormat;  
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
    import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
    public class test1{  
        public static List<String> ips=new ArrayList<String>();
        public static  List<String> times=new ArrayList<String>();
        public static List<String> traffic=new ArrayList<String>();
        public static  List<String> wen=new ArrayList<String>();
        public static  List<String> shi=new ArrayList<String>();
        
        public static class Map extends Mapper<Object , Text , Text,Text>{  
        private static Text Name =new Text();  
        private static Text num=new Text();  
        public void map(Object key,Text value,Context context) throws IOException, InterruptedException{  
        String line=value.toString();  
        String arr[]=line.split(",");  
            Name.set(arr[0]);
            num.set(arr[0]);
        context.write(Name,num);  
        }  
        }  
        public static class Reduce extends Reducer< Text, Text,Text, Text>{  
        private static Text result= new Text();  
        int i=0;
        public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{  
           
            for(Text val:values){  
                context.write(key, val);
                ips.add(val.toString());
            }
        
            
            }  
            }  
        public static int run()throws IOException, ClassNotFoundException, InterruptedException
        {
            Configuration conf=new Configuration();  
            conf.set("fs.defaultFS", "hdfs://localhost:9000");
            FileSystem fs =FileSystem.get(conf);
            Job job =new Job(conf,"OneSort");  
            job.setJarByClass(test1.class);  
            job.setMapperClass(Map.class);  
            job.setReducerClass(Reduce.class);  
            job.setOutputKeyClass(Text.class);  
            job.setOutputValueClass(Text.class);  
            job.setInputFormatClass(TextInputFormat.class);  
            job.setOutputFormatClass(TextOutputFormat.class);  
            Path in=new Path("hdfs://localhost:9000/test2/in/result.txt");  
            Path out=new Path("hdfs://localhost:9000/test2/out/ip/1");  
            FileInputFormat.addInputPath(job,in);  
            fs.delete(out,true);
            FileOutputFormat.setOutputPath(job,out);  
            return(job.waitForCompletion(true) ? 0 : 1);  
        }
            public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{  
        
                run();
            }  
            }
    }
    是多疑还是去相信 谎言背后的忠心 或许是自己太执迷 命题游戏 沿着他的脚步 呼吸开始变得急促 就算看清了面目 设下埋伏 真相却居无定处 I swear I'll never be with the devil 用尽一生孤独 没有退路的路 你看不到我 眉眼焦灼却不明下落 命运的轮轴 伺机而动 来不及闪躲 沿着他的脚步 呼吸开始变得急促 就算看清了面目 设下埋伏 真相却居无定处 I swear I'll never be with the devil 用尽一生孤独 没有退路的路 你看不到我 眉眼焦灼却不明下落 命运的轮轴 伺机而动 来不及闪躲 你看不到我 眉眼焦灼却不明下落 命运的轮轴 伺机而动 来不及闪躲 黑夜和白昼 你争我夺 真相被蛊惑 心从不退缩 这天堂荒漠 留给孤独的猎手
  • 相关阅读:
    浅谈 Nginx 的内部核心架构设计
    项目中常用的19条MySQL优化
    Redis分布式锁的正确实现方式
    C# 读取XML文件示例
    C# LINQ to XML示例
    最新的极光推送服务器端代码(java服务器后台向手机端自定义推送消息)
    极光推送>>java SDK服务端集成后台项目(使用详解)
    关于如何在Listener中注入service和ServletContextListener源码分析
    mysql 去除重复 Select中DISTINCT关键字的用法 在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值。其原因是 distinct只能返回它的目标字段,而无法返回其它字段,这个问题让我困扰了很久,用distinct不能解决的话,
    Spring 整合 Quartz 实现动态定时任务
  • 原文地址:https://www.cnblogs.com/jiaoaoshirenjinbu/p/14202466.html
Copyright © 2020-2023  润新知