用JAVA的API操作HBASE

我叫阿狸貓發表於2014-01-05
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.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;

public class HBaseApp {
	private static final String TABLE_NAME = "xxc";
	private static final String FAMILY_NAME = "family1";
	private static final String ROW_KEY = "rowkey1";

	// 建立表、刪除表、插入記錄、查詢一條記錄、遍歷所有記錄
	public static void main(String[] args) throws IOException {
		// 配置資訊
		Configuration conf = HBaseConfiguration.create();
		// 設定HBASE在HDFS上的儲存路徑
		conf.set("hbase.rootdir", "hdfs://xxc:9000/hbase");
		// 使用eclipse時必須新增這個,否則無法定位 使用zookepper定位HBASE
		conf.set("hbase.zookeeper.quorum", "xxc");

		// 1.建立表、刪除表使用HBaseAdmin
		HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
		// 如果表不存在則建立表
		createTable(hBaseAdmin);
		// 刪除表
		// deleteTable(hBaseAdmin);

		// 2.插入記錄、查詢一條記錄、遍歷所有記錄 使用HTable
		HTable hTable = new HTable(conf, TABLE_NAME);
		deleteRecord(hTable);//刪除一條記錄
		//putRecord(hTable);//插入一條記錄
		// getRecord(hTable);//獲取一條記錄
	//	scanTable(hTable);//遍歷整張表
		hTable.close();
	}

	private static void deleteRecord(HTable hTable) throws IOException {
		
		Delete delete = new Delete(ROW_KEY.getBytes());//引數是行鍵
		delete.deleteColumn(FAMILY_NAME.getBytes(), "age".getBytes());//引數是列族,列名
		hTable.delete(delete);
	}
	
	/**
	 * 遍歷整張表 
	 */
	private static void scanTable(HTable hTable) throws IOException {
		Scan scan = new Scan(); 
		ResultScanner scanner = hTable.getScanner(scan);
		for (Result result : scanner) {
			byte[] value = result.getValue(FAMILY_NAME.getBytes(), "age".getBytes());//列族,列名
			System.out.println(new String(value));
		}
	}

	/**
	 * 獲取一條記錄
	 */
	private static void getRecord(HTable hTable) throws IOException {
		Get get = new Get(ROW_KEY.getBytes());//引數是行鍵
		Result result = hTable.get(get);
		byte[] value = result.getValue(FAMILY_NAME.getBytes(), "age".getBytes());//列族,列名
		System.out.println(result + "\t" + new String(value));
	}

	/**
	 * 插入一條記錄
	 */
	private static void putRecord(HTable hTable) throws IOException {
		Put put = new Put(ROW_KEY.getBytes());//引數是行鍵
		put.add(FAMILY_NAME.getBytes(), "age".getBytes(), "20".getBytes());//設定列族,列名,值
		hTable.put(put);
	}

	/** 
	 * 刪除表
	 */
	private static void deleteTable(HBaseAdmin hBaseAdmin) throws IOException {
		hBaseAdmin.disableTable(TABLE_NAME);// 刪除表之前需要禁用表,否則刪除失敗
		hBaseAdmin.deleteTable(TABLE_NAME);
	}

	/**
	 * 新增表
	 */
	private static void createTable(HBaseAdmin hBaseAdmin) throws IOException {
		if (!hBaseAdmin.tableExists(TABLE_NAME)) {
			HTableDescriptor hd = new HTableDescriptor(TABLE_NAME);// 實參是表名稱
			HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);//建立列族 實參是列族名稱 
			hd.addFamily(family);//將列族新增到表上
			hBaseAdmin.createTable(hd);
		}
	}
}

相關文章