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; ? ?/** ? ? * 私有構(gòu)造方法:加載一些必要的參數(shù) ? ? */ ? ?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; ? ?} ? ?/** ? ? * 根據(jù)表名獲取到HTable實(shí)例 ? ? */ ? ?public HTable getTable(String tableName) { ? ? ? ?HTable table = null; ? ? ? ?try { ? ? ? ? ? ?table = new HTable(conf, tableName); ? ? ? ?} catch (IOException e) { ? ? ? ? ? ?e.printStackTrace(); ? ? ? ?} ? ? ? ?return table; ? ?} ? ?/** ? ? * 根據(jù)表名和輸入條件獲取HBase的記錄數(shù) ? ? */ ? ?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()); ? ? ? ?} ? ?}}
offset (0) + length (8) exceed the capacity of the array: 1
qq_易永勝_0
2018-02-21 15:05:16