HBase Java API 01: 기본 동작
1. HBaseUtil.java
import java.io.IOException;
import java.util.Date;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.util.Bytes;
import com.aura.hbase.utils.HBasePrintUtil;
/**
* HBase CRUD
*/
public class HbaseUtil {
private static final String ZOOKEEPER_LIST = "node01:2181,node02:2181,node03:2181";
private static Configuration conf = HBaseConfiguration.create();
private static Connection conn;
private static HBaseAdmin admin;
private static HTable table;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", ZOOKEEPER_LIST);
try {
conn = ConnectionFactory.createConnection(conf);
admin = (HBaseAdmin) conn.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static HTable getTable(String tableName) throws Exception {
return (HTable) conn.getTable(TableName.valueOf(tableName));
}
public static boolean isTableExist(String tableName) throws Exception {
return admin.tableExists(TableName.valueOf(tableName));
}
public static void disableTable(String tableName) throws Exception {
admin.disableTable(TableName.valueOf(tableName));
}
public static void enableTable(String tableName) throws Exception {
admin.enableTable(TableName.valueOf(tableName));
}
public static boolean isTableDisabled(String tableName) throws Exception {
return admin.isTableDisabled(TableName.valueOf(tableName));
}
public static boolean isTableEnabled(String tableName) throws Exception {
return admin.isTableEnabled(TableName.valueOf(tableName));
}
public static void getAllTables() throws Exception {
//
/*
TableName[] tables = admin.listTableNames();
for(TableName table : tables) {
System.out.println(table.getNameAsString());
}
*/
//
HTableDescriptor[] descs = admin.listTables();
for(HTableDescriptor desc : descs) {
System.out.println(desc.getNameAsString());
}
}
public static void createTable(String tableName, String[] family) throws Exception {
if(isTableExist(tableName)) {
dropTable(tableName);
}
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
if(family != null && family.length > 0) {
for(String cf :family) {
HColumnDescriptor hcd = new HColumnDescriptor(cf);
desc.addFamily(hcd);
}
}
admin.createTable(desc);
}
public static void descTable(String tableName) throws Exception {
table = getTable(tableName);
HTableDescriptor desc = table.getTableDescriptor();
HColumnDescriptor[] columnDescs = desc.getColumnFamilies();
HBasePrintUtil.printHColumnDescriptors(columnDescs);
}
public static void dropTable(String tableName) throws Exception {
if(isTableDisabled(tableName)) {
admin.deleteTable(TableName.valueOf(tableName));
}else {
disableTable(tableName);
admin.deleteTable(TableName.valueOf(tableName));
}
}
/*
* ,
* ...
*/
public void modifyTable(String tableName, String[] addColumn, String[] removeColumn) throws Exception {
/*
table = getTable(tableName);
HTableDescriptor newDesc = new HTableDescriptor(TableName.valueOf(tableName));
HTableDescriptor oldDesc = table.getTableDescriptor();
HColumnDescriptor[] columnFamilies = oldDesc.getColumnFamilies();
for(String column : addColumn) {
newDesc.addFamily(new HColumnDescriptor(column));
}
for(String column : removeColumn) {
newDesc.removeFamily(Bytes.toBytes(column));
}
admin.modifyTable(tableName, newDesc);
*/
}
public static void putData(String tableName, String rowKey, String familyName, String columnName, String value)
throws Exception {
putData(tableName, rowKey, familyName, columnName, value, new Date().getTime());
}
public static void putData(String tableName, String rowKey, String familyName, String columnName, String value,
long timestamp) throws Exception {
table = getTable(tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), timestamp, Bytes.toBytes(value));
table.put(put);
}
public static void putData(String tableName, Put put) throws Exception {
table = getTable(tableName);
table.put(put);
}
public static void putData(String tableName, List putList) throws Exception {
table = getTable(tableName);
table.put(putList);
}
public static Result getResult(String tableName, String rowKey) throws Exception {
table = getTable(tableName);
Get get = new Get(Bytes.toBytes(rowKey));
return table.get(get);
}
public static Result getResult(String tableName, String rowKey, String familyName) throws Exception {
table = getTable(tableName);
Get get = new Get(Bytes.toBytes(rowKey));
get.addFamily(Bytes.toBytes(familyName));
return table.get(get);
}
public static Result getResult(String tableName, String rowKey, String familyName, String columnName) throws Exception {
table = getTable(tableName);
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
return table.get(get);
}
public static Result getResultByVersion(String tableName, String rowKey, String familyName, String columnName,
int versions) throws Exception {
table = getTable(tableName);
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
get.setMaxVersions(versions);
return table.get(get);
}
public static ResultScanner getResultScann(String tableName) throws Exception {
table = getTable(tableName);
Scan scan = new Scan();
return table.getScanner(scan);
}
public static void deleteColumn(String tableName, String rowKey) throws Exception {
table = getTable(tableName);
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
}
public static void deleteColumn(String tableName, String rowKey, String falilyName) throws Exception {
table = getTable(tableName);
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addFamily(Bytes.toBytes(falilyName));
table.delete(delete);
}
public static void deleteColumn(String tableName, String rowKey, String falilyName, String columnName) throws Exception {
table = getTable(tableName);
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumn(Bytes.toBytes(falilyName), Bytes.toBytes(columnName));
table.delete(delete);
}
}
2. HBasePrintUtil.java
package com.aura.hbase.utils;
import java.util.List;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.KeyValueUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.util.Bytes;
public class HBasePrintUtil {
public static void printResultScanner(ResultScanner resultScann) {
for (Result result : resultScann) {
printResult(result);
}
}
public static void printResult(Result result) {
List cells = result.listCells();
for(Cell cell : cells) {
printCell(cell);
}
}
public static void printCell(Cell cell) {
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell)) + "\t" +
Bytes.toString(CellUtil.cloneFamily(cell)) + "\t" +
Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t" +
cell.getTimestamp() + "\t" +
Bytes.toString(CellUtil.cloneValue(cell)));
}
public static void printHTableDescriptor(HTableDescriptor desc) {
System.out.println(
"Name:" + desc.getNameAsString() + " " +
"FlushPolicyClassName:" + desc.getFlushPolicyClassName() + " " +
"MaxFileSize:" + desc.getMaxFileSize() + " " +
"MemStoreFlushSize:" + desc.getMemStoreFlushSize() + " " +
"RegionReplication:" + desc.getRegionReplication() + " " +
"RegionSplitPolicyClassName:" + desc.getRegionSplitPolicyClassName() + " " +
"---------------------------------------------");
}
public static void printHColumnDescriptors(HColumnDescriptor[] columnDescs) {
for(HColumnDescriptor desc : columnDescs) {
printHColumnDescriptor(desc);
}
}
public static void printHColumnDescriptor(HColumnDescriptor desc) {
System.out.println(
"Name : " + desc.getNameAsString() + " " +
"BloomFilterType : " + desc.getBloomFilterType() + " " +
"MinVersions : " + desc.getMinVersions() + " " +
"MaxVersions : " + desc.getMaxVersions() + " " +
"InMemory : " + desc.isInMemory() + " " +
"DataBlockEncoding : " + desc.getDataBlockEncoding() + " " +
"TimeToLive : " + desc.getTimeToLive() + " " +
"Compression : " + desc.getCompression() + " " +
"BlockCacheEnabled : " + desc.isBlockCacheEnabled() + " " +
"Blocksize : " + desc.getBlocksize() + " " +
"---------------------------------------------");
}
}
|
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.