offset (0) + length (8) exceed the capacity of the array: 1


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());
       }
   }

}
http://img3.mukewang.com/5a8d1a120001422408070113.jpg

qq_易永胜_0
浏览 2252回答 1
1回答

wangsha0

cf.getBytes()--->Bytes.toBytes(cf)or long clickCount--->String clickCountor 文件中含有有null值??
打开App,查看更多内容
随时随地看视频慕课网APP