直接上代码吧,API比较简单
```
package com.sdnware.start04.hbase;
import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* HBase CURD例子
* @author chenb.bob
* 2017年5月10日
*
*/
public class HBaseDMLExample {
private static Logger LOG = LoggerFactory.getLogger(HBaseDMLExample.class);
private static Configuration conf = null;
static {
System.setProperty("hadoop.home.dir", "E:/soft/hadoop-2.6.0");
conf = HBaseConfiguration.create();
}
/**
* put方法测试
* desc:
* author:chen.bob
* time:2017年5月10日 上午11:23:08
* @return
*/
public static boolean putTo(){
Connection connection = null;
Table table = null;
try {
//Old
//HTable table = new HTable(conf, "sdnware:mytest");
connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("sdnware:mytest"));
byte[] row = Bytes.toBytes("10002");
Put put = new Put(row);
byte[] family = Bytes.toBytes("c1");
byte[] qualifier = Bytes.toBytes("name");
byte[] value = Bytes.toBytes("cb");
put.addColumn(family, qualifier, value);
put.addColumn(Bytes.toBytes("c1"), Bytes.toBytes("age"), Bytes.toBytes("30"));
put.addColumn(Bytes.toBytes("c2"), Bytes.toBytes("name"), Bytes.toBytes("nxj"));
put.addColumn(Bytes.toBytes("c2"), Bytes.toBytes("age"), Bytes.toBytes("25"));
put.addColumn(Bytes.toBytes("c2"), Bytes.toBytes("city"), Bytes.toBytes("beijing"));
table.put(put);
LOG.error("数据存储成功!");
} catch (IOException e) {
LOG.error("数据存储失败!",e);
}finally{
if(null != table){
try {
table.close();
} catch (IOException e) {
}
}
if(null != connection){
try {
connection.close();
} catch (IOException e) {
}
}
}
return Boolean.FALSE;
}
/**
*
* desc:更新指定的列
* author:chen.bob
* time:2017年5月10日 下午3:03:32
* @return
*/
public static boolean putTo2(){
Connection connection = null;
Table table = null;
try {
//Old
//HTable table = new HTable(conf, "sdnware:mytest");
connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("sdnware:mytest"));
byte[] row = Bytes.toBytes("10002");
Put put = new Put(row);
put.addColumn(Bytes.toBytes("c1"), Bytes.toBytes("city"), Bytes.toBytes("changzhou"));
put.addColumn(Bytes.toBytes("c1"), Bytes.toBytes("post"), Bytes.toBytes("0519"));
//多个。。。。。
table.put(put);
LOG.error("数据存储成功!");
} catch (IOException e) {
LOG.error("数据存储失败!",e);
}finally{
if(null != table){
try {
table.close();
} catch (IOException e) {
}
}
if(null != connection){
try {
connection.close();
} catch (IOException e) {
}
}
}
return Boolean.FALSE;
}
/**
*
* desc:get
* author:chen.bob
* time:2017年5月10日 上午11:23:25
* @return
*/
public static boolean get(){
Connection connection = null;
Table table = null;
try {
connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("sdnware:mytest"));
byte[] row = Bytes.toBytes("10001");
Get get = new Get(row);
Result result = table.get(get);
byte[] colfam = Bytes.toBytes("c1");
byte[] col = Bytes.toBytes("name");
/* 方式1 :
byte[] value = result.getValue(colfam, col);
LOG.info("取值================:"+Bytes.toString(value));
*/
// 方式 2 :
List<Cell> columnCells = result.getColumnCells(colfam, col);
for(Cell cell:columnCells){
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
LOG.info("取值================:"+value);
}
return Boolean.TRUE;
}catch (IOException e) {
LOG.error("数据取值失败!",e);
}finally{
if(null != table){
try {
table.close();
} catch (IOException e) {
}
}
if(null != connection){
try {
connection.close();
} catch (IOException e) {
}
}
}
return Boolean.FALSE;
}
/**
*
* desc:get
* author:chen.bob
* time:2017年5月10日 上午11:23:35
* @return
*/
public static boolean get2(){
Connection connection = null;
Table table = null;
try {
connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("sdnware:mytest"));
byte[] row = Bytes.toBytes("10001");
Get get = new Get(row);
Result result = table.get(get);
List<Cell> listCells = result.listCells();
LOG.info("开始遍历");
for(Cell cell:listCells){
String family = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
String qualifier = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
LOG.info("family:"+family+",qualifier:"+qualifier+",value:"+value);
}
return Boolean.TRUE;
}catch (IOException e) {
LOG.error("数据取值失败!",e);
}finally{
if(null != table){
try {
table.close();
} catch (IOException e) {
}
}
if(null != connection){
try {
connection.close();
} catch (IOException e) {
}
}
}
return Boolean.FALSE;
}
/**
*
* desc:查询单个列
* author:chen.bob
* time:2017年5月10日 下午3:01:20
* @return
*/
public static boolean get3(){
Connection connection = null;
Table table = null;
try {
connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("sdnware:mytest"));
byte[] row = Bytes.toBytes("10001");
Get get = new Get(row);
get.addColumn(Bytes.toBytes("c1"), Bytes.toBytes("name")); // 获取指定列族和列修饰符对应的列
Result result = table.get(get);
List<Cell> listCells = result.listCells();
LOG.info("开始遍历");
for(Cell cell:listCells){
String family = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
String qualifier = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
LOG.info("family:"+family+",qualifier:"+qualifier+",value:"+value);
}
return Boolean.TRUE;
}catch (IOException e) {
LOG.error("数据取值失败!",e);
}finally{
if(null != table){
try {
table.close();
} catch (IOException e) {
}
}
if(null != connection){
try {
connection.close();
} catch (IOException e) {
}
}
}
return Boolean.FALSE;
}
/**
*
* desc:指定rowKey
* author:chen.bob
* time:2017年5月10日 下午1:38:12
* @return
*/
public static boolean scan(){
Connection connection = null;
Table table = null;
try {
connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("sdnware:mytest"));
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("10002"));//指定rowKey
ResultScanner rs = table.getScanner(scan);
LOG.info("开始遍历");
for (Result r : rs) {
List<Cell> listCells = r.listCells();
for(Cell cell:listCells){
String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
String family = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
String qualifier = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
LOG.info("row:"+row+",family:"+family+",qualifier:"+qualifier+",value:"+value);
}
}
return Boolean.TRUE;
}catch (IOException e) {
LOG.error("数据取值失败!",e);
}finally{
if(null != table){
try {
table.close();
} catch (IOException e) {
}
}
if(null != connection){
try {
connection.close();
} catch (IOException e) {
}
}
}
return Boolean.FALSE;
}
/**
*
* desc:指定rowKey前缀
* author:chen.bob
* time:2017年5月10日 下午1:38:12
* @return
*/
public static boolean scanByRowKeyPrefix(){
Connection connection = null;
Table table = null;
try {
connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("sdnware:mytest"));
Scan scan = new Scan();
scan.setFilter(new PrefixFilter("1000".getBytes())); //指定rowKey前缀
ResultScanner rs = table.getScanner(scan);
LOG.info("开始遍历");
for (Result r : rs) {
List<Cell> listCells = r.listCells();
for(Cell cell:listCells){
String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
String family = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
String qualifier = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
LOG.info("row:"+row+",family:"+family+",qualifier:"+qualifier+",value:"+value);
}
}
return Boolean.TRUE;
}catch (IOException e) {
LOG.error("数据取值失败!",e);
}finally{
if(null != table){
try {
table.close();
} catch (IOException e) {
}
}
if(null != connection){
try {
connection.close();
} catch (IOException e) {
}
}
}
return Boolean.FALSE;
}
/**
*
* desc:使用过滤
* author:chen.bob
* time:2017年5月10日 下午1:53:29
* @return
*/
public static boolean scanByFilter(){
Connection connection = null;
Table table = null;
try {
connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("sdnware:mytest"));
//过滤器
//1、FilterList代表一个过滤器列表
//FilterList.Operator.MUST_PASS_ALL -->and
//FilterList.Operator.MUST_PASS_ONE -->or
//eg、FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE);
//2、SingleColumnValueFilter
//3、ColumnPrefixFilter用于指定列名前缀值相等
//4、MultipleColumnPrefixFilter和ColumnPrefixFilter行为差不多,但可以指定多个前缀。
//5、QualifierFilter是基于列名的过滤器。
//6、RowFilter
//7、RegexStringComparator是支持正则表达式的比较器。
//8、SubstringComparator用于检测一个子串是否存在于值中,大小写不敏感。
// 使用SingleColumnValueFilter会影响查询性能,在真正处理海量数据时会消耗很大的资源,且需要较长的时间
// 过滤 c2:city = beijing
Filter filter = new SingleColumnValueFilter(
Bytes.toBytes("c2"), Bytes.toBytes("city"), CompareOp.EQUAL,
Bytes.toBytes("beijing"));
FilterList filterList=new FilterList();
filterList.addFilter(filter);
Scan scan = new Scan();
//scan.setFilter(filter); 单个filter
scan.setFilter(filterList); //多个filter组合
// scan.addColumn(Bytes.toBytes("c2"), Bytes.toBytes("city"));
scan.addFamily(Bytes.toBytes("c2"));
/**
* 注意此处,必须有定义,将查询的列簇显示加入,笔者测试发现,没有的话无法起到过滤效果
* scan.addColumn 只会查询定位到当前 qualifer
* scan.addFamily 会查询row
*
*/
ResultScanner rs = table.getScanner(scan);
LOG.info("ResultScanner:开始遍历");
for (Result r : rs) {
List<Cell> listCells = r.listCells();
for(Cell cell:listCells){
String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
String family = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
String qualifier = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
LOG.info("row:"+row+",family:"+family+",qualifier:"+qualifier+",value:"+value);
}
}
return Boolean.TRUE;
}catch (IOException e) {
LOG.error("数据取值失败!",e);
}finally{
if(null != table){
try {
table.close();
} catch (IOException e) {
}
}
if(null != connection){
try {
connection.close();
} catch (IOException e) {
}
}
}
return Boolean.FALSE;
}
/**
*
* desc:删除列
* author:chen.bob
* time:2017年5月10日 下午3:23:33
* @return
*/
public static boolean deleteQualifier(){
Connection connection = null;
Table table = null;
try {
connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("sdnware:mytest"));
byte[] row = Bytes.toBytes("10002");
Delete deleteColumn = new Delete(row);
deleteColumn.addColumn(Bytes.toBytes("c1"), Bytes.toBytes("post"));
table.delete(deleteColumn);
return Boolean.TRUE;
}catch (IOException e) {
LOG.error("数据取值失败!",e);
}finally{
if(null != table){
try {
table.close();
} catch (IOException e) {
}
}
if(null != connection){
try {
connection.close();
} catch (IOException e) {
}
}
}
return Boolean.FALSE;
}
/**
*
* desc:删除列簇所有列
* author:chen.bob
* time:2017年5月10日 下午3:36:59
* @return
*/
public static boolean deleteFamilyQualifier(){
Connection connection = null;
Table table = null;
try {
connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("sdnware:mytest"));
byte[] row = Bytes.toBytes("10002");
Delete deleteColumn = new Delete(row);
deleteColumn.addFamily(Bytes.toBytes("c1"));
table.delete(deleteColumn);
return Boolean.TRUE;
}catch (IOException e) {
LOG.error("数据取值失败!",e);
}finally{
if(null != table){
try {
table.close();
} catch (IOException e) {
}
}
if(null != connection){
try {
connection.close();
} catch (IOException e) {
}
}
}
return Boolean.FALSE;
}
/**
*
* desc:删除行
* author:chen.bob
* time:2017年5月10日 下午3:36:59
* @return
*/
public static boolean deleteRow(){
Connection connection = null;
Table table = null;
try {
connection = ConnectionFactory.createConnection(conf);
table = connection.getTable(TableName.valueOf("sdnware:mytest"));
byte[] row = Bytes.toBytes("10002");
Delete deleteColumn = new Delete(row);
table.delete(deleteColumn);
return Boolean.TRUE;
}catch (IOException e) {
LOG.error("数据取值失败!",e);
}finally{
if(null != table){
try {
table.close();
} catch (IOException e) {
}
}
if(null != connection){
try {
connection.close();
} catch (IOException e) {
}
}
}
return Boolean.FALSE;
}
public static void main(String[] args) {
//putTo2();
deleteRow();
}
}
在查询中还有一种方式是索引,可以参考http://www.cnblogs.com/kxdblog/p/4328699.html