HBase常用的JAVA API操作
程序员文章站
2022-04-21 18:05:52
为了方便以后查看,总结了一些常用的java操作hbase的代码: ......
为了方便以后查看,总结了一些常用的java操作hbase的代码:
package com.mcq;
import static org.hamcrest.corematchers.describedas;
import static org.hamcrest.corematchers.nullvalue;
import java.io.ioexception;
import java.io.pushbackinputstream;
import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.hbase.cell;
import org.apache.hadoop.hbase.cellscannable;
import org.apache.hadoop.hbase.cellutil;
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.admin;
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.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.client.table;
import org.apache.hadoop.hbase.util.bytes;
public class testhbase {
private static admin admin = null;
private static connection connection = null;
private static configuration conf = null;
static {
// hbase配置文件
conf = hbaseconfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.1.103");
// 获取连接对象
try {
connection = connectionfactory.createconnection(conf);
// 获取hbase管理员对象
admin = connection.getadmin();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
private static void close(connection conn, admin admin) throws ioexception {
if (conn != null) {
conn.close();
}
if (admin != null) {
admin.close();
}
}
// 判断表是否存在
public static boolean tableexist(string tablename) throws ioexception {
boolean tableexists = admin.tableexists(tablename.valueof(tablename));
return tableexists;
}
// 创建表
public static void createtable(string tablename, string... cfs) throws ioexception {
if (tableexist(tablename)) {
system.out.println("表已存在");
return;
}
// cfs是列族,官方建议一个表一个,但可以有多个
// 创建表描述器
htabledescriptor htabledescriptor = new htabledescriptor(tablename.valueof(tablename));
for (string cf : cfs) {
// 创建列描述器
hcolumndescriptor hcolumndescriptor = new hcolumndescriptor(cf);
// hcolumndescriptor.setmaxversions(3);//设置版本数
htabledescriptor.addfamily(hcolumndescriptor);
}
// 创建表操作
admin.createtable(htabledescriptor);
}
// 删除表
public static void deletetable(string tablename) throws ioexception {
if (!tableexist(tablename)) {
system.out.println("表不存在");
return;
}
// 使表不可用(下线)
admin.disabletable(tablename.valueof(tablename));
// 执行删除操作
admin.deletetable(tablename.valueof(tablename));
system.out.println("表已删除");
}
// 增、改
public static void putdata(string tablename, string rowkey, string cf, string cn, string value) throws ioexception {
// 获取表对象
// htable table=new htable(conf,tablename.valueof(tablename)); 已过时
table table = connection.gettable(tablename.valueof(tablename));
// 创建put对象
put put = new put(bytes.tobytes(rowkey));
// 添加数据
put.addcolumn(bytes.tobytes(cf), bytes.tobytes(cn), bytes.tobytes(value));
// 执行添加操作
table.put(put);
}
// 删
public static void delete(string tablename, string rowkey, string cf, string cn) throws ioexception {
// 获取table对象
table table = connection.gettable(tablename.valueof(tablename));
// 创建delete对象
delete delete = new delete(bytes.tobytes(rowkey));// 删除整个列族
delete.addcolumns(bytes.tobytes(cf), bytes.tobytes(cn));// 删除所有版本
// delete.addcolumn(bytes.tobytes(cf), bytes.tobytes(cn));不推荐,只删除最新的版本
// 执行删除操作
table.delete(delete);
table.close();
}
// 查——全表扫描,只能获取最新版本
public static void scantable(string tablename) throws ioexception {
// 获取table对象
table table = connection.gettable(tablename.valueof(tablename));
// 构建扫描器
scan scan = new scan();
resultscanner resultscanner = table.getscanner(scan);
// 遍历数据并打印
for (result result : resultscanner) { // rowkey
cell[] cells = result.rawcells();
for (cell cell : cells) { // cell
system.out.println("rk:" + bytes.tostring(cellutil.clonerow(cell)) + ",cf:"
+ bytes.tostring(cellutil.clonefamily(cell)) + ",cn:"
+ bytes.tostring(cellutil.clonequalifier(cell)) + ",value:"
+ bytes.tostring(cellutil.clonevalue(cell)));
}
}
table.close();
}
// 查——获取指定列族
public static void getdata(string tablename, string rowkey, string cf, string cn) throws ioexception {
table table = connection.gettable(tablename.valueof(tablename));
get get = new get(bytes.tobytes(rowkey));
get.addcolumn(bytes.tobytes(cf), bytes.tobytes(cn));
// get.addfamily(cf);
// get.setmaxversions();//不传参默认是表结构内的maxversions
get.setmaxversions(2);
result result = table.get(get);
cell[] cells = result.rawcells();
for (cell cell : cells) { // cell
system.out.println("rk:" + bytes.tostring(cellutil.clonerow(cell)) + ",cf:"
+ bytes.tostring(cellutil.clonefamily(cell)) + ",cn:"
+ bytes.tostring(cellutil.clonequalifier(cell)) + ",value:"
+ bytes.tostring(cellutil.clonevalue(cell)));
}
}
public static void main(string[] args) throws ioexception {
// 判断表是否存在
// system.out.println(tableexist("student"));
// system.out.println(tableexist("staff"));
// 创建表
// createtable("staff", "info");
// system.out.println(tableexist("staff"));
// 删除表
// deletetable("staff");
// 增、改
// putdata("student", "1001", "info", "name", "mcq");
// 删
// delete("student", "1001", "info", "name");
// 查——全表扫描
// scantable("student");
//查——获取指定列族
getdata("student", "1001","info","name");
// 关闭资源
close(connection, admin);
}
}