阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用Hadoop和Hive来进行应用的日志数据分析

103次阅读
没有评论

共计 6795 个字符,预计需要花费 17 分钟才能阅读完成。

整个架构流程的背景是:

1、各个应用产生日志打印约定格式的 syslog,然后在服务器端部署 syslog-ng server 做日志的统一收集。

2、在 syslog-ng server 所在的服务器做日志文件的分类然后发送日志消息给 storm 做实时流数据统计。

3、同时每日凌晨启动 rsync 服务将前一天的日志文件发送到 Hadoop 和 hive 服务器做非实时数据分析

————————————– 分割线 ————————————–

Ubuntu 13.04 上搭建 Hadoop 环境 http://www.linuxidc.com/Linux/2013-06/86106.htm

Ubuntu 12.10 +Hadoop 1.2.1 版本集群配置 http://www.linuxidc.com/Linux/2013-09/90600.htm

Ubuntu 上搭建 Hadoop 环境(单机模式 + 伪分布模式)http://www.linuxidc.com/Linux/2013-01/77681.htm

Ubuntu 下 Hadoop 环境的配置 http://www.linuxidc.com/Linux/2012-11/74539.htm

单机版搭建 Hadoop 环境图文教程详解 http://www.linuxidc.com/Linux/2012-02/53927.htm

Hadoop LZO 安装教程 http://www.linuxidc.com/Linux/2013-01/78397.htm

Hadoop 集群上使用 Lzo 压缩 http://www.linuxidc.com/Linux/2012-05/60554.htm

————————————– 分割线 ————————————–

使用 hadoop 和 hive 来进行应用的日志数据分析的详细流程:

1、安装 hadoop

Hadoop 的安装以及配置在我的文章中有详细的描述:http://www.linuxidc.com/Linux/2013-05/84303.htm

2、安装 hive

hive 的安装非常简单,只需要下载安装包:

wget http://mirrors.cnnic.cn/apache/hive/hive-0.10.0/hive-0.10.0.tar.gz

然后解压后,修改一点点配置文件用于连接 hadoop 的配置即可。

3、在 hive 中创建表

    /**
    * 根据日期来创建 hive 的邮件日志表
    * @param date
    * @return
    * @throws SQLException
    */
    public String createTable(Calendar cal) throws SQLException
    {
 String tableName = getTableName(cal.getTime());
 StringBuilder sql = new StringBuilder();
        sql.append(“create table if not exists “);
        sql.append(tableName);
        sql.append(“( “);
        sql.append(“syslog_month string,  “);
        if(cal.get(Calendar.DAY_OF_MONTH)<10){
            sql.append(“syslog_day_pre string,  “);
        }
        sql.append(“syslog_day string, “);
        sql.append(“syslog_time string, “);
        sql.append(“ip string, “);
        sql.append(“source string, “);
        sql.append(“message array<string>, “);
        sql.append(“information1 string, “);
        sql.append(“information2 string, “);
        sql.append(“information3 string,  “);
        sql.append(“information4 string,  “);
        sql.append(“information5 string)  “);
        sql.append(“row format delimited fields terminated by ‘ ‘  “);
        sql.append(“collection items terminated by ‘,’  “);
        sql.append(“map keys terminated by  ‘:’ “);
       
        LOG.info(“[ 创建 HIVE 表的 DLL]”+sql.toString());
       
        HiveUtil.createTable(sql.toString());
        return tableName;
    }

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2014-06/103055p2.htm

4、加载日志到 hive 中

    /**
    * 加载本地文件到 hive 邮件日志表
    * @param path
    * @param tableName
    * @throws SQLException
    */
    public void loadData(String path,String tableName) throws SQLException
    {
 StringBuilder sql = new StringBuilder();
 sql.append(“load data local inpath “);
 sql.append(“‘”);
 sql.append(path);
 sql.append(“‘”);
// sql.append(” overwrite into table “);
        sql.append(” into table “);
        sql.append(tableName);
       
        LOG.info(“[ 加载数据到 HIVE 表的 DLL]”+sql.toString());
       
        HiveUtil.loadData(sql.toString());
    }

5、然后就可以根据自己的需求进行各类简单的查询了:
 
(1)查询所有数据

    /**
    * 查询所有数据
    */
    public ResultSet queryData(String tableName) throws SQLException
    {
 StringBuilder sql = new StringBuilder();
 sql.append(“select syslog_month,syslog_day,syslog_time,ip,source,message,”);
 sql.append(“information1,information2,information3,information4,information5 “);
 sql.append(“from “);
 sql.append(tableName);
 
 LOG.info(“[ 查询所有 HIVE 数据的 DLL]”+sql.toString());
 
 ResultSet res = HiveUtil.queryData(sql.toString());
 return res;
    }

(2)查询 UserId 和 CategoryId 分类的总延时

    /**
    * 查询 UserId 和 CategoryId 分类的总延时
    */
    public List<Map<String,Object>> queryHiveDataForUserIdAndCategoryIdDelay(String tableName) throws SQLException
    {
 StringBuilder sql = new StringBuilder();
 sql.append(“select t2.message[4],t2.message[5],count(*),sum(t2.message[1]-t1.message[2]),sum(t2.message[1]-t1.message[2])/count(*) “);
 sql.append(“from “);
 sql.append(“(select * from “+tableName+” where message[0]=’&QUEUE’ ) t1 “);
 sql.append(“FULL OUTER JOIN “);
 sql.append(“(select * from “+tableName+” where message[0]=’&OUT’ OR message[0]=’&WORKERERROR’ OR message[0]=’&ERROR’ ) t2 “);
 sql.append(“ON concat(t1.message[1],’0$’,substring(t1.message[6],2,length(t1.message[6])-3))==t2.message[3] “);
 sql.append(“where t2.message[1]>=t1.message[2] “);
        sql.append(“group by t2.message[4],t2.message[5]”);

        LOG.info(“[ 查询 UserId 和 CategoryId 分类的总延时的 HIVE 数据的 DLL]”+sql.toString());
       
        HiveQueryResultSet res = (HiveQueryResultSet) HiveUtil.queryData(sql.toString());
 List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
 while(res.next())
 {
    int userId = res.getInt(1);
    int categoryId = res.getInt(2);
    int num = res.getInt(3);
    double delay = res.getDouble(4);
    double avgDelay = res.getDouble(5);
   
    Map<String,Object> map = new HashMap<String,Object>();
    map.put(“userId”, userId);
    map.put(“categoryId”, categoryId);
    map.put(“num”, num);
    map.put(“delay”, delay);
    map.put(“avgDelay”, avgDelay);
    list.add(map);
 }
        return list;
    }

更多 Hadoop 相关信息见 Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13

整个架构流程的背景是:

1、各个应用产生日志打印约定格式的 syslog,然后在服务器端部署 syslog-ng server 做日志的统一收集。

2、在 syslog-ng server 所在的服务器做日志文件的分类然后发送日志消息给 storm 做实时流数据统计。

3、同时每日凌晨启动 rsync 服务将前一天的日志文件发送到 Hadoop 和 hive 服务器做非实时数据分析

————————————– 分割线 ————————————–

Ubuntu 13.04 上搭建 Hadoop 环境 http://www.linuxidc.com/Linux/2013-06/86106.htm

Ubuntu 12.10 +Hadoop 1.2.1 版本集群配置 http://www.linuxidc.com/Linux/2013-09/90600.htm

Ubuntu 上搭建 Hadoop 环境(单机模式 + 伪分布模式)http://www.linuxidc.com/Linux/2013-01/77681.htm

Ubuntu 下 Hadoop 环境的配置 http://www.linuxidc.com/Linux/2012-11/74539.htm

单机版搭建 Hadoop 环境图文教程详解 http://www.linuxidc.com/Linux/2012-02/53927.htm

Hadoop LZO 安装教程 http://www.linuxidc.com/Linux/2013-01/78397.htm

Hadoop 集群上使用 Lzo 压缩 http://www.linuxidc.com/Linux/2012-05/60554.htm

————————————– 分割线 ————————————–

使用 hadoop 和 hive 来进行应用的日志数据分析的详细流程:

1、安装 hadoop

Hadoop 的安装以及配置在我的文章中有详细的描述:http://www.linuxidc.com/Linux/2013-05/84303.htm

2、安装 hive

hive 的安装非常简单,只需要下载安装包:

wget http://mirrors.cnnic.cn/apache/hive/hive-0.10.0/hive-0.10.0.tar.gz

然后解压后,修改一点点配置文件用于连接 hadoop 的配置即可。

3、在 hive 中创建表

    /**
    * 根据日期来创建 hive 的邮件日志表
    * @param date
    * @return
    * @throws SQLException
    */
    public String createTable(Calendar cal) throws SQLException
    {
 String tableName = getTableName(cal.getTime());
 StringBuilder sql = new StringBuilder();
        sql.append(“create table if not exists “);
        sql.append(tableName);
        sql.append(“( “);
        sql.append(“syslog_month string,  “);
        if(cal.get(Calendar.DAY_OF_MONTH)<10){
            sql.append(“syslog_day_pre string,  “);
        }
        sql.append(“syslog_day string, “);
        sql.append(“syslog_time string, “);
        sql.append(“ip string, “);
        sql.append(“source string, “);
        sql.append(“message array<string>, “);
        sql.append(“information1 string, “);
        sql.append(“information2 string, “);
        sql.append(“information3 string,  “);
        sql.append(“information4 string,  “);
        sql.append(“information5 string)  “);
        sql.append(“row format delimited fields terminated by ‘ ‘  “);
        sql.append(“collection items terminated by ‘,’  “);
        sql.append(“map keys terminated by  ‘:’ “);
       
        LOG.info(“[ 创建 HIVE 表的 DLL]”+sql.toString());
       
        HiveUtil.createTable(sql.toString());
        return tableName;
    }

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2014-06/103055p2.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计6795字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中