Namespace的API
package org.study;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
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;
public class NamespaceDemo {
Connection connection = null;
Admin admin = null;
@Before
public void connect() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", HbaseConfig.get("hbase.zookeeper.quorum"));
conf.set("hbase.zookeeper.property.clientPort", HbaseConfig.get("hbase.zookeeper.property.clientPort"));
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
}
@After
public void close() throws IOException {
if(null != admin){
admin.close();
}
if(null != connection){
connection.close();
}
}
@Test
public void test() throws IOException {
boolean isExists = admin.tableExists(TableName.valueOf("person"));
System.out.println(isExists);
}
@Test
public void createNamespace() throws IOException {
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("helloworld").build();
admin.createNamespace(namespaceDescriptor);
}
@Test
public void dropNamespace() throws IOException {
admin.deleteNamespace("helloworld");
}
@Test
public void list_namespace() throws IOException {
String[] namespaces = admin.listNamespaces();
for (String namespace : namespaces) {
System.out.println(namespace);
}
}
@Test
public void list_namespace_tables() throws IOException {
TableName[] tableNames = admin.listTableNamesByNamespace("default");
for (TableName tableName : tableNames) {
System.out.println(new String(tableName.getName()));
}
}
}
对table进行CURD的API
package org.study;
import org.apache.commons.lang.time.StopWatch;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class TableDemo {
Connection connection = null;
Admin admin = null;
@Before
public void connect() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", HbaseConfig.get("hbase.zookeeper.quorum"));
conf.set("hbase.zookeeper.property.clientPort", HbaseConfig.get("hbase.zookeeper.property.clientPort"));
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
}
@After
public void close() throws IOException {
if (null != admin) {
admin.close();
}
if (null != connection) {
connection.close();
}
}
@Test
public void generatorData() throws IOException {
if (!admin.tableExists(TableName.valueOf("people"))) {
ColumnFamilyDescriptor columnFamily1 = ColumnFamilyDescriptorBuilder.newBuilder("basic".getBytes(StandardCharsets.UTF_8)).build();
TableDescriptor table = TableDescriptorBuilder.newBuilder(TableName.valueOf("default", "people"))
.setColumnFamily(columnFamily1).build();
admin.createTable(table);
}
Table table = connection.getTable(TableName.valueOf("person"));
List<Put> puts = new ArrayList<>();
long startTime = System.currentTimeMillis();
for (int row = 0; row < 1000000; row++) {
byte[] rowkey = ("row" + row).getBytes(StandardCharsets.UTF_8);
Put put = new Put(rowkey);
byte[] basic = "basic".getBytes(StandardCharsets.UTF_8);
put.addColumn(basic, "password1".getBytes(StandardCharsets.UTF_8), getPassword());
put.addColumn(basic, "password2".getBytes(StandardCharsets.UTF_8), getPassword());
puts.add(put);
if(puts.size() > 1000) {
table.put(puts);
puts.clear();
}
}
long endTime = System.currentTimeMillis();
System.out.println("cost:" + (endTime - startTime));
}
private byte[] getPassword(){
StringBuilder sb = new StringBuilder(6);
for (int i = 0; i < 6; i++) {
char c = (char) (Math.random() * 26 + 65);
sb.append(c);
}
return sb.toString().getBytes(StandardCharsets.UTF_8);
}
@Test
public void createTable() throws IOException {
ColumnFamilyDescriptor columnFamily1 = ColumnFamilyDescriptorBuilder.newBuilder("basic".getBytes(StandardCharsets.UTF_8)).build();
ColumnFamilyDescriptor columnFamily2 = ColumnFamilyDescriptorBuilder.newBuilder("info".getBytes(StandardCharsets.UTF_8)).build();
TableDescriptor table = TableDescriptorBuilder.newBuilder(TableName.valueOf("default", "users"))
.setColumnFamily(columnFamily1).setColumnFamily(columnFamily2).build();
admin.createTable(table);
}
@Test
public void deleteTable() throws IOException {
TableName tableName = TableName.valueOf("users");
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
@Test
public void desc() throws IOException {
TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf("helloworld", "hello"));
System.out.println(descriptor);
}
@Test
public void descAllTables() throws IOException {
List<TableDescriptor> tableDescriptors = admin.listTableDescriptors();
for (int i = 0; i < tableDescriptors.size(); i++) {
TableDescriptor tableDescriptor = tableDescriptors.get(i);
System.out.println(tableDescriptor);
}
}
@Test
public void putData() throws IOException {
byte[] rowkey = "2".getBytes(StandardCharsets.UTF_8);
Put put = new Put(rowkey);
byte[] basic = "basic".getBytes(StandardCharsets.UTF_8);
byte[] info = "info".getBytes(StandardCharsets.UTF_8);
put.addColumn(basic, "name".getBytes(StandardCharsets.UTF_8), "张三".getBytes(StandardCharsets.UTF_8));
put.addColumn(basic, "age".getBytes(StandardCharsets.UTF_8), "10".getBytes(StandardCharsets.UTF_8));
put.addColumn(info, "address".getBytes(StandardCharsets.UTF_8), "guangzhou".getBytes(StandardCharsets.UTF_8));
Table table = connection.getTable(TableName.valueOf("person"));
table.put(put);
}
@Test
public void appendData() throws IOException {
byte[] rowkey = "2".getBytes(StandardCharsets.UTF_8);
Append append = new Append(rowkey);
byte[] basic = "basic".getBytes(StandardCharsets.UTF_8);
byte[] info = "info".getBytes(StandardCharsets.UTF_8);
append.addColumn(basic, "name".getBytes(StandardCharsets.UTF_8), "张三".getBytes(StandardCharsets.UTF_8));
append.addColumn(basic, "age".getBytes(StandardCharsets.UTF_8), "10".getBytes(StandardCharsets.UTF_8));
append.addColumn(info, "address".getBytes(StandardCharsets.UTF_8), "guangzhou".getBytes(StandardCharsets.UTF_8));
Table table = connection.getTable(TableName.valueOf("person"));
table.append(append);
}
@Test
public void deleteRow() throws IOException {
Table table = connection.getTable(TableName.valueOf("person"));
String rowkey = "2";
table.delete(new Delete(rowkey.getBytes(StandardCharsets.UTF_8)));
}
@Test
public void deleteCell() throws IOException {
Table table = connection.getTable(TableName.valueOf("person"));
String rowkey = "2";
Delete delete = new Delete(rowkey.getBytes(StandardCharsets.UTF_8));
delete.addColumn("basic".getBytes(StandardCharsets.UTF_8), "name".getBytes(StandardCharsets.UTF_8));
table.delete(delete);
}
@Test
public void get() throws IOException {
Table table = connection.getTable(TableName.valueOf("person"));
String rowkey = "2";
Result result = table.get(new Get(rowkey.getBytes(StandardCharsets.UTF_8)));
this.extractResult(result);
}
@Test
public void scan() throws IOException {
Table table = connection.getTable(TableName.valueOf("person"));
ResultScanner scanner = table.getScanner(new Scan());
for (Result result : scanner) {
this.extractResult(result);
}
}
@Test
public void count() throws IOException {
Table table = connection.getTable(TableName.valueOf("person"));
Scan scan = new Scan();
scan.setFilter(new KeyOnlyFilter());
StopWatch stopWatch = new StopWatch();
stopWatch.start();
int rowCount = 0;
ResultScanner rs = table.getScanner(scan);
for (Result result : rs) {
rowCount += result.size();
}
stopWatch.stop();
System.out.println("cost: " + stopWatch.getTime());
System.out.println(rowCount);
}
@Test
public void filter1() throws IOException {
Scan scan = new Scan();
scan.setFilter(new ValueFilter(CompareOperator.EQUAL, new RegexStringComparator(".*AAA.*")));
Table table = connection.getTable(TableName.valueOf("person"));
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ResultScanner scanner = table.getScanner(scan);
stopWatch.stop();
System.out.println("cost:" + stopWatch.getTime());
for (Result result : scanner) {
this.extractResult(result);
}
}
@Test
public void filter2() throws IOException {
Scan scan = new Scan();
FilterList filterList = new FilterList();
filterList.addFilter(new SingleColumnValueFilter("basic".getBytes(StandardCharsets.UTF_8), "password1".getBytes(StandardCharsets.UTF_8),
CompareOperator.EQUAL, new RegexStringComparator(".*AAA.*")));
filterList.addFilter(new SingleColumnValueFilter("basic".getBytes(StandardCharsets.UTF_8), "password2".getBytes(StandardCharsets.UTF_8),
CompareOperator.EQUAL, new RegexStringComparator("FOCPZE")));
scan.setFilter(filterList);
Table table = connection.getTable(TableName.valueOf("person"));
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
this.extractResult(result);
}
}
private void extractResult(Result result) throws IOException {
CellScanner cellScanner = result.cellScanner();
while (cellScanner.advance()) {
Cell current = cellScanner.current();
System.out.println("\n" + new String(CellUtil.cloneRow(current)) +
"\t" + new String(CellUtil.cloneFamily(current)) +
"\t" + new String(CellUtil.cloneQualifier(current)) +
"\t" + new String(CellUtil.cloneValue(current)));
}
}
@Test
public void listTableNames() throws IOException {
TableName[] tableNames = admin.listTableNames();
for (TableName tableName : tableNames) {
System.out.println(tableName);
}
}
}