Hadoop_Java操作Hbase
package com.lius.hadoop.hbase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.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.Durability;
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;
import org.apache.log4j.Logger;
/**
* java操作Hbase
* @author Administrator
*
*/
public class operationHbase {
public static Logger log = Logger.getLogger(operationHbase.class);
public static Configuration conf = null;
private static Connection connection = null;
//初始化配置
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop1,hadoop2");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
public static void main(String[] args) throws IOException {
init();
//建立表
// createTable(TableName.valueOf("test"));
//插入資料
// insertData("marry","test");
//删除一張表
dropTable("t1");
//根據rowkey删除一條記錄
// deleteRow("xiaoming","test");
//查詢所有資料
// QueryAll("test");
//單條件查詢,根據rowkey查詢唯一一條記錄
// QueryByCondition1("marry","test");
connection.close();
}
/**
* 删除一張表
* @param string
* @throws IOException
*/
private static void dropTable(String tableName) throws IOException {
// TODO Auto-generated method stub
Admin admin = connection.getAdmin();
TableName tabName = TableName.valueOf(tableName);
admin.disableTable(tabName);
admin.deleteTable(tabName);
}
/**
* 根據rowkey删除一條記錄
* @param string
* @param string2
* @throws IOException
*/
private static void deleteRow(String rowkey, String tableName) throws IOException {
// TODO Auto-generated method stub
Table table = connection.getTable(TableName.valueOf(tableName));
List<Delete> lists = new ArrayList();
Delete delete = new Delete(rowkey.getBytes());
lists.add(delete);
table.delete(lists);
table.close();
}
/**
* 單條件查詢,根據rowkey查詢唯一一條記錄
* @param string
* @param string2
* @throws IOException
*/
private static void QueryByCondition1(String rowkey, String tableName) throws IOException {
// TODO Auto-generated method stub
Table table = connection.getTable(TableName.valueOf(tableName));
Get scan = new Get(rowkey.getBytes());
Result r = table.get(scan);
List<Cell> cells = r.listCells();
for(Cell cell:cells) {
System.out.println(String.format("rowkey:%s family:%s qualifier:%s value:%s",
Bytes.toString(CellUtil.cloneRow(cell)),
Bytes.toString(CellUtil.cloneFamily(cell)),
Bytes.toString(CellUtil.cloneQualifier(cell)),
Bytes.toString(CellUtil.cloneValue(cell))));
}
table.close();
}
/**
* 查詢所有資料
* @param string
* @throws IOException
*/
private static void QueryAll(String tableName) throws IOException {
// TODO Auto-generated method stub
Table table = connection.getTable(TableName.valueOf(tableName));
ResultScanner rs = table.getScanner(new Scan());
for(Result r:rs) {
System.out.println("獲得的rowkey:"+new String(r.getRow()));
List<Cell> cells = r.listCells();
for(Cell cell:cells) {
System.out.println(String.format("rowkey:%s family:%s qualifier:%s value:%s ",
Bytes.toString(CellUtil.cloneRow(cell)),
Bytes.toString(CellUtil.cloneFamily(cell)),
Bytes.toString(CellUtil.cloneQualifier(cell)),
Bytes.toString(CellUtil.cloneValue(cell))));
}
}
table.close();
}
private static void init() throws IOException {
// TODO Auto-generated method stub
connection = ConnectionFactory.createConnection(conf);
}
/**
* 插入資料
* @param rowkey
* @param tableName
* @throws IOException
*/
private static void insertData(String rowkey, String tableName) throws IOException {
// TODO Auto-generated method stub
Table table = connection.getTable(TableName.valueOf(tableName)); //create Table
Put put = new Put(rowkey.getBytes()); //create Put
put.addColumn(Bytes.toBytes("column2"),"screen_width".getBytes(),"1080".getBytes());//addColumn(Family,key,Value)
put.addColumn("column2".getBytes(), "screen_height".getBytes(), "1920".getBytes());
put.addColumn("column2".getBytes(), "url".getBytes(), "www.baidu.com".getBytes());
put.addColumn("column2".getBytes(), "event_data".getBytes(), "12|16|13|17|12|16".getBytes());
put.setDurability(Durability.SYNC_WAL);
table.put(put);
table.close();
}
/**
* 建立表
* @param string
* @throws IOException
*/
private static void createTable(TableName tableName) throws IOException {
// TODO Auto-generated method stub
log.info("start create table...");
Admin hbaseAdmin = connection.getAdmin();
if(hbaseAdmin.tableExists(tableName)) {
hbaseAdmin.disableTable(tableName);
hbaseAdmin.deleteTable(tableName);
log.info(String.format("table %s is exists,delete...", tableName.toString()));
}
// TableDescriptor table = TableDescriptorBuilder.newBuilder(tableName).build();
// table.getColumnFamilies();
HTableDescriptor hTable = new HTableDescriptor(tableName);
hTable.addFamily(new HColumnDescriptor("column1"));
hTable.addFamily(new HColumnDescriptor("column2"));
hTable.addFamily(new HColumnDescriptor("column3"));
hbaseAdmin.createTable(hTable);
log.info(String.format("end create table %s", tableName.toString()));
}
}