zoukankan      html  css  js  c++  java
  • 8 Hbase get方式获取数据

    package com.hikvision.hbase.vertify.test;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.io.IOException;
    import java.io.InterruptedIOException;
    import java.io.UnsupportedEncodingException;
    
    /**
     */
    public class HbaseGetTest {
        public static final Logger LOGGER= LoggerFactory.getLogger(HbasePutTest.class);
        public final static String HBASE_MASTER = "hbase.master";
        public final static String HBASE_ZOOKEEPER_PORT = "hbase.zookeeper.property.clientPort";
        public final static String HBASE_ZOOKEEPER_QUORUM = "hbase.zookeeper.quorum";
        public static HTable htable;
        public static void main(String[] args) {
            String hbaseMaster="10.17.139.121:60010";
            String zookeeperPort="2181";
            String zookeeperQuorum="10.17.139.121";
            String tableName="testHbaseHdfsFileCopy";
            Configuration config= HBaseConfiguration.create();
            config.set(HBASE_MASTER, hbaseMaster);
            config.set(HBASE_ZOOKEEPER_PORT, zookeeperPort);
            config.set(HBASE_ZOOKEEPER_QUORUM, zookeeperQuorum);
    
            try {
                htable = new HTable(config, tableName);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            if(null==htable){
                LOGGER.error("htable is null");
                System.exit(-1);
            }
            int i=0;
            while(true){
                String rowkey="";
                if(i<1000) {
                    rowkey = "201609000"+i;
                }else if(i<2000){
                    rowkey = "201609001"+i;
                }
                else if(i<3000){
                    rowkey = "201609002"+i;
                }
                else if(i<4000){
                    rowkey = "201609003"+i;
                }
                else if(i<5000){
                    rowkey = "201609004"+i;
                }
                else if(i<6000){
                    rowkey = "201609005"+i;
                }
                else if(i<7000){
                    rowkey = "201609006"+i;
                }
                else if(i<8000){
                    rowkey = "201609007"+i;
                }
                else if(i<9000){
                    rowkey = "201609008"+i;
                }
                else if(i<10000){
                    rowkey = "201609009"+i;
                }else if(i<11000){
                    rowkey = "201609010"+i;
                }
                String family="info";
                String column=""+i;
                Result result=getDataFromTable(rowkey,family,column);
                try {
                    LOGGER.info(new String(result.getValue(Bytes.toBytes(family),Bytes.toBytes(column)),"utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
                if(i==11000){
                    i=0;
                }
            }
        }
        /**
         * @Description:往表中插入数据
         * @param rowkey
         * @param ts
         * @param family
         * @param column
         * @param value void:
         */
        public static void putDataToTable(String rowkey, long ts, String family, String column, int value) {
            Put put = new Put(Bytes.toBytes(rowkey), ts);
            put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
            try {
                htable.put(put);
            } catch (RetriesExhaustedWithDetailsException e) {
                e.printStackTrace();
            } catch (InterruptedIOException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * @Description:往表中插入数据
         * @param rowkey
         * @param ts
         * @param family
         * @param column
         * @param value void:
         */
        public static void putDataToTable(String rowkey, long ts, String family, String column, String value) {
            Put put = new Put(Bytes.toBytes(rowkey), ts);
            put.add(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
            try {
                htable.put(put);
            } catch (RetriesExhaustedWithDetailsException e) {
                e.printStackTrace();
            } catch (InterruptedIOException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * @Description:从表中查询数据
         * @param rowkey
         * @param family
         * @param column
         * @return Result:
         */
        public static Result getDataFromTable(String rowkey, String family, String column) {
            Get get = new Get(Bytes.toBytes(rowkey));
            get.addColumn(Bytes.toBytes(family), Bytes.toBytes(column));
            Result dbresult = null;
            try {
                dbresult = htable.get(get);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return dbresult;
        }
    
        /**
         * @Description:从表中查询数据
         * @param rowkey
         * @param family
         * @param column
         * @return Result:
         */
        public static Result getDataFromTable(String rowkey, String family, String... column) {
            Get get = new Get(Bytes.toBytes(rowkey));
            for (String string : column) {
                get.addColumn(Bytes.toBytes(family), Bytes.toBytes(string));
            }
            Result dbresult = null;
            try {
                dbresult = htable.get(get);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return dbresult;
        }
    }
  • 相关阅读:
    图片中添加文字
    几种经典的滤波算法(转)
    OPENCV初试
    图像处理和图像识别中常用的OpenCV函数
    SIP开发
    【转】opencv老是卡在某一层,
    大电子文件读取成二进制流方案
    C# 调试方法之即时窗口输出
    关于如何解锁你的WP7,以便安装自己开发的程序。
    Windows phone 7 之初体验(一.安装Windows phone 7 sdk)
  • 原文地址:https://www.cnblogs.com/yangh2016/p/5995850.html
Copyright © 2011-2022 走看看