hbase基本命令,api操作
阿新 • • 發佈:2019-01-28
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操作
|
|
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,用於記憶體儲存資料。