1 环境
1.1 依赖
<dependencies>
<dependency>
<groupId>org.apache.hbasegroupId>
<artifactId>hbase-clientartifactId>
<version>2.4.11version>
dependency>
dependencies>
1.2 配置文件hbase-site.xml
<configuration>
<property>
<name>hbase.zookeeper.quorumname>
<value>hadoop102:2181,hadoop103:2181,hadoop104:2181value>
property>
configuration>
2 创建连接
package com.hbase;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
public class HBaseThreadConnection {
public static Connection connection = null;
static {
try {
connection = ConnectionFactory.createConnection();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void closeConnection() throws IOException {
if (connection != null) {
connection.close();
}
}
public static void main(String[] args) throws IOException {
Connection connection1 = HBaseThreadConnection.connection;
System.out.println(connection1);
HBaseThreadConnection.closeConnection();
}
}
3 DDL API
package com.hbase;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBaseDDL {
public static Connection connection = HBaseThreadConnection.connection;
public static void createNamespace(String namespace) throws IOException {
Admin admin = connection.getAdmin();
NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(namespace);
builder.addConfiguration("user", "gaohechao");
try {
admin.createNamespace(builder.build());
} catch (IOException e) {
System.out.println("Namespace already exists");
e.printStackTrace();
}
admin.close();
}
public static boolean isTableExists(String namespace, String tableName) throws IOException {
Admin admin = connection.getAdmin();
boolean b = false;
try {
b = admin.tableExists(TableName.valueOf(namespace, tableName));
} catch (IOException e) {
e.printStackTrace();
}
admin.close();
return b;
}
public static void createTable(String namespace, String tableName, String... columnFamilies) throws IOException {
if (columnFamilies.length == 0) {
System.out.println("列族数量不能为0");
return;
}
if (isTableExists(namespace, tableName)) {
System.out.println("表已存在");
return;
}
Admin admin = connection.getAdmin();
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(namespace, tableName));
for (String columnFamily : columnFamilies) {
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily));
columnFamilyDescriptorBuilder.setMaxVersions(6);
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
}
try {
admin.createTable(tableDescriptorBuilder.build());
} catch (IOException e) {
e.printStackTrace();
}
admin.close();
}
public static void modifyTable(String namespace, String tableName, String columnFamily, int version) throws IOException {
if (!isTableExists(namespace, tableName)) {
System.out.println("表不存在");
return;
}
Admin admin = connection.getAdmin();
TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf(namespace, tableName));
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(descriptor);
ColumnFamilyDescriptor columnFamilyOld = descriptor.getColumnFamily(Bytes.toBytes(columnFamily));
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(columnFamilyOld);
columnFamilyDescriptorBuilder.setMaxVersions(version);
tableDescriptorBuilder.modifyColumnFamily(columnFamilyDescriptorBuilder.build());
try {
admin.modifyTable(tableDescriptorBuilder.build());
} catch (IOException e) {
e.printStackTrace();
}
admin.close();
}
public static boolean deleteTable(String namespace, String tableName) throws IOException {
if (!isTableExists(namespace, tableName)) {
System.out.println("表不存在");
return false;
}
Admin admin = connection.getAdmin();
try {
TableName tableName1 = TableName.valueOf(namespace, tableName);
admin.disableTable(tableName1);
admin.deleteTable(tableName1);
} catch (IOException e) {
e.printStackTrace();
}
admin.close();
return true;
}
public static void main(String[] args) throws IOException {
connection.close();
}
}
4 DML API
package com.hbase;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBaseDML {
public static Connection connection = HBaseThreadConnection.connection;
public static void putCell(String namespace, String tableName, String rowKey, String columnFamily, String columnName, String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName), Bytes.toBytes(value));
try {
table.put(put);
} catch (IOException e) {
throw new RuntimeException(e);
}
table.close();
}
public static void getCells(String namespace, String tableName, String rowKey, String columnFamily, String columnName) throws IOException {
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName));
get.readAllVersions();
try {
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
byte[] bytes = CellUtil.cloneValue(cell);
String s = new String(bytes);
System.out.println(s);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
table.close();
}
public static void scanRows(String namespace, String tableName, String startRow, String stopRow) throws IOException {
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
Scan scan = new Scan();
scan.withStartRow(Bytes.toBytes(startRow));
scan.withStopRow(Bytes.toBytes(stopRow));
try {
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("row:" + new String(CellUtil.cloneRow(cell)) + "," +
"family:" + new String(CellUtil.cloneFamily(cell)) + "," +
"qualifier:" + new String(CellUtil.cloneQualifier(cell)) + "," +
"value:" + new String(CellUtil.cloneValue(cell)) + "\t");
}
System.out.println("---------------------");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
table.close();
}
public static void filterScan(String namespace, String tableName, String startRow, String stopRow, String columnFamily, String columnName, String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
Scan scan = new Scan();
scan.withStartRow(Bytes.toBytes(startRow));
scan.withStopRow(Bytes.toBytes(stopRow));
FilterList filterList = new FilterList();
try {
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("row:" + new String(CellUtil.cloneRow(cell)) + "," +
"family:" + new String(CellUtil.cloneFamily(cell)) + "," +
"qualifier:" + new String(CellUtil.cloneQualifier(cell)) + "," +
"value:" + new String(CellUtil.cloneValue(cell)) + "\t");
}
System.out.println("---------------------");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
table.close();
}
public static void deleteColumn(String namespace, String tableName, String rowKey, String columnFamily, String columnName) throws IOException {
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumns(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName));
try {
table.delete(delete);
} catch (IOException e) {
throw new RuntimeException(e);
}
table.close();
}
public static void main(String[] args) throws IOException {
connection.close();
}
}