1. 程式人生 > >Hbase的基本操作(CDH組件可用)

Hbase的基本操作(CDH組件可用)

catch 名稱 let 滿足 api LEDE htable ati zookeeper

Habse創建一張表:
1,創建一個命名空間NameSpace(命名空間NameSpace指的是一個表的邏輯分組 ,同一分組中的各個表有類似的用途,相當於關系型數據庫中的DataBase)
admin.createNamespace(NamespaceDescriptor.create("MyNamespace").build());
NamespaceDescriptor類用於對NameSpace的描述
在Hbase中,命名空間、表、列族的創建,都需要先進行描述NamespaceDescriptor、HTableDescriptor、HColumnDescriptor,才能進行相應的操作。
2,創建一張表
//創建tablename對象,描述表的名稱信息,命名空間在此處使用
TableName tname = TableName.valueOf("MyNamespace:students");
//創建HTableDescriptor對象,描述表信息
HTableDescriptor tDescriptor = new HTableDescriptor(tname);
//創建HColumnDescriptor對象,描述列族信息
HColumnDescriptor famliy = new HColumnDescriptor("core");
//將列族的描述信息添加進表的描述信息
tDescriptor.addFamily(famliy);
//利用表的描述信息創建一張表
admin.createTable(tDescriptor);

HBase主要的四類操作,分別是:
Put :增加一行,修改一行
Delete :刪除一行,刪除指定列族,刪除指定column的多個版本,刪除指定column的制定版本等
Get :獲取指定行的所有信息,獲取指定行和指定列族的所有colunm,獲取指定column,獲取指定column的幾個版本,獲取指定column的指定版本等
Scan :獲取所有行,獲取指定行鍵範圍的行,獲取從某行開始的幾行,獲取滿足過濾條件的行等


maven依賴:

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-it</artifactId>
            <version>1.1.2</version>
        </dependency>    

Java API操作:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * @Author:Xavier * @Data:2019-02-20 15:48 *
*/ public class OptionTest { private Admin admin = null; private Connection connection = null; private Configuration conf = null; private TableName tname = null; @Before public void init() throws IOException { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "nn01,nn02,dn01,dn02,dn03,dn04"); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.master", "dn01:60000"); connection = ConnectionFactory.createConnection(conf); admin = connection.getAdmin(); } //創建一張表,指定表名,列族 @Test public void createTabl() throws IOException { try { admin.createNamespace(NamespaceDescriptor.create("MyNamespace").build()); } catch (NamespaceExistException e) { System.out.println("該命名空間已經存在"); } //創建tablename對象,描述表的名稱信息 tname = TableName.valueOf("MyNamespace:students"); //創建HTableDescriptor對象,描述表信息 HTableDescriptor tDescriptor = new HTableDescriptor(tname); if (admin.tableExists(tname)) { System.out.println("students" + "存在!"); System.exit(0); } else { HColumnDescriptor famliy = new HColumnDescriptor("core"); tDescriptor.addFamily(famliy); admin.createTable(tDescriptor); System.out.println("創建表成功!"); } } //獲取所有的表 @Test public void getAllTable() { if (admin != null) { try { HTableDescriptor[] alltable = admin.listTables(); for (HTableDescriptor hTableDesc : alltable) { System.out.println(hTableDesc.getNameAsString()); } } catch (IOException e) { e.printStackTrace(); } } } //刪除表 @Test public void deleteTab() throws IOException { tname = TableName.valueOf("MyNamespace:students"); if (admin.tableExists(tname)) { admin.disableTable(tname);//先禁用表才能刪除 admin.deleteTable(tname); System.out.println("刪除表成功!"); } else { System.out.println("表不存在"); } } //插入一條數據 @Test public void addOneRecord() throws IOException { //通過連接查詢tableName對象 tname = TableName.valueOf("MyNamespace:students"); if(admin.tableExists(tname)) { Table table = connection.getTable(tname); Put put = new Put(Bytes.toBytes("lisi")); put.add(Bytes.toBytes("core"), Bytes.toBytes("math"), Bytes.toBytes("98")); table.put(put); System.out.println("插入數據成功!"); }else { System.out.println("插入數據失敗"); } } //插入多條數據 @Test public void moreInsert() throws IOException { //測試在數據前補零 DecimalFormat format = new DecimalFormat(); format.applyPattern("0000"); tname = TableName.valueOf("MyNamespace:students"); HTable htable = (HTable) connection.getTable(tname); //不要自動清理緩沖區 htable.setAutoFlush(false); // 一個put代表一行數據,再new一個put表示第二行數據,每行一個唯一的RowKey for (int i = 1; i < 10000; i++) { Put put = new Put(Bytes.toBytes("leilei" + format.format(i))); //關閉寫前日誌 put.setWriteToWAL(false); put.addColumn(Bytes.toBytes("core"), Bytes.toBytes("math"), Bytes.toBytes(format.format(i))); put.addColumn(Bytes.toBytes("core"), Bytes.toBytes("english"), Bytes.toBytes(format.format(Math.random() * i))); put.addColumn(Bytes.toBytes("core"), Bytes.toBytes("chinese"), Bytes.toBytes(format.format(Math.random() * i))); htable.put(put); if (i % 2000 == 0) { htable.flushCommits(); } } htable.flushCommits(); htable.close(); } //通過RowKey,faimly,colum獲取cell數據 @Test public void getData() throws IOException { tname = TableName.valueOf("MyNamespace:students"); Table table = connection.getTable(tname); //通過RowKey Get get = new Get(Bytes.toBytes("lisi")); Result result = table.get(get); System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("core"), Bytes.toBytes("math")))); } //掃描在rowkey範圍內的cell數據 @Test public void deleteRangeRK() throws IOException { tname = TableName.valueOf("MyNamespace:students"); Table table = connection.getTable(tname); Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes("leilei1000")); scan.setStopRow(Bytes.toBytes("leilei9999")); ResultScanner resultScanner = table.getScanner(scan); Iterator<Result> iterator = resultScanner.iterator(); while (iterator.hasNext()) { System.out.println(Bytes.toString((iterator.next()).getValue(Bytes.toBytes("core"), Bytes.toBytes("english")))); // System.out.println((iterator.next()).toString()); } } //KeyValue形式查詢一行的數據 @Test public void getValueFromKey() throws IOException { tname = TableName.valueOf("MyNamespace:students"); Table table = connection.getTable(tname); Get get = new Get(Bytes.toBytes("lisi")); Result result = table.get(get); if (result.raw().length == 0) { System.out.println("不存在該關鍵字的行!!"); } else { for (Cell kv : result.rawCells()) { System.out.println( "列:"+Bytes.toString(CellUtil.cloneFamily(kv))+":"+Bytes.toString(CellUtil.cloneQualifier(kv)) +"\t 值:"+Bytes.toString(CellUtil.cloneValue(kv))); } } } //KeyValue形式查詢所有的數據 @Test public void getAllData() throws Exception { tname = TableName.valueOf("MyNamespace:students"); Table table = connection.getTable(tname); Scan scan = new Scan(); ResultScanner rs = table.getScanner(scan); for (Result r : rs) { for (KeyValue kv : r.raw()) { System.out.println(Bytes.toString(kv.getKey()) + Bytes.toString(kv.getValue())); } } } // 刪除一行Hbase表中記錄信息的 @Test public void deleteRecord() throws IOException { tname = TableName.valueOf("MyNamespace:students"); Table table = connection.getTable(tname); Delete de = new Delete(Bytes.toBytes("leilei9997")); try { table.delete(de); System.out.println("刪除記錄成功!!!"); } catch (IOException e) { System.out.println("刪除記錄異常!!!"); } } @After public void destory() throws IOException { admin.close(); connection.close(); } }

Hbase的基本操作(CDH組件可用)