package com.cxy.hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class Hbase {
static Configuration conf = null;
static Connection conn = null;
static{
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "127.0.0.1:2181");
try {
conn = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Create table
* @throws IOException
*/
public static void createTable(String tableName,String[]family) throws Exception{
Admin admin = conn.getAdmin();
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
for(int i =0;i<family.length;i++){
desc.addFamily(new HColumnDescriptor(family[i]));
}
if(admin.tableExists(TableName.valueOf(tableName))){
System.out.println("Table Exists");
System.exit(0);
}else{
admin.createTable(desc);
System.out.println("Create table success!");
}
}
/**
* Add data
*/
public static void addData(String rowKey,String tableName,String[] column1,String[] value1,String[] column2,String[] value2) throws IOException{
//config ROW_KEY
Put put = new Put(Bytes.toBytes(rowKey));
Table table = conn.getTable(TableName.valueOf(tableName));
HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies();
for(int i=0;i<columnFamilies.length;i++){
//get column_name
String familyName = columnFamilies[i].getNameAsString();
if(familyName.equals("article")){
for(int j=0;j<column1.length;j++){
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j]));
}
}
if(familyName.equals("author")){
for(int j=0;j<column2.length;j++){
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(column2[j]), Bytes.toBytes(value2[j]));
}
}
}
table.put(put);
System.out.println("Add data success");
}
/**
* queryBy ROW_KEY
* @throws IOException
*/
public static Result getResult(String tableName,String rowKey) throws IOException{
Get get = new Get(Bytes.toBytes(rowKey));
Table table = conn.getTable(TableName.valueOf(tableName));
Result result = table.get(get);
for(Cell cell:result.listCells()){
System.out.println("family:"+cell.toString());
}
return result;
}
/**
* Scanner table
*/
public static void getResultScann(String tableName) throws IOException{
Scan scan = new Scan();
ResultScanner rs = null;
Table table = conn.getTable(TableName.valueOf(tableName));
rs = table.getScanner(scan);
for(Result r :rs){
for(Cell cell:r.listCells()){
System.out.println("row:"+Bytes.toString(cell.getRowArray(),cell.getRowOffset(),cell.getRowLength()));
System.out.println("family:"+Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getValueLength()));
System.out.println("qualifier:"+Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(),cell.getQualifierLength()));
System.out.println("value:"+Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));
System.out.println("timestamp:"+cell.getTimestamp());
System.out.println("-------------------------------------");
}
}
rs.close();
}
/**
* queryBy column
*/
public static void getResultByColumn(String tableName,String rowKey,String familyName,String columnName) throws IOException{
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
Result result = table.get(get);
for(Cell cell:result.listCells()){
System.out.println("family:"+Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getValueLength()));
System.out.println("qualifier:"+Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(),cell.getQualifierLength()));
System.out.println("value:"+Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));
System.out.println("timestamp:"+cell.getTimestamp());
System.out.println("-------------------------------------");
}
}
/**
* queryBy cloumn version
*/
public static void getResultByVersion(String tableName,String rowKey,String familyName,String columnName) throws IOException{
Table table = conn.getTable(TableName.valueOf(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(Cell cell:result.listCells()){
System.out.println("version:"+cell.getTimestamp());
}
}
/**
* delete Column
*/
public static void deleteColumn(String tableName,String rowKey,String familyName,String columnName) throws IOException{
Table table = conn.getTable(TableName.valueOf(tableName));
Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));
deleteColumn.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
table.delete(deleteColumn);
System.out.println(familyName+","+columnName+"is deleted");
}
/**
* delete rowkey
*/
public static void deleteRowKey(String tableName,String rowKey) throws IOException{
Table table = conn.getTable(TableName.valueOf(tableName));
Delete deleteAll = new Delete(Bytes.toBytes(rowKey));
table.delete(deleteAll);
System.out.println("delete row");
}
/**
* delete table
*/
public static void deleteTable(String tableName) throws IOException{
Admin admin = conn.getAdmin();
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println(tableName+"is deleted");
}
public static void main(String[] args) throws Exception {
//# export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/usr/lib/hbase/lib/*
//# hadoop jar hbase.jar
//Create table
/*String tableName = "blog";
String[] family = {"article","author"};
createTable(tableName, family);*/
//Add data
/*String[] column1 = {"title","content","tag"};
String[] value1 = {"Head Fist Hbase","Hadoop","HBase"};
String[] column2 = {"name","nickname"};
String[] value2 = {"nicholas","lee"};
addData("rowkey1", "blog", column1, value1, column2, value2);
addData("rowkey2", "blog", column1, value1, column2, value2);
addData("rowkey3", "blog", column1, value1, column2, value2);*/
//queryBy ROW_KEY
/*("blog", "rowkey1");*/
//Scanner table
/*getResultScann("blog");*/
//queryBy column
/*getResultByColumn("blog", "rowkey1", "article", "content");*/
//queryBy cloumn version
/*getResultByVersion("blog", "rowkey1", "article", "content");*/
//delete Column
/*deleteColumn("blog", "rowkey1", "article", "content");*/
//delete Row
/*deleteRowKey("blog", "rowkey1");*/
//delete table
/*deleteTable("blog");*/
}
}
07-21
06-19
08-16
08-16
06-03