大数据之Hbase的API操作

package com.bw;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;

public class HbaseTest {

/*
4. 查看user_info表结构(若无user_info表先创建user_info表,包含info、data两个列族)。
5. 向user_info表中插入如下信息:
row key为rk0001,info:name值为zhangsan,info:age值为18;data:math值为80,data:computer值为90
row key为rk0002,info:name值为lisi,info:age值为19;data:math值为70,data:computer值为90
6. 查询user_info表中行键为rk0002的记录。
7. 查询user_info表中info:name为zhangsan的记录。
8. 查询user_info表中info:age大于等于18的记录。
9. 删除user_info中行键为rk0001的记录。*/

private static Connection connection = null;

static {

Configuration configuration = HBaseConfiguration.create();

configuration.set("hbase.zookeeper.quorum","hadoop1,hadoop2,hadoop3");
configuration.set("hbase.zookeeper.property.client","2181");

try {
connection = ConnectionFactory.createConnection(configuration);
} catch (IOException e) {
e.printStackTrace();
}
}

//删除表
private static void deleteTable(String tableName) throws IOException {
Admin admin = connection.getAdmin();

try {
if(!admin.tableExists(TableName.valueOf(tableName))){
return;
}
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
}finally {
admin.close();
}
}

//建表
private static void createTable(String tableName,String... family) throws IOException {
Admin admin = connection.getAdmin();

try {
if(admin.tableExists(TableName.valueOf(tableName))){
return;
}
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

for (String s : family) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(s);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
}finally {
admin.close();
}
}

//插入数据
private static void putCells(String tableName,String rowkey,String family,String column,String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Admin admin = connection.getAdmin();
try {
if(!admin.tableExists(TableName.valueOf(tableName))){
return;
}
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
table.put(put);
}finally {
table.close();
}
}

//查询数据
private static void getCell(String tableName,String rowkey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
try {
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
byte[] columnBytes = CellUtil.cloneQualifier(cell);
String columnStr = Bytes.toString(columnBytes);
//获取值
String valueStr = Bytes.toString(CellUtil.cloneValue(cell));

System.out.println(columnStr + " : " + valueStr);
}

}finally {
table.close();
}
}

//过滤 查询user_info表中info:name为zhangsan的记录。
private static void filterColumnValue(String tableName,String family,String column,String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
try {
//2
Scan scan = new Scan(Bytes.toBytes(family));
//4
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(column), CompareFilter.CompareOp.GREATER_OR_EQUAL, Bytes.toBytes(value));
//3
scan.setFilter(filter);
//1去执行scan操作方法
ResultScanner scanner = table.getScanner(scan);
//先循环行,然后通过行循环列
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
String rowStr = Bytes.toString(CellUtil.cloneRow(cell));
String familyStr = Bytes.toString(CellUtil.cloneFamily(cell));
String columnStr = Bytes.toString(CellUtil.cloneQualifier(cell));
String valueStr = Bytes.toString(CellUtil.cloneValue(cell));

System.out.println(rowStr + " : " + familyStr + " : " + columnStr + " : " + valueStr);
}
}
}finally {
table.close();
}
}

//删除某一行记录
private static void deleteRow(String tableName,String rowkey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
try {
Delete delete = new Delete(Bytes.toBytes(rowkey));

table.delete(delete);
}finally {
table.close();
}
}
//查询所有
public static void scans(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan();

ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
String row = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String column = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(row+"-"+family+"-"+column+"-"+value);
}
}
table.close();
}

public static void main(String[] args) throws IOException {
//删除表
// deleteTable("user_info");
// deleteTable("employee");
// deleteTable("student");
//建表
//createTable("user_info","info","data");
// createTable("Score","sname","course");
//插入数据
/* row key为rk0001,info:name值为zhangsan,info:age值为18;data:math值为80,data:computer值为90
row key为rk0002,info:name值为lisi,info:age值为19;data:math值为70,data:computer值为90*/
/* putCells("user_info","rk0001","info","name","zhangsan");
putCells("user_info","rk0001","info","age","18");
putCells("user_info","rk0001","data","math","80");
putCells("user_info","rk0001","data","computer","90");
putCells("user_info","rk0002","info","name","lisi");
putCells("user_info","rk0002","info","age","19");
putCells("user_info","rk0002","data","math","70");
putCells("user_info","rk0002","data","computer","90");
*/
// putCells("Score","95001","course","Math","88");
// putCells("Score","95001","course","English","85");
//根据rowkey获取数据
// getCell("user_info","rk0002");

//查询user_info表中info:name为zhangsan的记录。
// filterColumnValue("user_info","info","name","zhangsan");
// filterColumnValue("user_info","data","math","80");
//删除user_info中行键为rk0001的记录
// deleteRow("user_info","rk0001");

// scans("user_info");


}


}

大数据之Hbase的API操作

上一篇:P3622 [APIO2007]动物园


下一篇:真-API控制器AJAS