HBAse構成および操作例


一、Hbaseクラスタ構築
1.圧縮パッケージのアップロードと解凍
  圧縮パッケージhbase.tar.gzをダウンロードし、tar zxvf hbase.tar.gzを実行
2.hbaseクラスタの構成、プロファイルの変更
2.1 hbase-env.shの変更
export JAVA_HOME=/usr/java/jdk1.7.0_55
//  hbase     zk
export HBASE_MANAGES_ZK=false
# Extra Java CLASSPATH elements.  Optional.
export HBASE_CLASSPATH=/home/hadoop/etc/hadoop/

2.2 hbase-site.xml
	
        
                  mini1:60000
        
        
                  180000
        
        
                  hbase.rootdir
                  hdfs://mini1:9000/data/hbase#hbase    ,   hbase  
        
        
                  hbase.master.info.port
                    60010
            
        
                  hbase.cluster.distributed  #       ,false    
                  true
        
        
                   hbase.zookeeper.quorum#zookeeper  
                   mini1,mini2,mini3
        
        
                   hbase.zookeeper.property.dataDir#zookeeper>         
                   /home/hadoop/hbase/tmp/zookeeper
        


2.3 regionservers
mini2
mini3
mini1

3.hbaseを他のノードにコピーする
	scp -r /home/hadoop/hbase/ mini2:/home/hadoop/
	scp -r /home/hadoop/hbase/ mini3:/home/hadoop/

4.同期時間
    
tzselect

sudo cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

5.起動
	    zk
		./zkServer.sh start
	  hbase  
		start-dfs.sh
	  hbase,       :
		start-hbase.sh

ブラウザからhbase管理ページにアクセスします.192.168.157.129:60010
6.クラスタの信頼性を確保するため、複数のHMasterを起動する
	hbase-daemon.sh start master

7.拡張ノード
           
     hbase-daemon.sh start regionserver

二、保存データdemo
新規HBAseUtils
public class HBaseUtils {

    HBaseAdmin admin = null;
    Configuration configuration = null;

    private HBaseUtils(){
        configuration = new Configuration();
        configuration.set("hbase.zookeeper.quorum", "mini1,mini2,mini3");
//        configuration.set("hbase.zookeeper.property.clientPort","2181"); //   
        configuration.set("hbase.rootdir", "hdfs://192.168.157.129:9000/data/hbase");
        try {
            admin = new HBaseAdmin(configuration);
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    private static HBaseUtils instance = null;

    public  static synchronized HBaseUtils getInstance() {
        if(null == instance) {
            instance = new HBaseUtils();
        }
        return instance;
    }

    /**
     *        HTable  
     * @param tableName
     * @return
     */
    public HTable getTable(String tableName) {

        HTable table = null;
        try {
            table = new HTable(configuration,tableName);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return table;
    }

    /**
     *        HBase 
     * @param tableName HBase  
     * @param rowkey HBase  rowkey
     * @param cf  HBase  columnfamily
     * @param column  HBase   
     * @param value    HBase   
     */
    public void put(String tableName, String rowkey, String cf, String column, String value) {
        HTable table = getTable(tableName);
        Put put = new Put(Bytes.toBytes(rowkey));

        put.add(Bytes.toBytes(cf),Bytes.toBytes(column), Bytes.toBytes(value));

        try {
            table.put(put);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        String tableName = "course_clickcount" ;
        String rowkey = "20171111_88";
        String cf = "info" ;
        String column = "click_count";
        String value = "2";

        HBaseUtils.getInstance().put(tableName, rowkey, cf, column, value);

//        HTable table = HBaseUtils.getInstance().getTable("course_clickcount");
//        System.out.println(table.getName().getNameAsString());
    }


	}

開発言語scala:
  エンティティクラスを定義します.
case class CourseClickCount(day_course:String, click_count:Long)

 定義daoレイヤ操作データ
def save(list: ListBuffer[CourseClickCount]) : Unit = {

    val table = HBaseUtils.getInstance().getTable(tableName)

    for(ele 

テスト:
def main(args: Array[String]): Unit = {
    val list = new ListBuffer[CourseClickCount]
    list.append(CourseClickCount("20171111_8",8))
    list.append(CourseClickCount("20171111_9",9))
    list.append(CourseClickCount("20171111_1",100))

    save(list)	
  }

三、クエリーデータdemo
 scala言語を採用し、rowkeyクエリー値に基づいて
def count(day_course: String):Long = {
    val table = HBaseUtils.getInstance().getTable(tableName)
    val get = new Get(Bytes.toBytes(day_course))
    val value = table.get(get).getValue(cf.getBytes,qualifer.getBytes)

    if(value == null){
      0L
    } else {
      Bytes.toLong(value)
    }

  }

 java言語でテーブル名と入力条件に基づいて取得するHBAseのレコード数
public Map query(String tableName, String condition) throws Exception {
        Map 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 map = HBaseUtils.getInstance().query("course_clickcount" , "20171022");

        for(Map.Entry entry: map.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }