package com.utils;
import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
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.HTable;
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.util.Bytes;
/**
* hbase
* @author xiaoming.liang
*
*/
public class HbaseUtils {
HBaseAdmin admin = null;
Configuration conf = null;
public HbaseUtils() {
conf = new Configuration();
conf.set(“hbase.zookeeper.quorum”,
“10.200.42.1,10.200.42.2,10.200.42.3”);
conf.set(“hbase.rootdir”, “hdfs://cluster/hbase”);
try {
admin = new HBaseAdmin(conf);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
HbaseUtils hbaseUtils = new HbaseUtils();
// 创建表1
//hbaseUtils.createTable(“test_demo”, “cfg”);
String tableName = “blog2”;
String[] family = { “article”, “author” };
//hbaseUtils.creatTable(tableName, family);
// 为表加入数据
String[] column1 = { “title”, “content”, “tag” };
String[] value1 = {
“Head First HBase”,
“HBase is the Hadoop database. Use it when you need random, realtime read/write access to your Big Data.”,
“Hadoop,HBase,NoSQL” };
String[] column2 = { “name”, “nickname” };
String[] value2 = { “nicholas”, “lee” };
hbaseUtils.addData(“rowkey1”, “blog2”, column1, value1, column2, value2);
hbaseUtils.addData(“rowkey2”, “blog2”, column1, value1, column2, value2);
hbaseUtils.addData(“rowkey3”, “blog2”, column1, value1, column2, value2);
// 遍历查询
hbaseUtils.getResultScann(“blog2”, “rowkey4”, “rowkey5”);
//查询
hbaseUtils.getResult(“blog2”, “rowkey1”);
// 查询某一列的值
hbaseUtils.getResultByColumn(“blog2”, “rowkey1”, “author”, “name”);
// 更新列
hbaseUtils.updateTable(“blog2”, “rowkey1”, “author”, “name”, “bin”);
// 查询某列的多版本号
hbaseUtils.getResultByVersion(“blog2”, “rowkey1”, “author”, “name”);
// 删除一列
hbaseUtils.deleteColumn(“blog2”, “rowkey1”, “author”, “nickname”);
// 删除一列
hbaseUtils.deleteColumn(“blog2”, “rowkey1”, “author”, “nickname”);
// 删除全部列
hbaseUtils.deleteAllColumn(“blog2”, “rowkey1”);
// 删除表
hbaseUtils.deleteTable(“blog2”);
}
//删除表
public void deleteTable(String tableName) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName + “is deleted!”);
}
public void deleteAllColumn(String tableName, String rowKey)
throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Delete deleteAll = new Delete(Bytes.toBytes(rowKey));
table.delete(deleteAll);
System.out.println(“all columns are deleted!”);
}
// 删除指定的列
public void deleteColumn(String tableName, String rowKey, String falilyName,
String columnName) throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));
deleteColumn.deleteColumn(Bytes.toBytes(falilyName), Bytes.toBytes(columnName));
table.delete(deleteColumn);
System.out.println(falilyName + “:” + columnName + “is deleted!”);
}
//查询某列数据的多个版本号
public void getResultByVersion(String tableName, String rowKey,
String familyName, String columnName) throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
get.setMaxVersions(5);
Result result = table.get(get);
for (KeyValue kv : result.list()) {
System.out.println(“family:” + Bytes.toString(kv.getFamily()));
System.out.println(“qualifier:” + Bytes.toString(kv.getQualifier()));
System.out.println(“value:” + Bytes.toString(kv.getValue()));
System.out.println(“Timestamp:” + kv.getTimestamp());
System.out.println(“——————————————-“);
}
}
public void updateTable(String tableName, String rowKey, String familyName,
String columnName, String value) throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value));
table.put(put);
System.out.println(“update table Success!”);
}
//查询表中的某一列
public void getResultByColumn(String tableName, String rowKey,
String familyName, String columnName) throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));// // 获取指定列族和列修饰符相应的列
Result result = table.get(get);
for (KeyValue kv : result.list()) {
System.out.println(“family:” + Bytes.toString(kv.getFamily()));
System.out.println(“qualifier:” + Bytes.toString(kv.getQualifier()));
System.out.println(“value:” + Bytes.toString(kv.getValue()));
System.out.println(“Timestamp:” + kv.getTimestamp());
System.out.println(“——————————————-“);
}
}
public Result getResult(String tableName, String rowkey) throws IOException {
Get get = new Get(Bytes.toBytes(rowkey));
HTable table = new HTable(conf, Bytes.toBytes(tableName));// 获取表
Result result = table.get(get);
//
for (KeyValue kv : result.list()) {
//
System.out.println(“family:” + Bytes.toString(kv.getFamily()));
//
System.out
//
.println(“qualifier:” + Bytes.toString(kv.getQualifier()));
//
System.out.println(“value:” + Bytes.toString(kv.getValue()));
//
System.out.println(“Timestamp:” + kv.getTimestamp());
//
System.out.println(“——————————————-“);
//
}
return result;
}
//遍历查询hbase表
public void getResultScann(String tableName, String start_rowkey, String stop_rowkey) throws IOException {
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(start_rowkey));
scan.setStopRow(Bytes.toBytes(stop_rowkey));
HTable table = new HTable(conf, Bytes.toBytes(tableName));
ResultScanner rs = table.getScanner(scan);
try {
for (Result r : rs) {
List<KeyValue> list = r.list();
for (KeyValue kv : list) {
System.out.println(“row:” + Bytes.toString(kv.getRow()));
System.out.println(“family:” + Bytes.toString(kv.getFamily()));
System.out.println(“qualifier:” + Bytes.toString(kv.getQualifier()));
System.out.println(“value:” + Bytes.toString(kv.getValue()));
System.out.println(“timestamp:” + kv.getTimestamp());
System.out.println(“——————————————-“);
}
}
}finally{
rs.close();
}
}
//为表加入数据(适合知道有多少列族的固定表)
public void addData(String rowKey, String tableName, String[] column1,
String[] value1, String[] column2, String[] value2) throws IOException {
Put put = new Put(Bytes.toBytes(rowKey));// 设置rowkey
HTable table = new HTable(conf, Bytes.toBytes(tableName));// HTabel负责跟记录相关的操作如增删改查等//
HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies(); // 获取全部的列族
for (int i = 0; i < columnFamilies.length; i++) {
String familyName = columnFamilies[i].getNameAsString(); // 获取列族名
// article列族put数据
if(familyName.equals(“article”)){
for(int j =0;j<column1.length;j++){
put.add(Bytes.toBytes(familyName),Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j]));
}
}
if(familyName.equals(“author”)){
for(int j =0;j<column2.length;j++){
put.add(Bytes.toBytes(familyName),Bytes.toBytes(column2[j]), Bytes.toBytes(value2[j]));
}
}
}
table.put(put);
System.out.println(“add data Success!”);
}
public void creatTable(String tableName, String[] family) {
HTableDescriptor desc = new HTableDescriptor(tableName);
for (int i = 0; i < family.length; i++) {
desc.addFamily(new HColumnDescriptor(family[i]));
}
try {
if (admin.tableExists(tableName)) {
System.out.println(“table Exists!”);
System.exit(0);
} else {
admin.createTable(desc);
System.out.println(“create table Success!”);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//
public void createTable(String tableName, String column) {
//
try {
//
if (admin.tableExists(tableName)) {
//
System.out.println(“table Exists!”);
//
} else {
//
HTableDescriptor desc = new HTableDescriptor(tableName);
//
desc.addFamily(new HColumnDescriptor(column));
//
admin.createTable(desc);
//
System.out.println(“create table Success!”);
//
}
//
} catch (IOException e) {
//
e.printStackTrace();
//
}
//
}
}