这里主要是针对Hbase的java Api编程做一个记录。以便以后复习回顾。
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
public class Hbase1 {
//指定列族名和表名
private static String SERIES = "info";
private static String TABLENAME = "user";
private static Connection conn;
private static String hbaseIp = "192.168.1.11";
public static void init() {
Configuration config = HBaseConfiguration.create();
//指定zookeeper的ip和端口
config.set("hbase.zookeeper.quorum", hbaseIp);
config.set("hbase.zookeeper.property.clientPort","2181");
try {
conn = ConnectionFactory.createConnection(config);
} catch (IOException e) {
e.printStackTrace();
}
}
//创建hbase表
public static void createTable(String tableName, String seriesStr) throws IllegalArgumentException, IOException {
Admin admin = null;
TableName table = TableName.valueOf(tableName);
try {
admin = conn.getAdmin();
if (!admin.tableExists(table)) {
System.out.println(tableName + " table not Exists");
HTableDescriptor descriptor = new HTableDescriptor(table);
String[] series = seriesStr.split(",");
for (String s : series) {
descriptor.addFamily(new HColumnDescriptor(s.getBytes()));
}
admin.createTable(descriptor);
}
} catch(Exception e){
e.printStackTrace();
}
finally {
IOUtils.closeQuietly(admin);
}
}
//添加数据
public static void add(String rowKey, Map<String, Object> columns) throws IOException {
Table table = null;
try {
table = conn.getTable(TableName.valueOf(TABLENAME));
Put put = new Put(Bytes.toBytes(rowKey));
for (Map.Entry<String, Object> entry : columns.entrySet()) {
put.addColumn(SERIES.getBytes(), Bytes.toBytes(entry.getKey()),
Bytes.toBytes(entry.getValue().toString()));
}
table.put(put);
} finally {
IOUtils.closeQuietly(table);
}
}
//根据table查询所有数据
public static void getValueByTable() throws Exception {
Table table = null;
try {
table = conn.getTable(TableName.valueOf(TABLENAME));
ResultScanner rs = table.getScanner(new Scan());
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println(
"列:" + new String(keyValue.getFamily()) + "====值:" + new String(keyValue.getValue()));
}
}
} finally {
IOUtils.closeQuietly(table);
}
}
//根据table查询所有数据另一种实现
public static void getValueByTable1() throws Exception {
Table table = null;
try {
table = conn.getTable(TableName.valueOf(TABLENAME));
ResultScanner rs = table.getScanner(new Scan());
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (Cell cell: r.listCells()) {
System.out.println(
"列:" + new String(cell.getFamilyArray(),"utf-8"));
}
}
} finally {
IOUtils.closeQuietly(table);
}
}
//根据rowkey获取数据
public static Map<String, String> getAllValue(String rowKey) throws IllegalArgumentException, IOException {
Table table = null;
Map<String, String> resultMap;
try {
table = conn.getTable(TableName.valueOf(TABLENAME));
Get get = new Get(Bytes.toBytes(rowKey));
get.addFamily(SERIES.getBytes());
Result res = table.get(get);
Map<byte[], byte[]> result = res.getFamilyMap(SERIES.getBytes());
Iterator<Entry<byte[], byte[]>> it = result.entrySet().iterator();
resultMap = new HashMap<String, String>();
while (it.hasNext()) {
Entry<byte[], byte[]> entry = it.next();
resultMap.put(Bytes.toString(entry.getKey()), Bytes.toString(entry.getValue()));
}
} finally {
IOUtils.closeQuietly(table);
}
return resultMap;
}
//根据rowkey和column获取数据
public static String getValueBySeries(String rowKey, String column) throws IllegalArgumentException, IOException {
Table table = null;
String resultStr = null;
try {
table = conn.getTable(TableName.valueOf(TABLENAME));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(SERIES), Bytes.toBytes(column));
Result res = table.get(get);
byte[] result = res.getValue(Bytes.toBytes(SERIES), Bytes.toBytes(column));
resultStr = Bytes.toString(result);
} finally {
IOUtils.closeQuietly(table);
}
return resultStr;
}
//删除表
public static void dropTable(String tableName) throws IOException {
Admin admin = null;
TableName table = TableName.valueOf(tableName);
try {
admin = conn.getAdmin();
if (admin.tableExists(table)) {
admin.disableTable(table);
admin.deleteTable(table);
}
} finally {
IOUtils.closeQuietly(admin);
}
}
//根据过滤器查询
public static void filterData(){
Table table = null;
try{
table = conn.getTable(TableName.valueOf(TABLENAME));
Scan scan = new Scan();
//前缀过滤器
Filter filter = new PrefixFilter(Bytes.toBytes("rk"));
//分页过滤器,指定记录条数
//filter = new PageFilter(3);
//值比较器
/*ByteArrayComparable comparable = null;
comparable = new SubstringComparator("jiangyi");
Filter filter = new SingleColumnValueFilter(Bytes.toBytes("info"),Bytes.toBytes("name"),
CompareFilter.CompareOp.EQUAL,comparable);
scan.addColumn("info".getBytes(),"name".getBytes());*/
scan.setFilter(filter);
ResultScanner results = table.getScanner(scan);
for(Result result : results){
printData(result);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(table != null){
org.apache.hadoop.io.IOUtils.closeStream(table);
}
}
}
//根据扫描器查询
public static void scanData(){
Table table = null;
try{
table = conn.getTable(TableName.valueOf(TABLENAME));
Scan scan = new Scan();
//查询范围内数据
// scan.setStartRow(Bytes.toBytes("rowkey001"));
// scan.setStopRow(Bytes.toBytes("rowkey002"));
//指定列族
scan.addFamily(Bytes.toBytes("info"));
ResultScanner results = table.getScanner(scan);
for(Result result : results){
printData(result);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(table != null){
org.apache.hadoop.io.IOUtils.closeStream(table);
}
}
}
public static void printData(Result result){
for(Cell cell : result.rawCells()){
System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println(new String(CellUtil.cloneRow(cell)));
System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println(new String(CellUtil.cloneValue(cell)));
}
}
public static void main(String[] args) throws Exception{
init();
//创建表
createTable(TABLENAME, SERIES);
//添加数据
// String rowkey = "rowkey001";
// Map<String,Object> columnMap = new HashMap<String, Object>();
// columnMap.put("name","jiangyi");
// columnMap.put("age","28");
// add(rowkey,columnMap);
//根据表查询所有数据
// getValueByTable();
//
//
// getValueByTable1();
//根据行健获取所有数据
/*Map<String, String> tempMap = getAllValue("rowkey001");
System.out.println(tempMap);
//根据行健和列名获取数据
System.out.println(getValueBySeries("rowkey001","name"));*/
//根据过滤器查询
//filterData();
//根据扫描器查询
// scanData();
}
}
以上就是对API编程的一些简单介绍。