package com.sanmao.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.*;
/**
* HBase的相关java API 操作
*/
public class HbaseCRUDTest {
private Admin admin;
private Configuration configuration;
private Connection connection;
@Before
public void setup() throws IOException {
//获取配置信息
configuration = new Configuration();
//设置配置信息
// 自动加载 resources下的hbase-site.xml
// 也可以手动配置
configuration.set("hbase.rootdir","hdfs://master:9000/hbase");
configuration.set("hbase.zookeeper.quorum","master,slave01,slave02");
//根据配置文件,创建连接
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
// System.out.println(admin);
}
//获取Hbase数据库中,表的信息
@Test
public void testList() throws IOException {
TableName[] tableNames = admin.listTableNames();
System.out.println(Arrays.toString(tableNames));
HTableDescriptor[] htds = admin.listTables();
for (HTableDescriptor htd:htds) {
TableName tn =htd.getTableName();
System.out.println(tn + " ");
}
}
/**
* 创建一个表
* */
@Test
public void testAddTable() throws IOException {
System.out.println("=----------testAddTable---------------------");
//创建一个表名称 sanmao
TableName tn = TableName.valueOf("sanmao1");
//构造一个表的描述器
HTableDescriptor htd = new HTableDescriptor(tn);
//列族描述描述器
HColumnDescriptor family = new HColumnDescriptor("cf");
//添加列族
htd.addFamily(family);
//创建表
admin.createTable(htd);
}
/**
* 往表中添加记录信息
* */
@Test
public void testAddRecord() throws IOException {
Table table = connection.getTable(TableName.valueOf("sanmao1"));
byte[] rowkey = "1".getBytes();
Put put = new Put(rowkey);
byte[] family = "cf".getBytes();
byte[] qualifier = "name".getBytes();
byte[] value = "maomao".getBytes();
put.addColumn(family,qualifier,value);
byte[] qualifier1 = "age".getBytes();
byte[] value1="18".getBytes();
put.addColumn(family,qualifier1,value1);
table.put(put);
}
/**
* 获取其中一条记录
* 列族
* 列
* 值
* */
@Test
public void testQueryRecord() throws IOException {
Table table = connection.getTable(TableName.valueOf("sanmao1"));
String rowkey="1";
Get get = new Get(rowkey.getBytes());
Result result = table.get(get);
List<Cell> cells = result.listCells();
for (Cell cell:cells){
Map<String,String> map = getCell(cell);
for (Map.Entry<String,String> me : map.entrySet()){
System.out.println(me.getKey()+"=="+me.getValue());
}
}
System.out.println("--------------------------");
}
//从HFile 上提取cell 详细信息
public Map<String,String> getCell(Cell cell){
Map<String,String> map = new TreeMap<String, String>(new Comparator<String>() {
public int compare(String o1, String o2) {
return 1;//按存放顺序输出
}
});
String rk = new String(cell.getRowArray()).substring(cell.getRowOffset(),cell.getRowOffset()+cell.getRowLength());
String cf = new String(cell.getFamilyArray()).substring(cell.getQualifierOffset(),cell.getQualifierOffset()+cell.getQualifierLength());
String qualifier = new String(cell.getQualifierArray()).substring(cell.getQualifierOffset(),cell.getQualifierOffset()+cell.getQualifierLength());
String value = new String(cell.getValueArray()).substring(cell.getValueOffset(),cell.getValueOffset()+cell.getValueLength());
map.put("rk",rk);
map.put("cf",cf);
map.put("cq",qualifier);
map.put("v",value);
return map;
}
/**
* 显示Result内容
* */
private void showResult(Result result){
List<Cell> cells = result.listCells();
for (Cell cell : cells){
Map<String,String> map = getCell(cell);
for (Map.Entry<String,String> me : map.entrySet()){
System.out.println(me.getValue()+" ");
}
System.out.println();
}
}
/**
* 获取全表记录
* 列族
* 列
* 值
* */
@Test
public void testQueryAllRecord() throws IOException {
Table table = connection.getTable(TableName.valueOf("sanmao1"));
Scan scan = new Scan();
ResultScanner rs = table.getScanner(scan);
for (Result result : rs){
showResult(result);
}
table.close();
}
/**
* 添加过滤器查询(条件查询)
* */
@Test
public void testQueryByCodition() throws IOException {
Table table = connection.getTable(TableName.valueOf("sanmao1"));
Scan scan = new Scan();
byte[] family = "cf".getBytes();
byte[] qualifer = "age".getBytes();
/**
* family 指定要在那个列族上进行数据过滤
* qualifier 指定要在哪个列族上的列进行数据过滤
* CompareOp 具体要比较的方式
* ByteArrayComparable 要和谁【字节数组】进行比较
* */
Filter filter = new SingleColumnValueExcludeFilter(family,qualifer,
CompareFilter.CompareOp.LESS_OR_EQUAL,"18".getBytes());
scan.setFilter(filter);
ResultScanner rs= table.getScanner(scan);
for (Result result :rs){
showResult(result);
}
table.close();
}
@Test
public void testDropTable() throws IOException {
TableName tn = TableName.valueOf("sanmao1");
if (admin.tableExists(tn)){//判断表是否存在
if (!admin.isTableDisabled(tn)){
//判断表状态是否是disable
admin.disableTable(tn);
}
admin.deleteTable(tn);
}
}
@After
public void cleanup() throws IOException {
admin.close();
connection.close();
}
}
大数据之Hbase[JavaAPI-CRUD]
最新推荐文章于 2022-06-12 19:00:40 发布