HBase經常使用的JAVA API操做

爲了方便之後查看,總結了一些經常使用的java操做hbase的代碼:java

package com.mcq;

import static org.hamcrest.CoreMatchers.describedAs;
import static org.hamcrest.CoreMatchers.nullValue;

import java.io.IOException;
import java.io.PushbackInputStream;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScannable;
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.Get;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class TestHbase {
	private static Admin admin = null;
	private static Connection connection = null;
	private static Configuration conf = null;
	static {
		// HBase配置文件
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "192.168.1.103");
		// 獲取鏈接對象
		try {
			connection = ConnectionFactory.createConnection(conf);
			// 獲取HBase管理員對象
			admin = connection.getAdmin();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void close(Connection conn, Admin admin) throws IOException {
		if (conn != null) {
			conn.close();
		}
		if (admin != null) {
			admin.close();
		}
	}

	// 判斷表是否存在
	public static boolean tableExist(String tableName) throws IOException {
		boolean tableExists = admin.tableExists(TableName.valueOf(tableName));
		return tableExists;
	}

	// 建立表
	public static void createTable(String tableName, String... cfs) throws IOException {
		if (tableExist(tableName)) {
			System.out.println("表已存在");
			return;
		}
		// cfs是列族,官方建議一個表一個,但能夠有多個
		// 建立表描述器
		HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
		for (String cf : cfs) {
			// 建立列描述器
			HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
//			hColumnDescriptor.setMaxVersions(3);//設置版本數
			hTableDescriptor.addFamily(hColumnDescriptor);
		}
		// 建立表操做
		admin.createTable(hTableDescriptor);
	}

	// 刪除表
	public static void deleteTable(String tableName) throws IOException {
		if (!tableExist(tableName)) {
			System.out.println("表不存在");
			return;
		}
		// 使表不可用(下線)
		admin.disableTable(TableName.valueOf(tableName));
		// 執行刪除操做
		admin.deleteTable(TableName.valueOf(tableName));
		System.out.println("表已刪除");
	}

	// 增、改
	public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
		// 獲取表對象
//		HTable table=new HTable(conf,TableName.valueOf(tableName)); 已過期
		Table table = connection.getTable(TableName.valueOf(tableName));
		// 建立put對象
		Put put = new Put(Bytes.toBytes(rowKey));
		// 添加數據
		put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
		// 執行添加操做
		table.put(put);
	}

	// 刪
	public static void delete(String tableName, String rowKey, String cf, String cn) throws IOException {
		// 獲取table對象
		Table table = connection.getTable(TableName.valueOf(tableName));
		// 建立delete對象
		Delete delete = new Delete(Bytes.toBytes(rowKey));// 刪除整個列族
		delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));// 刪除全部版本
//		delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));不推薦,只刪除最新的版本
		// 執行刪除操做
		table.delete(delete);
		table.close();
	}

	// 查——全表掃描,只能獲取最新版本
	public static void scanTable(String tableName) throws IOException {
		// 獲取table對象
		Table table = connection.getTable(TableName.valueOf(tableName));
		// 構建掃描器
		Scan scan = new Scan();
		ResultScanner resultScanner = table.getScanner(scan);

		// 遍歷數據並打印
		for (Result result : resultScanner) { // rowkey
			Cell[] cells = result.rawCells();
			for (Cell cell : cells) { // cell
				System.out.println("RK:" + Bytes.toString(CellUtil.cloneRow(cell)) + ",CF:"
						+ Bytes.toString(CellUtil.cloneFamily(cell)) + ",CN:"
						+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:"
						+ Bytes.toString(CellUtil.cloneValue(cell)));
			}
		}
		table.close();
	}

	// 查——獲取指定列族
	public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException {
		Table table = connection.getTable(TableName.valueOf(tableName));
		Get get = new Get(Bytes.toBytes(rowKey));
		get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
//		get.addFamily(cf);
//		get.setMaxVersions();//不傳參默認是表結構內的maxversions
		get.setMaxVersions(2);
		Result result = table.get(get);
		Cell[] cells = result.rawCells();
		for (Cell cell : cells) { // cell
			System.out.println("RK:" + Bytes.toString(CellUtil.cloneRow(cell)) + ",CF:"
					+ Bytes.toString(CellUtil.cloneFamily(cell)) + ",CN:"
					+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:"
					+ Bytes.toString(CellUtil.cloneValue(cell)));
		}
	}

	public static void main(String[] args) throws IOException {
		// 判斷表是否存在
//		System.out.println(tableExist("student"));
//		System.out.println(tableExist("staff"));

		// 建立表
//		createTable("staff", "info");
//		System.out.println(tableExist("staff"));

		// 刪除表
//		deleteTable("staff");

		// 增、改
//		putData("student", "1001", "info", "name", "mcq");

		// 刪
//		delete("student", "1001", "info", "name");

		// 查——全表掃描
//		scanTable("student");

		//查——獲取指定列族
		getData("student", "1001","info","name");
		
		// 關閉資源
		close(connection, admin);
	}
}
相關文章
相關標籤/搜索