hbase 增删改查 api 简单操作

package com.utils;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
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.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;


public class HBaseTest {
HBaseAdmin admin=null;
Configuration conf=null;
/**
* 构造函数加载配置
*/
public HBaseTest(){
conf = new Configuration();
conf.set("hbase.zookeeper.quorum","192.168.80.20,192.168.80.21,192.168.80.22");
conf.set("hbase.rootdir", "hdfs://cluster/hbase");
conf.set("hbase.master", "192.168.80.20:60010");
try {
admin = new HBaseAdmin(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
HBaseTest hbase = new HBaseTest();
//创建一张表
hbase.createTable("stu","cf");
// //查询所有表名
// hbase.getALLTable();
// //往表中添加一条记录
// hbase.addOneRecord("stu","key1","cf","name","zhangsan");
// hbase.addOneRecord("stu","key1","cf","age","24");
// //查询一条记录
// hbase.getKey("stu","key1");
// //获取表的所有数据
// hbase.getALLData("stu");
// //删除一条记录
// hbase.deleteOneRecord("stu","key1");
// //删除表
// hbase.deleteTable("stu");
//scan过滤器的使用
hbase.getScanData("stu","cf","age");
//rowFilter的使用
//84138413_20130313145955
hbase.getRowFilter("waln_log","^*_201303131400\\d*$");
}


public void getRowFilter(String tableName, String reg) throws Exception {
HTable hTable = new HTable(conf, tableName);
Scan scan = new Scan();
RowFilter rowFilter = new RowFilter(CompareOp.NOT_EQUAL, new RegexStringComparator(reg));
scan.setFilter(rowFilter);
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
System.out.println(new String(result.getRow()));
}
}
public void getScanData(String tableName, String family, String qualifier) throws Exception {
HTable hTable = new HTable(conf, tableName);
Scan scan = new Scan();
scan.addColumn(family.getBytes(), qualifier.getBytes());
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
if(result.raw().length==0){
System.out.println(tableName+" 表数据为空!");
}else{
for (KeyValue kv: result.raw()){
System.out.println(new String(kv.getKey())+"\t"+new String(kv.getValue()));
}
}
}
}
private void deleteTable(String tableName) {
try {
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName+"表删除成功!");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println(tableName+"表删除失败!");
}

}
/**
* 删除一条记录
* @param tableName
* @param rowKey
*/
public void deleteOneRecord(String tableName, String rowKey) {
HTablePool hTablePool = new HTablePool(conf, 1000);
HTableInterface table = hTablePool.getTable(tableName);
Delete delete = new Delete(rowKey.getBytes());
try {
table.delete(delete);
System.out.println(rowKey+"记录删除成功!");
} catch (IOException e) {
e.printStackTrace();
System.out.println(rowKey+"记录删除失败!");
}
}
/**
* 获取表的所有数据
* @param tableName
*/
public void getALLData(String tableName) {
try {
HTable hTable = new HTable(conf, tableName);
Scan scan = new Scan();
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
if(result.raw().length==0){
System.out.println(tableName+" 表数据为空!");
}else{
for (KeyValue kv: result.raw()){
System.out.println(new String(kv.getKey())+"\t"+new String(kv.getValue()));
}
}
}
} catch (IOException e) {
e.printStackTrace();
}

}
/**
* 查询一条记录
* @param tableName
* @param rowKey
*/
public void getKey(String tableName, String rowKey) {
HTablePool hTablePool = new HTablePool(conf, 1000);
HTableInterface table = hTablePool.getTable(tableName);
Get get = new Get(rowKey.getBytes());
try {
Result result = table.get(get);
if (result.raw().length==0) {
System.out.println("查询的关键词"+rowKey+"不存在");
}else {
for (KeyValue kv : result.raw()) {
System.out.println(new String(kv.getKey())+"\t"+new String(kv.getValue()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 往表中添加一条记录
* @param tableName
* @param rowKey
* @param column
* @param qua
* @param value
*/
public void addOneRecord(String tableName, String rowKey, String column,
String qua, String value) {
HTablePool hTablePool = new HTablePool(conf, 1000);
HTableInterface table = hTablePool.getTable(tableName);
Put put = new Put(rowKey.getBytes());
put.add(column.getBytes(), qua.getBytes(), value.getBytes());
try {
table.put(put);
System.out.println("添加记录 "+rowKey+ " 成功!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("添加记录 "+rowKey+ " 失败!");
}
}
/**
* 查询所有表名
* @return
* @throws Exception
*/
public List<String> getALLTable() throws Exception {
ArrayList<String> tables = new ArrayList<String>();
if(admin!=null){
HTableDescriptor[] listTables = admin.listTables();
if (listTables.length>0) {
for (HTableDescriptor tableDesc : listTables) {
tables.add(tableDesc.getNameAsString());
System.out.println(tableDesc.getNameAsString());
}
}
}
return tables;
}
/**
* 创建一张表
* @param tableName
* @param column
* @throws Exception
*/
public void createTable(String tableName, String column) throws Exception {
if(admin.tableExists(tableName)){
System.out.println(tableName+"表已经存在!");
}else{
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(column.getBytes()));
admin.createTable(tableDesc);
System.out.println(tableName+"表创建成功!");
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值