1. 程式人生 > >hbase基本命令,api操作

hbase基本命令,api操作

hbase單節點安裝

    啟動一個單節點的hbase方便我們測試,學習。在官網下載hbase.tar.gz安裝包 https://hbase.apache.org/

修改配置檔案${hbase}/conf/hbase-env.sh,指定jdk位置

【hbase-env.sh】

export JAVA_HOME=/usr/local/jdk

【hbase-site.xml】

指定hbase的data路徑,zookeeper的data路徑

<configuration>
  <property>
    <name>hbase.rootdir</name>
    <value>file:///home/hadoop/hbase/data</value>
  </property>
  <property>
    <name>hbase.zookeeper.property.dataDir</name>
    <value>/home/hadoop/hbase/zkData</value>
  </property>
  <property>
    <name>hbase.unsafe.stream.capability.enforce</name>
    <value>false</value>
    <description>
      Controls whether HBase will check for stream capabilities (hflush/hsync).
      Disable this if you intend to run on LocalFileSystem, denoted by a rootdir
      with the 'file://' scheme, but be mindful of the NOTE below.
      WARNING: Setting this to false blinds you to potential data loss and
      inconsistent system state in the event of process and/or node failures. If
      HBase is complaining of an inability to use hsync or hflush it's most
      likely not a false positive.
    </description>
  </property>
</configuration>
hbase單節點啟動
//start-hbase.sh
>start-hbase.sh
//檢視webUI,“localhost:16010”


hbase_shell操作

$>hbase shell  //登入shell終端
$hbase>help								//檢視幫助
$hbase>help	'list_namespace'			//檢視(特定命令)名稱空間命令幫助
$hbase>list_namespace					//列出名字空間(資料庫)
$hbase>list_namespace_tables 'defalut'	//列出名字空間(資料庫)
$hbase>create_namespace 'ns1'			//建立名字空間
$hbase>help 'create'
$hbase>create 'ns1:t1','f1'				//建立表,指定空間下
$hbase>put 'ns1:t1','row1','f1:id',100		//插入資料
$hbase>put 'ns1:t1','row1','f1:name','tom'	//

$hbase>get 'ns1:t1','row1'					//查詢指定row
$hbase>scan 'ns1:t1'						//掃描表
$hbase>flush 'ns1:t1'//清理記憶體資料到磁碟。
$hbase>count 'ns1:t1'//統計函式
$hbase>disable 'ns1:t1'//刪除表之前需要禁用表
$hbase>drop 'ns1:t1'//
$hbase>scan 'hbase:meta'//檢視元資料表
$hbase>split 'ns1:t1'//切割表 
$hbase>split ''//切割區域
//給表新增列簇,先禁用表
>disable 'test'
//如果表在default空間中,可以不加空間限定
>disable 'default:test'
//修改表,列簇是“NAME=>'cf2'”,最多保留3個版本的資料
>alter 'test',{NAME=>'cf2',VERSIONS=>3}
//然後enable表
>enable 'test'

java api操作

需要把“hbase-site.xml”放到類路徑下

1.put get

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

public class TestCRUD {

    @Test
    public void testPut() throws Exception {
        //建立配置檔案
        Configuration conf = HBaseConfiguration.create();
        //建立連線
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("default:test");
        //根據表名獲得表物件
        Table table = conn.getTable(tableName);
        byte[] row1 = Bytes.toBytes("row2");
        //利用put物件向表中插入資料,new的時候需要指定rowkey
        Put put = new Put(row1);
        byte[] cf1 = Bytes.toBytes("cf1");
        byte[] col = Bytes.toBytes("id");
        byte[] value = Bytes.toBytes("101");
        //put物件新增一列,需要引數列簇,列,值
        put.addColumn(cf1,col,value);
        table.put(put);
        conn.close();
    }
    /**
     * 根據rowkey,列簇,列,查詢value
     * @throws Exception
     */
    @Test
    public void testGet() throws Exception{
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("default:test");
        Table table = conn.getTable(tableName);
        byte[] row2 = Bytes.toBytes("row1");
        //通過get物件,執行對錶中一行的get操作
        Get get_row2 = new Get(row2);
        Result r = table.get(get_row2);
        byte[] cf1 = Bytes.toBytes("cf1");
        byte[] col = Bytes.toBytes("id");
        byte[] idvalue = r.getValue(cf1,col);
        System.out.println(Bytes.toString(idvalue));
    }
}

hbase中的一些概念

寫前日誌

WAL,"write ahead log",寫前日誌。位於、hbase/meta下,表的操作先寫WAL裡
如果插入大量資料
    table.setAutoFlush(false);//關閉表的自動flush
    put.setWriteToWAL(false);//關閉寫前日誌
    table.flushCommits();//提交日誌

hbase基於hdfs

相同列簇的資料存放在一個檔案中。hdfs://ip:port/hbase/data/${名稱空間}/${表名}/${區域名稱}/${列簇名稱}/${檔名}
[WAL目錄結構構成]
hdfs://s201:8020/hbase/WALs/${區域伺服器名稱,主機名,埠號,時間戳}/

client端互動過程
0.hbase叢集啟動時,master負責分配區域到指定區域伺服器。
1.聯絡zk,找出meta表所在rs(regionserver)
/hbase/meta-region-server
2.定位row key,找到對應region server
3.快取資訊在本地。
4.聯絡RegionServer
5.HRegionServer負責open HRegion物件,為每個列族建立Store物件,Store包含多個StoreFile例項,
 他們是對HFile的輕量級封裝。每個Store還對應了一個MemStore,用於記憶體儲存資料。