package com.yys.spark.yys_web.utils;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.PrefixFilter;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;import java.util.HashMap;import java.util.Map;/** * HBase操作工具类 */public class HBaseUtils { HBaseAdmin admin = null; Configuration conf = null; /** * 私有构造方法:加载一些必要的参数 */ private HBaseUtils() { conf = new Configuration(); conf.set("hbase.zookeeper.quorum", "spark:2181"); conf.set("hbase.rootdir", "hdfs://spark:9000/hbase"); try { admin = new HBaseAdmin(conf); } catch (IOException e) { e.printStackTrace(); } } private static HBaseUtils instance = null; public static synchronized HBaseUtils getInstance() { if (null == instance) { instance = new HBaseUtils(); } return instance; } /** * 根据表名获取到HTable实例 */ public HTable getTable(String tableName) { HTable table = null; try { table = new HTable(conf, tableName); } catch (IOException e) { e.printStackTrace(); } return table; } /** * 根据表名和输入条件获取HBase的记录数 */ public Map<String, Long> query(String tableName, String condition) throws Exception { Map<String, Long> map = new HashMap<>(); HTable table = getTable(tableName); String cf = "info"; String qualifier = "click_count"; Scan scan = new Scan(); Filter filter = new PrefixFilter(Bytes.toBytes(condition)); scan.setFilter(filter); ResultScanner rs = table.getScanner(scan); for(Result result : rs) { String row = Bytes.toString(result.getRow()); long clickCount = Bytes.toLong(result.getValue(cf.getBytes(), qualifier.getBytes())); map.put(row, clickCount); } return map; } public static void main(String[] args) throws Exception { Map<String, Long> map = HBaseUtils.getInstance().query("yys_course_clickcount" , "20180220"); for(Map.Entry<String, Long> entry: map.entrySet()) { System.out.println(entry.getKey()+ " : " + entry.getValue()); } }}
1 回答

wangsha0
TA贡献1条经验 获得超0个赞
cf.getBytes()--->Bytes.toBytes(cf)
or
long clickCount--->String clickCount
or
文件中含有有null值??
- 1 回答
- 0 关注
- 2317 浏览
添加回答
举报
0/150
提交
取消