HBase學習筆記(3)—— hbase java API
1 hbase依賴zookeeper
儲存Hmaster的地址和backup-master地址
- 管理HregionServer
- 做增刪改查表的節點
- 管理HregionServer中的表分配
儲存表-ROOT-的地址 hbase預設的根表,檢索表。
HRegionServer列表 表的增刪改查資料;和hdfs互動,存取資料。
2 hbase API
2.1 配置
HBaseConfiguration
包:org.apache.hadoop.hbase.HBaseConfiguration
作用:通過此類可以對HBase進行配置
用法例項:
Configuration config = HBaseConfiguration.create();
zoo.cfg
的配置檔案中的埠一致
使用方法:
static Configuration config = null;
static {
config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "slave1,slave2,slave3" );
config.set("hbase.zookeeper.property.clientPort", "2181");
}
2.1.1 測試程式碼
首先叢集要啟動 hadoop,zookeeper,hbase
package demo1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache .hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Table;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class HbaseTest {
static Configuration config = null;
private Connection connection = null;
private Table table = null;
@Before
public void init() throws IOException {
config = HBaseConfiguration.create();
config.set("hbase.master","node1:60000");
config.set("hbase.zookeeper.quorum", "node1,node2,node3");
config.set("hbase.zookeeper.property.clientPort", "2181");
connection = ConnectionFactory.createConnection(config);
table = connection.getTable(TableName.valueOf("user"));
}
@Test
public void createTable() throws IOException {
//建立表管理類
HBaseAdmin admin = new HBaseAdmin(config);
//建立表描述類
TableName tableName = TableName.valueOf("test3");//表名字
HTableDescriptor desc = new HTableDescriptor(tableName);
//建立列族描述類
HColumnDescriptor family = new HColumnDescriptor("info"); //列族
//將列族加入到表
desc.addFamily(family);
HColumnDescriptor family2=new HColumnDescriptor("info2");//列族
desc.addFamily(family2);
admin.createTable(desc);
}
@After
public void close() throws Exception {
table.close();
connection.close();
}
}
2.2 表管理類
HBaseAdmin
包:org.apache.hadoop.hbase.client.HBaseAdmin
作用:提供介面關係HBase 資料庫中的表資訊
用法:
HBaseAdmin admin = new HBaseAdmin(config);
2.3 表描述類
HTableDescriptor 包:org.apache.hadoop.hbase.HTableDescriptor 作用:HTableDescriptor 類包含了表的名字以及表的列族資訊 表的schema(設計) 用法:
HTableDescriptor htd =new HTableDescriptor(tablename);
htd.addFamily(new HColumnDescriptor(“myFamily”));
2.4 列族的描述類
HColumnDescriptor
包:org.apache.hadoop.hbase.HColumnDescriptor
作用:HColumnDescriptor 維護列族的資訊
用法:
htd.addFamily(new HColumnDescriptor(“myFamily”));
2.5 建立表操作
static Configuration config = null;
static {
config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "slave1,slave2,slave3");
config.set("hbase.zookeeper.property.clientPort", "2181");
}
HBaseAdmin admin = new HBaseAdmin(config);
HTableDescriptor desc = new HTableDescriptor(tableName);
HColumnDescriptor family1 = new HColumnDescriptor(“f1”);
HColumnDescriptor family2 = new HColumnDescriptor(“f2”);
desc.addFamily(family1);
desc.addFamily(family2);
admin.createTable(desc);
2.6 刪除表
HBaseAdmin admin = new HBaseAdmin(config);
admin.disableTable(tableName);
admin.deleteTable(tableName);
2.6.1 測試
@Test
public void deleteTable() throws IOException {
HBaseAdmin admin = new HBaseAdmin(config);
admin.disableTable("test3");
admin.deleteTable("test3");
admin.close();
}
2.7 插入資料
@Test
public void insertData() throws IOException {
ArrayList<Put> list =new ArrayList<>();
//建立的資料封裝類
Put put = new Put(Bytes.toBytes("zhangsanfen_1234"));
put.add(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zhangsanfeng"));
put.add(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes(23));
put.add(Bytes.toBytes("info1"),Bytes.toBytes("sex"),Bytes.toBytes(0));
put.add(Bytes.toBytes("info1"),Bytes.toBytes("address"),Bytes.toBytes("Shanghai"));
Put put1 = new Put(Bytes.toBytes("John_1234"));
put1.add(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("John"));
put1.add(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes(20));
put1.add(Bytes.toBytes("info1"),Bytes.toBytes("sex"),Bytes.toBytes(1));
put1.add(Bytes.toBytes("info1"),Bytes.toBytes("address"),Bytes.toBytes("USA"));
list.add(put);
list.add(put1);
//新增資料
table.put(list);
}
2.8 查詢
2.8.1 單條查詢
/*
* 單條查詢
* */
@Test
public void queryData() throws IOException {
//建立查詢封裝的類
Get get=new Get(Bytes.toBytes("John_1234"));
Result result = table.get(get);
byte[] value=result.getValue(Bytes.toBytes("info1"),Bytes.toBytes("name"));
byte[] sex=result.getValue(Bytes.toBytes("info1"),Bytes.toBytes("sex"));
byte[] address=result.getValue(Bytes.toBytes("info1"),Bytes.toBytes("address"));
byte[] age=result.getValue(Bytes.toBytes("info1"),Bytes.toBytes("age"));
System.out.println(Bytes.toString(value));
System.out.println(Bytes.toInt(sex));
System.out.println(Bytes.toString(address));
System.out.println(Bytes.toInt(age));
}
2.8.2 全表掃描
@Test
public void scanData() throws IOException {
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for(Result result:scanner){
byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
byte[] sex = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("sex"));
byte[] address = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("address"));
byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
System.out.println(Bytes.toString(value));
System.out.println(Bytes.toInt(sex));
System.out.println(Bytes.toString(address));
System.out.println(Bytes.toInt(age));
}
}
3 過濾器
3.1 過濾器種類
- 列值過濾器—SingleColumnValueFilter:過濾列值的相等、不等、範圍等;
- 列名字首過濾器—ColumnPrefixFilter:過濾指定字首的列名;
- 多個列名字首過濾器—MultipleColumnPrefixFilter:過濾多個指定字首的列名;
- rowKey過濾器—RowFilter:通過正則,過濾rowKey值。
3.2 列值過濾器—SingleColumnValueFilter
@Test
public void scanDataByFilter1() throws IOException {
SingleColumnValueFilter singleColumnValueFilter =
new SingleColumnValueFilter(Bytes.toBytes("info1"), Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("John"));
Scan scan = new Scan();
scan.setFilter(singleColumnValueFilter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
byte[] sex = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("sex"));
byte[] address = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("address"));
byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
System.out.println(Bytes.toString(value));
System.out.println(Bytes.toInt(sex));
System.out.println(Bytes.toString(address));
System.out.println(Bytes.toInt(age));
}
}
3.3 列名字首過濾器—ColumnPrefixFilter
ColumnPrefixFilter 用於指定列名字首值相等 ColumnPrefixFilter f = new ColumnPrefixFilter(Bytes.toBytes(“values”)); s1.setFilter(f);
3.4 多個列值字首過濾器—MultipleColumnPrefixFilter
MultipleColumnPrefixFilter 和 ColumnPrefixFilter 行為差不多,但可以指定多個字首 byte[][] prefixes = new byte[][] {Bytes.toBytes(“value1”),Bytes.toBytes(“value2”)}; Filter f = new MultipleColumnPrefixFilter(prefixes); s1.setFilter(f);
3.5 rowKey過濾器—RowFilter
RowFilter 是rowkey過濾器 通常根據rowkey來指定範圍時,使用scan掃描器的StartRow和StopRow方法比較好。 Filter f = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator(“^1234”)); //匹配以1234開頭的rowkey s1.setFilter(f);
3.6 FilterList
FilterList 代表一個過濾器列表,可以新增多個過濾器進行查詢,多個過濾器之間的關係有: 與關係(符合所有):FilterList.Operator.MUST_PASS_ALL 或關係(符合任一):FilterList.Operator.MUST_PASS_ONE
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
Scan s1 = new Scan();
filterList.addFilter(new SingleColumnValueFilter(Bytes.toBytes(“f1”), Bytes.toBytes(“c1”), CompareOp.EQUAL,Bytes.toBytes(“v1”) ) );
filterList.addFilter(new SingleColumnValueFilter(Bytes.toBytes(“f1”), Bytes.toBytes(“c2”), CompareOp.EQUAL,Bytes.toBytes(“v2”) ) );
// 新增下面這一行後,則只返回指定的cell,同一行中的其他cell不返回
s1.addColumn(Bytes.toBytes(“f1”), Bytes.toBytes(“c1”));
s1.setFilter(filterList); //設定filter
ResultScanner ResultScannerFilterList = table.getScanner(s1); //返回結果列表