JAVA中连接HBASE的常用API

大家:

   好! java中连接hbase的常用api

package cn.bsr_hbase;
import java.nio.ByteBuffer;
import java.util.ArrayList;


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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
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.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.CompareFilter;
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.FilterList.Operator;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.thrift2.generated.THBaseService.Processor.put;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class HbaseTest {


/**
* 配置ss
*/
static Configuration config = null;
private Connection connection = null;
private Table table = null;


@Before
public void init() throws Exception {
config = HBaseConfiguration.create();// 配置
config.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");// zookeeper地址
config.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
connection = ConnectionFactory.createConnection(config);
table = connection.getTable(TableName.valueOf("user"));
}


/**
* 创建一个表
* 
* @throws Exception
*/
@Test
public void createTable() throws Exception {
// 创建表管理类
HBaseAdmin admin = new HBaseAdmin(config); // hbase表管理
// 创建表描述类
TableName tableName = TableName.valueOf("test"); // 表名称
HTableDescriptor desc = new HTableDescriptor(tableName);
// 创建列族的描述类
HColumnDescriptor family = new HColumnDescriptor("info"); // 列族
// 将列族添加到表中
desc.addFamily(family);
HColumnDescriptor family2 = new HColumnDescriptor("info2"); // 列族
// 将列族添加到表中
desc.addFamily(family2);
// 创建表
admin.createTable(desc); // 创建表
}


@Test
@SuppressWarnings("deprecation")
public void deleteTable() throws MasterNotRunningException,
ZooKeeperConnectionException, Exception {
HBaseAdmin admin = new HBaseAdmin(config);
admin.disableTable("");
admin.deleteTable("test");
admin.close();
}


/**
* 向hbase中增加数据
* 
* @throws Exception
*/
@SuppressWarnings({ "deprecation", "resource" })
@Test
public void insertData() throws Exception {
//创建数据封装类
//Put put = new Put(Bytes.toBytes("mc_123"));
//put.add(Bytes.toBytes("info1"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
//put.add(Bytes.toBytes("info1"), Bytes.toBytes("sex"), Bytes.toBytes("nan"));
//put.add(Bytes.toBytes("info1"), Bytes.toBytes("age"), Bytes.toBytes(18));
//put.add(Bytes.toBytes("info1"), Bytes.toBytes("addr"), Bytes.toBytes("beijing"));

//添加数据
//table.put(put);
ArrayList<Put> list = new ArrayList<Put>();
Put put1 = new Put(Bytes.toBytes("mc_1234"));
put1.add(Bytes.toBytes("info1"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
put1.add(Bytes.toBytes("info1"), Bytes.toBytes("sex"), Bytes.toBytes("nan"));
put1.add(Bytes.toBytes("info1"), Bytes.toBytes("age"), Bytes.toBytes(18));
put1.add(Bytes.toBytes("info1"), Bytes.toBytes("addr"), Bytes.toBytes("beijing"));

Put put2 = new Put(Bytes.toBytes("mc_123"));
put2.add(Bytes.toBytes("info1"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
put2.add(Bytes.toBytes("info1"), Bytes.toBytes("sex"), Bytes.toBytes("nan"));
put2.add(Bytes.toBytes("info1"), Bytes.toBytes("age"), Bytes.toBytes(19));
put2.add(Bytes.toBytes("info1"), Bytes.toBytes("addr"), Bytes.toBytes("beijing"));
list.add(put2);
list.add(put1);
//添加数据
table.put(list);

}





/**
* 删除数据
* 
* @throws Exception
*/
@Test
public void deleteDate() throws Exception {
//创建删除的rowkey
Delete delete = new Delete(Bytes.toBytes("mc_123"));
delete.addColumn(Bytes.toBytes("info1"), Bytes.toBytes("name"));

table.delete(delete);

}


/**
* 单条查询
* 
* @throws Exception
*/
@Test
public void queryData() throws Exception {
Get get = new Get(Bytes.toBytes("mc_123"));
Result result = table.get(get);
byte[] name = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
byte[] sex = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("sex"));
System.out.println(Bytes.toString(name));
System.out.println(Bytes.toInt(age));
System.out.println(Bytes.toString(sex));
}


/**
* 全表扫描
* 
* @throws Exception
*/
@Test
public void scanData() throws Exception {
//设置一个全表扫描
Scan scan = new Scan();

//scan.setStartRow(Bytes.toBytes("1"));
//scan.setStopRow(Bytes.toBytes("z"));
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] name = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
byte[] sex = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("sex"));
System.out.println(Bytes.toString(result.getRow()));
System.out.println(Bytes.toString(name));
System.out.println(Bytes.toInt(age));
System.out.println(Bytes.toString(sex));
}
}
/**
* 
* 列值过滤器
* 
*
*/
public void scanDatabyFilter1() throws Exception {
//创建一个过滤器
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("info1"), Bytes.toBytes("name"), CompareOp.EQUAL, Bytes.toBytes("mc_123"));
//设置一个全表扫描
Scan scan = new Scan();
//设置过滤器
scan.setFilter(singleColumnValueFilter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] name = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
byte[] sex = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("sex"));
System.out.println(Bytes.toString(name));
System.out.println(Bytes.toInt(age));
System.out.println(Bytes.toString(sex));
}
}

public void scanDatabyFilter2() throws Exception {
//创建一个过滤器
ColumnPrefixFilter columnPrefixFilter = new ColumnPrefixFilter(Bytes.toBytes("name_"));
//设置一个全表扫描
Scan scan = new Scan();
//设置过滤器
scan.setFilter(columnPrefixFilter);吗
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] name = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
byte[] sex = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("sex"));
System.out.println(Bytes.toString(result.getRow()));
System.out.println(Bytes.toString(name));
System.out.println(Bytes.toInt(age));
System.out.println(Bytes.toString(sex));
}
}


/*
* 多列名浅醉过滤器
byte[][] prefixes = new byte[][] {Bytes.toBytes("value1"),Bytes.toBytes("value2")};
Filter f = new MultipleColumnPrefixFilter(prefixes);
scan.setFilter(f);
*/



//rowkey过滤器
public void scanDatabyFilter3() throws Exception {
//创建一个过滤器匹配以mc_12开头的rowkey
Filter rowfilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^mc_12")); 
//设置一个全表扫描
Scan scan = new Scan();
//设置过滤器
scan.setFilter(rowfilter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] name = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
byte[] sex = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("sex"));
System.out.println(Bytes.toString(result.getRow()));
System.out.println(Bytes.toString(name));
System.out.println(Bytes.toInt(age));
System.out.println(Bytes.toString(sex));
}
}
//多个过滤器查询  FilterList
public void scanDatabyFilter4() throws Exception {
//创建一个过滤器     匹配以mc_12开头的rowkey以及name为zhangsan的数据
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE); 
Filter rowfilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^mc_12")); 
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
Bytes.toBytes("info1"), Bytes.toBytes("name"), CompareOp.EQUAL, Bytes.toBytes("zhangsan"));
filterList.addFilter(rowfilter);
filterList.addFilter(singleColumnValueFilter);
//设置一个全表扫描
Scan scan = new Scan();
//设置过滤器
scan.setFilter(filterList);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] name = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
byte[] sex = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("sex"));
System.out.println(Bytes.toString(result.getRow()));
System.out.println(Bytes.toString(name));
System.out.println(Bytes.toInt(age));
System.out.println(Bytes.toString(sex));
}
}
}

说明: 可以把这当成一个工具类,知道怎么用就行了。

猜你喜欢

转载自blog.csdn.net/zhaoxiangchong/article/details/78409160