HBase java API
ddl的操作
package com.bigdata.admin;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
/**
* namespace 的操作
*/
public class HBaseDemo01
{
private Connection connection;
@Before
public void init() throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop84:2181,hadoop85:2181,hadoop86:2181");
connection = ConnectionFactory.createConnection(conf);
}
//create namespace
@Test
public void test01() throws IOException {
Admin admin = connection.getAdmin();
NamespaceDescriptor descriptor = NamespaceDescriptor.create("java_nameSpace").build();
admin.createNamespace(descriptor);
admin.close();
}
//list namespace
@Test
public void test02() throws IOException {
Admin admin = connection.getAdmin();
NamespaceDescriptor[] descriptors = admin.listNamespaceDescriptors();
for (NamespaceDescriptor descriptor : descriptors) {
System.out.println(descriptor.getName());
}
admin.close();
}
//list_namespace_tables
@Test
public void test03() throws IOException {
Admin admin = connection.getAdmin();
TableName[] defaults = admin.listTableNamesByNamespace("default");
for (TableName tableName : defaults) {
System.out.println(tableName.getNameAsString());
}
admin.close();
}
//describe_namespace
@Test
public void test04() throws IOException {
Admin admin = connection.getAdmin();
NamespaceDescriptor descriptor = admin.getNamespaceDescriptor("java_nameSpace");
System.out.println(descriptor);
admin.close();
}
//alter_namespace
@Test
public void test05() throws IOException {
Admin admin = connection.getAdmin();
NamespaceDescriptor descriptor = admin.getNamespaceDescriptor("java_nameSpace");
descriptor.setConfiguration("author","wangWu");
admin.modifyNamespace(descriptor);
admin.close();
}
//drop_namespace
@Test
public void test06() throws IOException {
Admin admin = connection.getAdmin();
admin.deleteNamespace("java_nameSpace");
admin.close();
}
@After
public void close() throws IOException {
connection.close();
}
}
#table的创建
package com.bigdata.admin;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
/**
*
*/
public class HBaseDemo02
{
private Connection connection;
@Before
public void init() throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop84:2181,hadoop85:2181,hadoop86:2181");
connection = ConnectionFactory.createConnection(conf);
}
//create table
@Test
public void test01() throws IOException {
Admin admin = connection.getAdmin();
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf("t_java01"));
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("cf01");
descriptor.addFamily(hColumnDescriptor);//添加列族
admin.createTable(descriptor);
admin.close();
}
//create table--分区
@Test
public void test02() throws IOException {
Admin admin = connection.getAdmin();
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf("t_java02"));
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("cf01");
descriptor.addFamily(hColumnDescriptor);//添加列族
byte[][] splitKeys={"10".getBytes(),"20".getBytes()};
admin.createTable(descriptor,splitKeys);
admin.close();
}
//建表-预分区
@Test
public void test03() throws IOException {
Admin admin = connection.getAdmin();
TableName tableName = TableName.valueOf("t_java003");
HTableDescriptor descriptor = new HTableDescriptor(tableName);
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("cf02");
descriptor.addFamily(hColumnDescriptor);
admin.createTable(descriptor,"10".getBytes(),"30".getBytes(),3);
admin.close();
}
//describe
@Test
public void test04() throws IOException {
Admin admin = connection.getAdmin();
HTableDescriptor descriptor = admin.getTableDescriptor(TableName.valueOf("t2"));
System.out.println(descriptor);
admin.close();
}
//exists
//禁用
//删除表
@Test
public void test05() throws IOException {
Admin admin = connection.getAdmin();
TableName tableName = TableName.valueOf("t_java02");
boolean b = admin.tableExists(tableName);
if(b){
admin.disableTable(tableName);//禁用
admin.deleteTable(tableName);//删除表
}
admin.close();
}
//list
@Test
public void test06() throws IOException {
Admin admin = connection.getAdmin();
//TableName tableName = TableName.valueOf("t_java");
TableName[] tableNames = admin.listTableNames();
for (TableName name : tableNames) {
System.out.println(name.getNameAsString());
}
admin.close();
}
//list_regions
@Test
public void test07() throws IOException {
Admin admin = connection.getAdmin();
List<HRegionInfo> regions = admin.getTableRegions(TableName.valueOf("t_java003"));
for (HRegionInfo region : regions) {
System.out.println(region);
}
admin.close();
}
//alter
@Test
public void test08() throws IOException {
Admin admin = connection.getAdmin();
TableName tableName = TableName.valueOf("t_java003");
HTableDescriptor descriptor = new HTableDescriptor(tableName);
HColumnDescriptor columnDescriptor = new HColumnDescriptor("cf01");
descriptor.addFamily(columnDescriptor);
columnDescriptor.setVersions(1,5);
admin.modifyTable(tableName,descriptor);
admin.close();
}
@After
public void close() throws IOException {
connection.close();
}
}
#table的操作
package com.bigdata.table;
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.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @version 1.0
* @name mir zhou
* @date 2020/12/2 10:15
* @function table 的操作
* @return
*/
public class HBaseDemo03
{
private Connection connection;
@Before
public void init() throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop84:2181,hadoop85:2181,hadoop86:2181");
connection = ConnectionFactory.createConnection(conf);
}
//get
@Test
public void test01() throws IOException {
Table table = connection.getTable(TableName.valueOf("t2"));
Get get = new Get(Bytes.toBytes("1001"));
Result result = table.get(get);
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("--------------");
}
table.close();
}
//get 指定列
@Test
public void test001() throws IOException {
Table table = connection.getTable(TableName.valueOf("t2"));
Get get = new Get(Bytes.toBytes("1001"));
get.addColumn(Bytes.toBytes("cf01"),Bytes.toBytes("name"));
Result result = table.get(get);
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
}
table.close();
}
//scan 扫描所有 慎用
@Test
public void test02() throws IOException {
Table table = connection.getTable(TableName.valueOf("t2"));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while(iterator.hasNext()){
Result result = iterator.next();
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("=---------------");
}
}
table.close();
}
//scan 指定扫描
@Test
public void test03() throws IOException {
Table table = connection.getTable(TableName.valueOf("t2"));
Scan scan = new Scan();
scan.withStartRow(Bytes.toBytes("1001"));
scan.withStopRow(Bytes.toBytes("1002")); //包左不包右
// scan.setOneRowLimit()
scan.setRaw(true);//开启raw模式 会扫描添加了删除标记的,但未真正删除的
ResultScanner scanner = table.getScanner(scan);
// for (Result result : scanner) {
//
// }
Iterator<Result> iterator = scanner.iterator();
while(iterator.hasNext()){
Result result = iterator.next();
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("---------------");
}
}
table.close();
}
// put Mutation
@Test
public void test04() throws IOException {
Table table = connection.getTable(TableName.valueOf("t2"));
Put put = new Put(Bytes.toBytes("1004")); //可以添加多条
put.addColumn(Bytes.toBytes("cf01"),Bytes.toBytes("phone"),Bytes.toBytes("11111111"));
put.addColumn(Bytes.toBytes("cf01"),Bytes.toBytes("qq"),Bytes.toBytes("12211111"));
table.put(put);
table.close();
}
//append
@Test
public void test05() throws IOException {
Table table = connection.getTable(TableName.valueOf("t2"));
Append append = new Append(Bytes.toBytes("1004")); //指定rowkey
append.add(Bytes.toBytes("cf01"),Bytes.toBytes("phone"),Bytes.toBytes("_11111111"));
Result result = table.append(append);
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
}
//delete
@Test
public void test06() throws IOException {
Table table = connection.getTable(TableName.valueOf("t2"));
Delete delete = new Delete(Bytes.toBytes("1004")); //指定rowKey
// delete.addFamily(Bytes.toBytes("cf01"));//指定cf
delete.addColumn(Bytes.toBytes("cf01"),Bytes.toBytes("phone"));//指定col 和列限定符(qualifier)
table.delete(delete);
table.close();
}
@After
public void close() throws IOException {
connection.close();
}
}
#过滤器的使用
package com.bigdata.table;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
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.Iterator;
import java.util.List;
/**
* @version 1.0
* @name mir zhou
* @date 2020/12/2 14:32
* @function
* @return
*/
public class HBaseDemo04
{
private Connection connection;
@Before
public void init() throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop84:2181,hadoop85:2181,hadoop86:2181");
connection = ConnectionFactory.createConnection(conf);
}
//age > 20
@Test
public void test02() throws IOException {
Table table = connection.getTable(TableName.valueOf("t2"));
Scan scan = new Scan();
SingleColumnValueFilter filter = new SingleColumnValueFilter(
Bytes.toBytes("cf01"),
Bytes.toBytes("age"),
CompareFilter.CompareOp.GREATER,
Bytes.toBytes("20")
);
filter.setFilterIfMissing(true); //如果没有age字段就过滤掉
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
}
}
table.close();
}
//name zh打头 or 含有 g
@Test
public void test03() throws IOException {
Table table = connection.getTable(TableName.valueOf("t2"));
Scan scan = new Scan();
SingleColumnValueFilter filter = new SingleColumnValueFilter(
Bytes.toBytes("cf01"),
Bytes.toBytes("name"),
CompareFilter.CompareOp.EQUAL,
new BinaryPrefixComparator(Bytes.toBytes("zh"))
//new SubstringComparator("g") //含有g
);
filter.setFilterIfMissing(true); //如果没有age字段就过滤掉。
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
}
}
table.close();
}
@After
public void close() throws IOException {
connection.close();
}
}

浙公网安备 33010602011771号