HIVE分析日志

回复 收藏
hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供完整的sql查询功能,可以将sql语句转换为MapReduce任务进行运行。
其优点是学习成本低,可以通过类SQL语句快速实现简单的MapReduce统计,不必开发专门的MapReduce应用,十分适合数据仓库的统计分析。

Hadoop安装
http://www.lishiming.net/thread-5637-1-1.html

Hadoop官方文档
https://cwiki.apache.org/confluence/display/Hive/Home

一、HIVE安装
HIVE下载合适版本,支持对应的Hadoop版本 http://hive.apache.org/releases.html

在指定的目录下
解压 tar zxvf hive-0.11.0.tar.gz

配置环境 变量
  1. vi /etc/profile
  2. export HIVE_INSTALL=/usr/local/hive-0.11.0
  3. export PATH=$PATH:$HIVE_INSTALL/bin
1、交互模式
  1. hive> show tables;
  2. OK
  3. Time taken: 6.896 seconds
第一次执行时慢,是正在机器上创建metastore数据库,数据库放置在hive命令执行下的目录中,metastore_db

2、非交互模式
  1. hive -e 'show tables'
  2. 如果执行多条语句 ,可以写入脚本 中 script.q
  3. hive -f script.q
  4. hive -S  不打印输出内容
二、HIVE的配置
HIVE的配置文件放置在$HIVE_INSTALL/conf目录中,同Hadoop一样,也是XML文件,可以指定--config参数 ,指定配置文件目录
  1. hive --config=/usrs/home/hive-conf
也可以指定 HIVE_CONF_DIR环境变量来指定文件目录
1、HIVE的主要配置文件为hive-site.xml,其中主要修改目录存储的项目,如果用MYSQL,要做相应修改
与Hadoop相关联配置,均在此处修改
  1. cp hive-default.xml.template  hive-site.xml
2、修改env中部分环境变量,主要修改warehouse存储路径,默认的/user/hive/warehouse,修改为/usr/local/hive-0.11.0/warehouse
  1. [root@zabbix conf]# grep -v "^\#" hive-env.sh
  2. HADOOP_HOME=/usr/local/hadoop
  3. export HIVE_CONF_DIR=/usr/local/hive-0.11.0/conf
修改hive-site.xml中的hive.metastore.warehouse.dir项
cp hive-default.xml.template  hive-site.xml
  1. hive.metastore.warehouse.dir/usr/local/hive-0.11.0/warehouselocation of default database for the warehouse
三、HIVE应用
根据特定的日志格式,导入HIVE中,首先按日志格式建表
日志格式为
  1. 1.1.1.1  m.c.com - - [08/Oct/2013:00:02:08 +0800] "GET /thread-559-1-1.html HTTP/1.1" 200 9006 "-" "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1)"
建表
  1. CREATE TABLE test(host STRING,  web STRING,identity STRING, user STRING ,time STRING ,method STRING,request STRING,protocol STRING,status STRING, size STRING,referer STRING,agent STRING) ROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.RegexSerDe' WITH SERDEPROPERTIES (
  2. "input.regex" = "([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) (-|\\[[^\\]]*\\]) (\"[^ ]*) ([^ ]*) ([^ ]*\") (-|[0-9]*) (-|[0-9]*)(?: ([^ \"]*|\"[^\"]*\") ([^ \"]*|\"[^\"]*\"))?",
  3. "output.format.string" = "%1$s %2$s %3$s %4$s %5$s %6$s %7$s %8$s %9$s %10$s %11$s %12$s")STORED AS TEXTFILE;
导入数据时可以指定导入本地数据 或HDFS日志文件
1、导入本地数据
  1. LOAD DATA LOCAL INPATH '/usr/local/log/m.m.com_20131008.gz' OVERWRITE INTO TABLE test;
2、导入HDFS数据
先将本地日志文件复制到HDFS中
  1. hadoop fs  -put /usr/local/log/m.m.com_20131008.gz  /usr/local/log/
导入HDFS日志
  1. hive> LOAD DATA INPATH '/usr/local/log/m.m.com_20131008.gz' OVERWRITE INTO TABLE hdfstest;
查看表结构是否正确
  1. hive> describe test;
当用"select count (*)"时报错,要引入JAR包
  1. hive> add jar /usr/local/hive-0.11.0/lib/hive-contrib-0.11.0.jar ;
  2. Added /usr/local/hive-0.11.0/lib/hive-contrib-0.11.0.jar to class path
  3. Added resource: /usr/local/hive-0.11.0/lib/hive-contrib-0.11.0.jar
查找日志总数
  1. hive> select count(*) from test;
  2. ...
  3. OK
  4. 1099483
  5. Time taken: 27.846 seconds, Fetched: 1 row(s)
查看排名前10的URL访问
  1. hive> select request,count(request) as numrequest from test group by request sort by numrequest desc limit 10;
  2. OK
  3. /forum-103-1.html       17004
  4. /forum-103-2.html       6023
  5. /forum-103-3.html       2395
  6. /forum-103-4.html       1380
  7. /       1375
  8. /forum-103-5.html       1093
  9. /forum-103-6.html       840
  10. /cee/index.php/comment/news/more/5347782 797
  11. /csdf/index.php/comment/news/more/5348055 788
  12. /member.php?mod=logging&action=login    661
  13. Time taken: 69.206 seconds, Fetched: 10 row(s)
2013-10-10 11:25 举报
已邀请:

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: