<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.4.6</version>
</dependency>
</dependencies>
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseUtils {
private static final String HBASE_ZOOKEEPER_QUORUM = "hbase.zookeeper.quorum";
private static final String HBASE_ZOOKEEPER_PROPERTY_CLIENT_PORT = "hbase.zookeeper.property.clientPort";
private static final String TABLE_NAME = "table_name";
private static Configuration configuration;
private static Connection connection;
static {
configuration = HBaseConfiguration.create();
configuration.set(HBASE_ZOOKEEPER_QUORUM, "localhost");
configuration.set(HBASE_ZOOKEEPER_PROPERTY_CLIENT_PORT, "2181");
try {
connection = ConnectionFactory.createConnection(configuration);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Table getTable(String tableName) throws IOException {
return connection.getTable(TableName.valueOf(tableName));
}
public static void put(String tableName, String rowKey, String columnFamily, String column, String value)
throws IOException {
Table table = getTable(tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
table.close();
}
public static void delete(String tableName, String rowKey) throws IOException {
Table table = getTable(tableName);
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
table.close();
}
public static Result get(String tableName, String rowKey) throws IOException {
Table table = getTable(tableName);
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
table.close();
return result;
}
public static List<Result> scan(String tableName, String startRowKey, String endRowKey) throws IOException {
Table table = getTable(tableName);
Scan scan = new Scan(Bytes.toBytes(startRowKey), Bytes.toBytes(endRowKey));
ResultScanner scanner = table.getScanner(scan);
List<Result> resultList = new ArrayList<>();
for (Result result : scanner) {
resultList.add(result);
}
table.close();
return resultList;
}
public static String getValue(Result result, String columnFamily, String column) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
if (Bytes.toString(CellUtil.cloneFamily(cell)).equals(columnFamily)
&& Bytes.toString(CellUtil.cloneQualifier(cell)).equals(column)) {
return Bytes.toString(CellUtil.cloneValue(cell));
}
}
return null;
}
}