/**
* @(#)HbaseTest.java 1.0 2017年7月19日
* @Copyright: Copyright 2007 - 2017 MPR Tech. Co. Ltd. All Rights Reserved.
* @Description:
*
* Modification History:
* Date: 2017年7月19日
* Author: liuwei
* Version: 1.0.0.0
* Description: (Initialize)
* Reviewer:
* Review Date:
*/
package com.hbase.hbasetest;
import java.io.IOException;
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.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
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.FilterList;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseTest {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
/**
* 初始化链接
*/
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "172.16.5.125,172.16.5.126,172.16.5.127");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
System.out.println("链接-----");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭连接
*/
public static void close() {
try {
if (null != admin) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建表
*
* @param tableName 表名
* @param family 列族列表
* @throws IOException
*/
public static void createTable(String tableName, String[] cols) throws IOException {
init();
TableName tName = TableName.valueOf(tableName);
if (admin.tableExists(tName)) {
println(tableName + " exists.");
} else {
HTableDescriptor hTableDesc = new HTableDescriptor(tName);
for (String col : cols) {
HColumnDescriptor hColumnDesc = new HColumnDescriptor(col);
hTableDesc.addFamily(hColumnDesc);
}
admin.createTable(hTableDesc);
}
close();
}
/**
* 删除表
*
* @param tableName 表名称
* @throws IOException
*/
public static void deleteTable(String tableName) throws IOException {
init();
TableName tName = TableName.valueOf(tableName);
if (admin.tableExists(tName)) {
admin.disableTable(tName);
admin.deleteTable(tName);
} else {
println(tableName + " not exists.");
}
close();
}
/**
* 查看已有的表
* @throws IOException
*/
public static void listTables() throws IOException{
init();
HTableDescriptor hTableDescriptor[]=admin.listTables();
for(HTableDescriptor h:hTableDescriptor){
System.out.println(h.getNameAsString());
}
close();
}
public static void delete(String tableName, String rowKey, String colFamily, String col) throws IOException {
init();
if (!admin.tableExists(TableName.valueOf(tableName))) {
println(tableName + " not exists.");
} else {
Table table = connection.getTable(TableName.valueOf(tableName));
Delete del = new Delete(Bytes.toBytes(rowKey));
if (colFamily != null) {
del.addFamily(Bytes.toBytes(colFamily));
}
if (colFamily != null && col != null) {
del.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
}
/*
* 批量删除 List<Delete> deleteList = new ArrayList<Delete>(); deleteList.add(delete); table.delete(deleteList);
*/
table.delete(del);
table.close();
}
close();
}
public static void main(String[] args) throws IOException {
//listTables();
//getData("reading", "095|1383741390291|2017-07-14 16:30:28.308|201510191634114768|632", null, null);
scan("reading");
}
/**
* 根据RowKey获取数据
*
* @param tableName 表名称
* @param rowKey RowKey名称
* @param colFamily 列族名称
* @param col 列名称
* @throws IOException
*/
public static void getData(String tableName, String rowKey, String colFamily, String col) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
if (colFamily != null) {
get.addFamily(Bytes.toBytes(colFamily));
}
if (colFamily != null && col != null) {
get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
}
Result result = table.get(get);
showCell(result);
table.close();
close();
}
/**
* 格式化输出
*
* @param result
*/
public static void showCell(Result result) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.print("RowName: " + new String(CellUtil.cloneRow(cell)) + " ");
System.out.print("column Family: " + new String(CellUtil.cloneFamily(cell)) + " ");
System.out.print("row Name: " + new String(CellUtil.cloneQualifier(cell)) + " ");
System.out.print("value: " + new String(CellUtil.cloneValue(cell)) + " ");
System.out.println("");
}
}
/**
全表扫描
*/
public static void scan(String tableName) throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for(Result re:scanner){
showCell(re);
System.out.println(“———————————————-“);
}
close();
}
/**
* 插入单行
*
* @param tableName 表名称
* @param rowKey RowKey
* @param colFamily 列族
* @param col 列
* @param value 值
* @throws IOException
*/
public static void insert(String tableName, String rowKey, String colFamily, String col, String value) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(value));
table.put(put);
/*
* 批量插入 List<Put> putList = new ArrayList<Put>(); puts.add(put); table.put(putList);
*/
table.close();
close();
}
}