搜狗日志查询分析 (MapReduce+Hive综合实验)

前提条件:


安装好hadoop2.7.3(Linux系统下)

安装好MySQL(Windows系统下),推荐使用Xampp

安装好Hive(Linux系统下)参考:Hive安装配置

 
题目:

从搜狗实验室下载搜索数据进行分析

下载的数据包含6个字段,数据格式说明如下:

访问时间  用户ID  [查询词]  该URL在返回结果中的排名  用户点击的顺序号  用户点击的URL

注意:

1.字段分隔符:字段分隔符是个数不等的空格;

2.字段个数:有些行有6个字段,有些达不到6个字段。

 

问题:使用MapReduce和Hive查询出搜索结果排名为第2名,点击顺序排在第1的数据?

 


实验步骤:
 

思路:用MapReduce做数据清洗,用Hive来分析数据。

1.下载数据源

打开搜狗实验室链接
http://www.sogou.com/labs/resource/q.php

下载精简版(一天数据,63MB)  tar.gz格式数据

下载后文件如下:

2.上传下载文件至HDFS

   2.1将下载的文件通过WinScp工具上传到Linux系统

  2.2 解压SogouQ.reduced.tar.gz并上传到HDFS

  解压:

$ tar -zxvf SogouQ.reduced.tar.gz

可以用tail命令查看解压文件最后3行的数据

tail -3 SogouQ.reduced

查询词为中文,这里编码按UTF-8查出来是乱码,编码时指定为‘GBK’可避免乱码。数据格式如前面的说明:

访问时间  用户ID  [查询词]  该URL在返回结果中的排名  用户点击的顺序号  用户点击的URL 

上传至HDFS:

$ hdfs dfs -put SogouQ.reduced /

3.数据清洗

   因为原始数据中有些行的字段数不为6,且原始数据的字段分隔符不是Hive表规定的逗号',',所以需要对原始数据进行数据清洗。

  通过编写MapReduce程序完成数据清洗:

      a.将不满足6个字段的行删除

      b.将字段分隔符由不等的空格变为逗号‘,’分隔符

3.1 Eclipse新建Maven工程:Zongheshiyan

Group Id填写com, Artifact Id填写Zongheshiyan

新建工程目录结构如下: 

3.2 修改pom.xml文件

 设置主类:在</project>一行之前添加如下语句

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <!-- main()所在的类,注意修改为包名+主类名 -->
                  <mainClass>com.Zongheshiyan.App</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

 添加依赖:在  </dependencies>一行之前添加如下语句

    <dependency>
	    <groupId>org.apache.hadoop</groupId>
	    <artifactId>hadoop-common</artifactId>
	    <version>2.7.3</version>
	</dependency>
	<dependency>
	    <groupId>org.apache.hadoop</groupId>
	    <artifactId>hadoop-client</artifactId>
	    <version>2.7.3</version>
	</dependency>
	<dependency>
	    <groupId>org.apache.hadoop</groupId>
	    <artifactId>hadoop-hdfs</artifactId>
	    <version>2.7.3</version>
	</dependency>
	<dependency>
	    <groupId>org.apache.hadoop</groupId>
	    <artifactId>hadoop-mapreduce-client-core</artifactId>
	    <version>2.7.3</version>
    </dependency>

3.3 新建SogouMapper类

3.4 编写代码

SogouMapper.java

package com.Zongheshiyan;


import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

//                                                                                         k1     ,        v1,     k2   ,    v2
public class SogouMapper extends Mapper<LongWritable,Text,Text,NullWritable> {

    @Override
    /**
     * 在任务开始时,被调用一次。且只会被调用一次。
     */
    protected void setup(Context context) throws IOException, InterruptedException {
        super.setup(context);
    }

    @Override
    protected void map(LongWritable k1, Text v1, Context context) throws IOException, InterruptedException {
        //避免乱码
        //数据格式:20111230000005  57375476989eea12893c0c3811607bcf    奇艺高清        1      1      http://www.qiyi.com/
        String data = new String(v1.getBytes(),0,v1.getLength(),"GBK");
        
        //split("\\s+") \\s+为正则表达式,意思是匹配一个或多个空白字符,包括空格、制表、换页符等。
        //参考:http://www.runoob.com/java/java-regular-expressions.html
        String words[] = data.split("\\s+");
        
        //判断数据如果不等于6个字段,则退出程序
        if(words.length != 6){
            return;//return语句后不带返回值,作用是退出该程序的运行  https://www.cnblogs.com/paomoopt/p/3746963.html
        }
        //用逗号代替空白字符
        String newData = data.replaceAll("\\s+",",");
        //输出
        context.write(new Text(newData),NullWritable.get());
    }

    @Override
    /**
     * 在任务结束时,被调用一次。且只会被调用一次。
     */
    protected void cleanup(Context context) throws IOException, InterruptedException {
        super.cleanup(context);
    }
}

 App.java

package com.Zongheshiyan;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


/**
 * 数据清洗器 主类
 *
 */
public class App 
{
    public static void main( String[] args ) throws Exception {
        Configuration conf = new Configuration();

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

        //指定map输出
        job.setMapperClass(SogouMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        //指定reduce的输出
        job.setOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        //指定输入、输出
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        //提交job,等待结束
        job.waitForCompletion(true);

    }
}

 3.5 打包工程:mvn clean package

 3.6上传到Linux: WinScp工具

 3.7 运行jar包

  在运行jar包之前,确保开启了hadoop所有进程

start-all.sh

 同时也把mr历史服务器进程打开

mr-jobhistory-daemon.sh start historyserver

  运行jar包: 

hadoop jar Zongheshiyan-0.0.1-SNAPSHOT.jar /SogouQ.reduced /out/Oneday

机器配置不同,执行时间也不同(执行时间约3分钟左右)。 看到输出如下图所示为执行成功。

 查看输出结果

hdfs dfs -ls /out/Oneday

查看输出文件最后10行数据: 

hdfs dfs -tail /out/Oneday/part-r-00000

4.创建hive表

   进入hive命令行

hive

   创建hive表 

create table sogoulog_1(accesstime string,useID string,keyword string,no1 int,clickid int,url string) row format delimited fields terminated by ',';

5.将MapReduce清洗后的数据导入Hive sogoulog_1表中

load data inpath '/out/Oneday/part-r-00000' into table sogoulog_1;

6.使用SQL查询满足条件的数据(只显示前10条)

select * from sogoulog_1 where no1=2 and clickid=1 limit 10;

其实,还可以对数据做一些探索,例如:

查看 sogoulog_1表结构

hive> describe sogoulog_1;

OK

accesstime           string                                   

useid                string                                   

keyword              string                                   

no1                  int                                      

clickid              int                                      

url                  string                                   

Time taken: 0.411 seconds, Fetched: 6 row(s)

一天内,一共搜索关键词的个数

hive> select count(keyword) from sogoulog_1;

1724253

第一次点击的次数来看,排名越靠前,点击次数越多

hive> select count(keyword) from sogoulog_1 where no1=1 and clickid=1;

279492

hive> select count(keyword) from sogoulog_1 where no1=2 and clickid=1;

99224

hive> select count(keyword) from sogoulog_1 where no1=3 and clickid=1;

50782

 

从排名第一URL来看,点击顺序越小越多(首先被点到的可能性就越大)。

hive> select count(keyword) from sogoulog_1 where no1=1 and clickid=1;

279492

hive> select count(keyword) from sogoulog_1 where no1=1 and clickid=2;

79721

hive> select count(keyword) from sogoulog_1 where no1=1 and clickid=3;

39726

小结:

MapReduce对原始数据进行清洗,是本实验的难点,要结合注释看懂代码(数据清洗)。

hive对数据进行数据分析,找到隐含在数据中的规律/价值(数据挖掘)。 

还可以做的是数据可视化等。

完成! enjoy it!

猜你喜欢

转载自blog.csdn.net/qq_42881421/article/details/84899795