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
