package com.test.hbase.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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HBaseAPIDemo {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
//设置zk的地址
conf.set("hbase.zookeeper.quorum", Constants.hostNames);
//获取链接hbase数据的链接对象
Connection conn = ConnectionFactory.createConnection(conf);
//过去时接口
// HBaseAdmin admin=new HBaseAdmin(conn);
//获取操作hbase数据库的对象
HBaseAdmin ha = (HBaseAdmin) conn.getAdmin();
// createNamespace(ha);
// listNamespace(ha);
// createTables(ha);
// listNamespaceTables(ha);
// putDataToTables(ha, conn);
// putMultilDataToTables(ha, conn);
// scanTablesData(ha, conn);
// getTablesData(ha, conn);
deleteNsAndTable(ha, conn);
}
public static void createNamespace(HBaseAdmin ha) throws IOException {
ha.createNamespace(NamespaceDescriptor.create("ns2").build());
ha.close();
System.out.println("-----createNamespace----over");
}
public static void listNamespace(HBaseAdmin ha) throws IOException {
NamespaceDescriptor[] listns = ha.listNamespaceDescriptors();
for (NamespaceDescriptor ns : listns) {
System.out.println(ns.getName());
}
ha.close();
System.out.println("-----listNamespace----over");
}
public static void createTables(HBaseAdmin ha) throws IOException {
//先判断表是否存在
if (!ha.tableExists("ns2:stu")) {
HTableDescriptor htable = new HTableDescriptor(TableName.valueOf("ns2:stu"));
//创建一个表至少需要添加一个列族
htable.addFamily(new HColumnDescriptor("cf_info"));
htable.addFamily(new HColumnDescriptor("cf_beizhu"));
ha.createTable(htable);
}
ha.close();
System.out.println("-----createTables----over");
}
public static void listNamespaceTables(HBaseAdmin ha) throws IOException {
HTableDescriptor[] htables = ha.listTableDescriptorsByNamespace("ns2");
for (HTableDescriptor tb : htables) {
System.out.println(tb.getTableName());
}
ha.close();
System.out.println("-----createTables----over");
}
public static void putDataToTables(HBaseAdmin ha, Connection conn) throws IOException {
//判断表是否存在
if (ha.tableExists("ns2:stu")) {
Table mTable = conn.getTable(TableName.valueOf("ns2:stu"));
//创建Put对象并且添加rowkey
Put put = new Put("soft_20170101".getBytes());
//第一个参数是列族,第二个参数是列名,第三个参数是列的值
put.addColumn("cf_info".getBytes(), "name".getBytes(), "laowang".getBytes());
put.addColumn("cf_info".getBytes(), "age".getBytes(), "20".getBytes());
put.addColumn("cf_info".getBytes(), "sex".getBytes(), "nan".getBytes());
put.addColumn("cf_beizhu".getBytes(), "address".getBytes(), "fangshan".getBytes());
mTable.put(put);
}
ha.close();
System.out.println("-----putDataToTables----over");
}
public static void putMultilDataToTables(HBaseAdmin ha, Connection conn) throws IOException {
//判断表是否存在
if (ha.tableExists("ns2:stu")) {
Table mTable = conn.getTable(TableName.valueOf("ns2:stu"));
List<Put> listput = new ArrayList<Put>();
for (int i = 0; i < 100; i++) {
//创建Put对象并且添加rowkey
Put put = new Put(("soft_20170101" + i).getBytes());
//第一个参数是列族,第二个参数是列名,第三个参数是列的值
put.addColumn("cf_info".getBytes(), "name".getBytes(), ("laowang" + i).getBytes());
put.addColumn("cf_info".getBytes(), "age".getBytes(), "20".getBytes());
if (i % 2 == 1) {
put.addColumn("cf_info".getBytes(), "sex".getBytes(), "nv".getBytes());
} else {
put.addColumn("cf_info".getBytes(), "sex".getBytes(), "nan".getBytes());
}
put.addColumn("cf_beizhu".getBytes(), "address".getBytes(), ("fangshan" + i).getBytes());
listput.add(put);
}
//批量插入
mTable.put(listput);
}
ha.close();
System.out.println("-----putMultilDataToTables----over");
}
public static void scanTablesData(HBaseAdmin ha, Connection conn) throws IOException {
//判断表是否存在
if (ha.tableExists("ns2:stu")) {
Table mTable = conn.getTable(TableName.valueOf("ns2:stu"));
//扫描整张表
Scan scan = new Scan();
//scan.addFamily("cf_info".getBytes());
// scan.addColumn("cf_info".getBytes(), "name".getBytes());
ResultScanner rs = mTable.getScanner(scan);
for (Result result : rs) {
System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));
System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));
System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));
System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));
}
}
ha.close();
System.out.println("-----scanTablesData----over");
}
public static void getTablesData(HBaseAdmin ha, Connection conn) throws IOException {
//判断表是否存在
if (ha.tableExists("ns2:stu")) {
Table mTable = conn.getTable(TableName.valueOf("ns2:stu"));
//获取整行数据
Get get = new Get("soft_2017010196".getBytes());
// get.addFamily("cf_info".getBytes());
get.addColumn("cf_info".getBytes(), "name".getBytes());
Result result = mTable.get(get);
System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));
System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));
System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));
System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));
}
ha.close();
System.out.println("-----getTablesData----over");
}
public static void deleteNsAndTable(HBaseAdmin ha, Connection conn) throws IOException {
//遍历命名空间下所有表
HTableDescriptor[] htables = ha.listTableDescriptorsByNamespace("ns1");
for (HTableDescriptor tb : htables) {
System.out.println(tb.getTableName());
if (ha.isTableEnabled(tb.getTableName())) {
ha.disableTable(tb.getTableName());
}
ha.deleteTable(tb.getTableName());
}
ha.deleteNamespace("ns1");
ha.close();
System.out.println("-----deleteNsAndTable----over");
}
}
// HBase 的过滤器操作
public class HbaseAPIFilter {
public static void main(String[] args) throws Exception {
// singleColumnValueFilter();
// qualifierFilter();
familyFilter();
}
public static void singleColumnValueFilter() throws IOException {
Table mTable = TableUtils.getTable(Constants.tableName);
//如果匹配,那么可用获取行数据
//单个列值的匹配:比较基类为BinaryComparator BinaryComparator 匹配完整字节数组
// SingleColumnValueFilter scvf = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "laowang88".getBytes());
SingleColumnValueFilter scvf = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator("laowang88".getBytes()));
//BinaryPrefixComparator 匹配开始的部分字节数组 name的值开始部分为"xiao"的行都过滤出来
SingleColumnValueFilter scvf2 = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator("xiao".getBytes()));
// Only EQUAL or NOT_EQUAL comparisons are valid with this comparator.
// 所以对于RegexStringComparator基类只能用EQUAL或者NOT_EQUAL
SingleColumnValueFilter scvf3 = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^[x].*$"));
// Only EQUAL or NOT_EQUAL tests are valid with this comparator.
// 所以对于SubstringComparator基类只能用EQUAL或者NOT_EQUAL
SingleColumnValueFilter scvf4 = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, new SubstringComparator("bai"));
Scan scan = new Scan();
scan.setFilter(scvf4);
ResultScanner rs = mTable.getScanner(scan);
for (Result result : rs) {
System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));
System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));
System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));
System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));
}
}
public static void qualifierFilter() throws IOException {
Table mTable = TableUtils.getTable(Constants.tableName);
//列名过滤:
//比较基类为BinaryComparator BinaryComparator 匹配完整字节数组
QualifierFilter columnsNameFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator("name".getBytes()));
QualifierFilter columnsNameFilter2 = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator("age".getBytes()));
// Only EQUAL or NOT_EQUAL tests are valid with this comparator.
// 所以对于SubstringComparator基类只能用EQUAL或者NOT_EQUAL
QualifierFilter columnsNameFilter3 = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("a"));
//BinaryPrefixComparator 匹配开始的部分字节数组, (可能用)
// RegexStringComparator, 正则表达式匹配(可能用)
Scan scan = new Scan();
scan.setFilter(columnsNameFilter3);
ResultScanner rs = mTable.getScanner(scan);
for (Result result : rs) {
System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));
System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));
System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));
System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));
}
}
public static void familyFilter() throws IOException {
Table mTable = TableUtils.getTable(Constants.tableName);
//因为是根据列族的名称过滤,所以传入列族名称
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator("cf_i".getBytes()));
// Only EQUAL or NOT_EQUAL tests are valid with this comparator.
// 所以对于RegexStringComparator基类只能用EQUAL或者NOT_EQUAL
FamilyFilter familyFilter2 = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^[a-z_]{3}[^b].+$"));
// BinaryComparator 匹配完整字节数组, (可能用)
// SubstringComparator 比配子串、大小写不敏感(可能用)
Scan scan = new Scan();
scan.setFilter(familyFilter2);
ResultScanner rs = mTable.getScanner(scan);
for (Result result : rs) {
System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));
System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));
System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));
System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));
}
}
}
注意: 前提是保证集群正常运行和HBase集群正常工作。