hbase所需的相关maven依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-mapreduce</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
package com.briup.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
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.util.ArrayList;
import java.util.Map;
import java.util.NavigableMap;
public class HbaseOperator {
private Connection conn;
private Admin admin;
private Table table;
@Before
public void getConnection() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.10.129:2181");
conn = ConnectionFactory.createConnection(conf);
admin = conn.getAdmin();
}
@Test
public void createNS() throws IOException {
NamespaceDescriptor.Builder nsd = NamespaceDescriptor.create("bd2101");
nsd.addConfiguration("name", "briup");
NamespaceDescriptor nd = nsd.build();
System.out.println("准备创建命名空间。。。");
admin.createNamespace(nd);
System.out.println("创建命名空间成功");
}
public void listNS() throws IOException {
admin = conn.getAdmin();
NamespaceDescriptor[] nds = admin.listNamespaceDescriptors();
for (NamespaceDescriptor nd : nds) {
System.out.println("namespacce => " + nd.getName());
}
}
@Test
public void list_NS_TB() throws IOException {
admin = conn.getAdmin();
TableName[] tableNames = admin.listTableNamesByNamespace("burup");
for (TableName tb : tableNames) {
System.out.println(tb.getName());
}
}
@Test
public void deleteNS() throws IOException {
admin = conn.getAdmin();
admin.deleteNamespace("bd2101");
}
@Test
public void createTB() throws IOException {
admin = conn.getAdmin();
TableName tableName = TableName.valueOf("bd2101:emp");
TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(tableName);
ColumnFamilyDescriptorBuilder cfdb = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f1"));
ColumnFamilyDescriptor cfd = cfdb.build();
tdb.setColumnFamily(cfd);
TableDescriptor td = tdb.build();
admin.createTable(td);
System.out.println("表创建成功。。。");
}
@Test
public void delete_TB() throws IOException {
admin = conn.getAdmin();
TableName tableName = TableName.valueOf("bd2101:emp");
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("表删除成功。。。");
}
@Test
public void put_TB() throws IOException {
table = conn.getTable(TableName.valueOf("bd2101:emp"));
for (int i = 0; i < 3; i++) {
Put put = new Put(Bytes.toBytes("40000" + i));
put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"), Bytes.toBytes("briup" + i));
put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("age"), Bytes.toBytes(18 + i));
put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("gender"), Bytes.toBytes("male"));
table.put(put);
}
}
@Test
public void getTB() throws IOException {
table = conn.getTable(TableName.valueOf("bd2101:emp"));
Get get = new Get(Bytes.toBytes("100000"));
Result result = table.get(get);
System.out.println(Bytes.toString(result.getRow()));
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
showTables(map);
}
private static void showTables(NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map) {
for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> f : map.entrySet()) {
System.out.print(" 列族:" + new String(f.getKey()));
NavigableMap<byte[], NavigableMap<Long, byte[]>> qs = f.getValue();
for (Map.Entry<byte[], NavigableMap<Long, byte[]>> q : qs.entrySet()) {
System.out.print(" 表名:" + new String(q.getKey()));
NavigableMap<Long, byte[]> values = q.getValue();
for (Map.Entry<Long, byte[]> val : values.entrySet()) {
System.out.print(" 版本:" + val.getKey());
System.out.print(" 值:" + new String(val.getValue()));
}
}
}
System.out.println();
}
@Test
public void scanTB() throws IOException {
table = conn.getTable(TableName.valueOf("bd2101:emp"));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
System.out.print("行键:" + Bytes.toString(result.getRow()));
showTables(map);
}
}
@Test
public void scan_limit_TB() throws IOException {
table = conn.getTable(TableName.valueOf("bd2101:emp"));
Scan scan = new Scan();
scan.setLimit(4);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
System.out.println("行键:" + Bytes.toString(result.getRow()));
showTables(map);
}
}
@Test
public void scan_Filter() throws IOException {
table = conn.getTable(TableName.valueOf("bd2101:emp"));
Scan scan = new Scan();
PrefixFilter filter = new PrefixFilter(Bytes.toBytes("10"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
System.out.print("行键:" + Bytes.toString(result.getRow()));
showTables(map);
}
}
@Test
public void scan_Filter1() throws IOException {
table = conn.getTable(TableName.valueOf("bd2101:emp"));
Scan scan = new Scan();
Filter filter = new KeyOnlyFilter();
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
System.out.print("行键:" + Bytes.toString(result.getRow()));
showTables(map);
}
}
@Test
public void scan_Filter2() throws IOException {
table = conn.getTable(TableName.valueOf("bd2101:emp"));
Scan scan = new Scan();
RandomRowFilter filter = new RandomRowFilter(0.5f);
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
System.out.print("行键:" + Bytes.toString(result.getRow()));
showTables(map);
}
}
@Test
public void scan_Filter3() throws IOException {
table = conn.getTable(TableName.valueOf("bd2101:emp"));
Scan scan = new Scan();
ArrayList<MultiRowRangeFilter.RowRange> list = new ArrayList<>();
list.add(new MultiRowRangeFilter.RowRange("1000", true, "400", true));
MultiRowRangeFilter filter = new MultiRowRangeFilter(list);
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
System.out.print("行键:" + Bytes.toString(result.getRow()));
showTables(map);
}
}
@Test
public void scan_Filter4() throws IOException {
table = conn.getTable(TableName.valueOf("bd2101:emp"));
Scan scan = new Scan();
ValueFilter filter = new ValueFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("jake0")));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
System.out.print("行键:" + Bytes.toString(result.getRow()));
showTables(map);
}
}
@Test
public void scan_Filter5() throws IOException {
table = conn.getTable(TableName.valueOf("bd2101:emp"));
Scan scan = new Scan();
ValueFilter filter = new ValueFilter(CompareOperator.EQUAL, new RegexStringComparator(".*ja.*"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
System.out.print("行键:" + Bytes.toString(result.getRow()));
showTables(map);
}
}
@After
public void close() throws IOException {
if (conn != null) {
conn.close();
}
if (admin != null) {
admin.close();
}
if (table != null) {
table.close();
}
}
}
|