package com.gense;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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;
public class HbaseTest_01
{
private static Configuration conf = null;
/**
* static hadoop configuration object
*/
static
{
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.146.129");
}
/**
* create hbase table
* @param tableName
* @param columnFamilys
* @throws IOException
* @see [类、类#方法、类#成员]
*/
private static void createTable(String tableName,String[] columnFamilys) throws IOException
{
HBaseAdmin hbaseAdmin = new HBaseAdmin(conf);
//判断tableName是否存在
if(hbaseAdmin.tableExists(tableName))
{
System.out.println("the "+tableName +" is exists");
}
else
{
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
for(String column:columnFamilys)
{
tableDesc.addFamily(new HColumnDescriptor(column));
}
hbaseAdmin.createTable(tableDesc);
System.out.println("create table "+tableName+" success");
}
}
/**
* delete hbase table
* @param tableName
* @throws IOException
* @see [类、类#方法、类#成员]
*/
private static void deleteTable(String tableName) throws IOException
{
HBaseAdmin admin = new HBaseAdmin(conf);
//判断tableName是否存在
if(!admin.tableExists(tableName))
{
System.out.println("the "+tableName+" not exists");
}
else
{
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("the "+tableName+" delete success");
}
}
/**
* insert data into table
* @param tableName
* @param row
* @param family
* @param qualifier
* @param value
* @throws IOException
* @see [类、类#方法、类#成员]
*/
private static void put(String tableName,String row,String family,String qualifier,String value) throws IOException
{
HTable hTable = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(row));
put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
hTable.put(put);
System.out.println("insert record "+row +" to table "+tableName+" success");
}
/**
* delete record
* @param tableName
* @param rowkey
* @throws IOException
* @see [类、类#方法、类#成员]
*/
private static void delete(String tableName,String rowkey) throws IOException
{
HTable hTable = new HTable(conf, tableName);
Delete delete = new Delete(Bytes.toBytes(rowkey));
hTable.delete(delete);
System.out.println("delete record "+rowkey +" from table "+tableName+" success");
}
/**
* get data from table
* @param tableName
* @param rowkey
* @param family
* @param qualifier
* @return
* @throws IOException
* @see [类、类#方法、类#成员]
*/
private static void get(String tableName,String rowkey,String family,String qualifier) throws IOException
{
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(rowkey));
//column family
if(family!=null)
{
//qualifier
if(qualifier!=null)
{
get.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier));
}else
{
get.addFamily(Bytes.toBytes(family));
}
}
Result result = table.get(get);
for(KeyValue value : result.raw())
{
System.out.println(new String(value.getRow())+"\t"+new String(value.getFamily())+":"+new String(value.getQualifier())+"\t"+new String(value.getValue())+"\t"+value.getTimestamp());
}
}
/**
* scan all data
* @param tableName
* @throws IOException
* @see [类、类#方法、类#成员]
*/
private static void scan(String tableName) throws IOException
{
HTable table = new HTable(conf, tableName);
//defined scan
Scan scan = new Scan();
//scan table
ResultScanner scanner = table.getScanner(scan);
for(Result result : scanner)
{
for(KeyValue value : result.raw())
{
System.out.println(new String(value.getRow())+"\t"+new String(value.getFamily())+":"+new String(value.getQualifier())+"\t"+new String(value.getValue())+"\t"+value.getTimestamp());
}
}
}
/** <一句话功能简述>
* <功能详细描述>
* @param args
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static void main(String[] args) throws IOException
{
//hbase create table
//createTable("test1", new String[]{"baseinfo"});
//delete table
//deleteTable("test");
//insert data
//put("test1", "kitty", "baseinfo", "", "kitty");
//put("test1", "kitty", "baseinfo", "age", "22");
//delete("test1", "kitty");
//get data
//get("test1","kitty",null,null);
//scan all data
scan("test1");
}
}