你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2014-06-12] 来源:Linux社区  作者:王奎 [字体:大 中 小]

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

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-06/103055.htm

linux
相关资讯       Hadoop日志  Hadoop和Hive 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款