1. 程式人生 > >hbase程式設計:通過Java api操作hbase

hbase程式設計:通過Java api操作hbase

轉:http://www.aboutyun.com/thread-7151-1-1.html

http://blog.csdn.net/cnweike/article/details/42920547

http://blog.csdn.net/zwx19921215/article/details/41820199

package com.hbase;

import java.io.IOException;
import java.util.ArrayList;

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.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.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class HTest {

	private static Configuration config = null;
	private static Connection connection = null;
	private static Table table = null;

	public static void main(String[] args) throws Exception {
		config = HBaseConfiguration.create();
		
		config.set("hbase.zookeeper.quorum", "hellotest");//單機
		
//		config.set("hbase.zookeeper.quorum", "master,work1,work2");// zookeeper地址
//		config.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper埠
		connection = ConnectionFactory.createConnection(config);
		table = connection.getTable(TableName.valueOf("user"));

		// testCrateTable();
		// testDropTable();
		// insertDate();
		// insertBatch();
		// uodateData();
		// deleteData();
		// queryData();
		// scanDataByFilter1();
		scanDataByFilter3();
		table.close();
		connection.close();
	}

	public static void testCrateTable() throws IOException {
		// 表管理類
		Admin admin = connection.getAdmin();// hbase表管理類
		TableName tableName = TableName.valueOf("user");
		HTableDescriptor descriptor = new HTableDescriptor(tableName);
		// 建立列簇的描述類
		HColumnDescriptor family = new HColumnDescriptor("info");
		// 將列簇新增到表中
		descriptor.addFamily(family);
		// 建立列簇的描述類
		HColumnDescriptor family2 = new HColumnDescriptor("age");
		// 將列簇新增到表中
		descriptor.addFamily(family2);
		admin.createTable(descriptor);
		admin.close();
	}

	public static void testDropTable() throws IOException {
		// 表管理類
		Admin admin = connection.getAdmin();// hbase表管理類
		TableName tableName = TableName.valueOf("user");
		admin.disableTable(tableName);
		admin.deleteTable(tableName);
		admin.close();
		System.out.println("刪除表成功");
	}

	public static void insertData() throws IOException {
		// 表管理類
		Put put = new Put(Bytes.toBytes("wangwu_1"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("wangwang"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(50));
		table.put(put);
		System.out.println("新增成功");
	}

	public static void insertBatch() throws IOException {

		ArrayList<Put> arrayList = new ArrayList<Put>();
		for (int i = 21; i < 50; i++) {
			Put put = new Put(Bytes.toBytes("1234" + i));
			put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("wangwu" + i));
			put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes(1234 + i));
			arrayList.add(put);
		}

		// 插入資料
		table.put(arrayList);
	}

	/**
	 * 修改資料 相同 rowkey 列簇 column 會覆蓋新值
	 * 
	 * @throws Exception
	 */

	public static void uodateData() throws Exception {
		Put put = new Put(Bytes.toBytes("wangwu_1"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lisi1234"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(23));
		// 插入資料
		table.put(put);

	}

	/**
	 * 刪除資料
	 * 
	 * @throws IOException
	 */
	public static void deleteData() throws IOException {
		// 按rowkey刪除
		Delete delete = new Delete(Bytes.toBytes("123434"));
		table.delete(delete);

		// 按family刪除
		Delete delete2 = new Delete(Bytes.toBytes("123435"));
		delete2.addFamily(Bytes.toBytes("info"));
		table.delete(delete2);

		// 按column刪除
		Delete delete3 = new Delete(Bytes.toBytes("123436"));
		delete3.addColumns(Bytes.toBytes("info"), Bytes.toBytes("password"));
		table.delete(delete3);

		System.out.println("刪除資料成功");
	}

	public static void queryData() throws IOException {
		Get get = new Get(Bytes.toBytes("123449"));
		Result result = table.get(get);
		System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));
		System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));

	}

	/**
	 * 全表掃描
	 * 
	 * @throws Exception
	 */

	public static void scanData() throws Exception {
		Scan scan = new Scan();
		// scan.addFamily(Bytes.toBytes("info")); //掃描指定family
		// scan.addColumn(Bytes.toBytes("info"),
		// Bytes.toBytes("password"));//掃描column指定

		// 按rowkey字典序掃描
		// scan.setStartRow(Bytes.toBytes("wangsf_0"));
		// scan.setStopRow(Bytes.toBytes("wangwu"));
		ResultScanner scanner = table.getScanner(scan);
		for (Result result : scanner) {
			System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));
			System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));
			// System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info2"),
			// Bytes.toBytes("password"))));
			// System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info2"),
			// Bytes.toBytes("name"))));
		}
	}

	/**
	 * 全表掃描的過濾器 列值過濾器
	 * 
	 * @throws Exception
	 */

	public static void scanDataByFilter1() throws Exception {

		// 建立全表掃描的scan
		Scan scan = new Scan();
		// 過濾器:列值過濾器
		SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("name"),
				CompareOp.EQUAL, Bytes.toBytes("lisi1234"));
		// 設定過濾器
		scan.setFilter(filter);

		// 列印結果集
		ResultScanner scanner = table.getScanner(scan);
		for (Result result : scanner) {
			System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));
			System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"))));
			// System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info2"),
			// Bytes.toBytes("password"))));
			// System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info2"),
			// Bytes.toBytes("name"))));
		}

	}

	/**
	 * rowkey過濾器
	 * 
	 * @throws Exception
	 */
	public static void scanDataByFilter2() throws Exception {

		// 建立全表掃描的scan
		Scan scan = new Scan();
		// 匹配rowkey以wangsenfeng開頭的
		RowFilter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^123"));
		// 設定過濾器
		scan.setFilter(filter);
		// 列印結果集
		ResultScanner scanner = table.getScanner(scan);
		for (Result result : scanner) {
			System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));
			System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));
			// System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info2"),
			// Bytes.toBytes("password"))));
			// System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info2"),
			// Bytes.toBytes("name"))));
		}

	}

	/**
	 * 匹配列名字首
	 * 
	 * @throws Exception
	 */

	public static void scanDataByFilter3() throws Exception {

		// 建立全表掃描的scan
		Scan scan = new Scan();
		// 匹配rowkey以wangsenfeng開頭的
		ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("na7"));
		// 設定過濾器
		scan.setFilter(filter);
		// 列印結果集
		ResultScanner scanner = table.getScanner(scan);
		for (Result result : scanner) {
			System.out.println("rowkey:" + Bytes.toString(result.getRow()));
			System.out.println(
					"info:name:" + Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));
			// 判斷取出來的值是否為空
			if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")) != null) {
				System.out.println(
						"info:age:" + Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"))));
			}
			// 判斷取出來的值是否為空
			if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")) != null) {
				System.out.println(
						"infi:sex:" + Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex"))));
			}
			// 判斷取出來的值是否為空
			if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")) != null) {
				System.out.println(
						"info2:name:" + Bytes.toString(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name"))));
			}
			// 判斷取出來的值是否為空
			if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age")) != null) {
				System.out.println(
						"info2:age:" + Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age"))));
			}
			// 判斷取出來的值是否為空
			if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex")) != null) {
				System.out.println(
						"info2:sex:" + Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex"))));
			}
		}

	}

	/**
	 * 過濾器集合
	 * 
	 * @throws Exception
	 */

	public static void scanDataByFilter4() throws Exception {

		// 建立全表掃描的scan
		Scan scan = new Scan();
		// 過濾器集合:MUST_PASS_ALL(and),MUST_PASS_ONE(or)
		FilterList filterList = new FilterList(Operator.MUST_PASS_ONE);
		// 匹配rowkey以wangsenfeng開頭的
		RowFilter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^wangsenfeng"));
		// 匹配name的值等於wangsenfeng
		SingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("name"),
				CompareOp.EQUAL, Bytes.toBytes("zhangsan"));
		filterList.addFilter(filter);
		filterList.addFilter(filter2);
		// 設定過濾器
		scan.setFilter(filterList);
		// 列印結果集
		ResultScanner scanner = table.getScanner(scan);
		for (Result result : scanner) {
			System.out.println("rowkey:" + Bytes.toString(result.getRow()));
			System.out.println(
					"info:name:" + Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));
			// 判斷取出來的值是否為空
			if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")) != null) {
				System.out.println(
						"info:age:" + Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"))));
			}
			// 判斷取出來的值是否為空
			if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")) != null) {
				System.out.println(
						"infi:sex:" + Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex"))));
			}
			// 判斷取出來的值是否為空
			if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")) != null) {
				System.out.println(
						"info2:name:" + Bytes.toString(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name"))));
			}
			// 判斷取出來的值是否為空
			if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age")) != null) {
				System.out.println(
						"info2:age:" + Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age"))));
			}
			// 判斷取出來的值是否為空
			if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex")) != null) {
				System.out.println(
						"info2:sex:" + Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex"))));
			}
		}

	}

}