package com.abchina.hbase.demo;

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

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.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;

public class HbaseAdmin {
	private static Logger logger = Logger.getLogger(HbaseAdmin.class);
	private static Configuration conf = null;
	private static HTablePool tablePool = null;
	private static final int poolsize = 100;
	static {

          //此处可以使用hbase的配置文件,也可以通过代码来实例化hbase连接
              
		/*
		 * Configuration HBASE_CONFIG = new Configuration();
		 * HBASE_CONFIG.set("hbase.zookeeper.quorum", "10.229.171.45");
		 * HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
		 * HBASE_CONFIG.set("hbase.zookeeper.quorum",
		 * "10.233.92.85,10.233.92.86,10.233.92.88");
		 * HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
		 * HBASE_CONFIG.set("zookeeper.znode.parent", "/hbase-unsecure"); conf =
		 * HBaseConfiguration.create(HBASE_CONFIG);
		 */
		conf = HBaseConfiguration.create();
		tablePool = new HTablePool(HBaseConfiguration.create(), poolsize);
		System.out.println("create connection success");
	}

	/**
	 * 根据主键查找一行记录
	 */
	public static Result findByRowKey(String tableName, String rowKey) {

		HTableInterface table = tablePool.getTable(tableName);
		Get get = new Get(Bytes.toBytes(rowKey));
		try {
			Result rs = table.get(get);
			if (rs != null) {
				for (KeyValue kv : rs.raw()) {
				/*	System.out.println(new String(kv.getRow()) + " ");
					System.out.println(new String(kv.getFamily()) + ":");
					System.out.println(new String(kv.getQualifier()) + " ");
					System.out.println(new String(kv.getValue()));*/
				}
			}
			return rs;
		} catch (IOException ioe) {
			logger.error("HbaseAdmin_" + "findByRowKey查询异常");
			logger.error(ioe.getStackTrace());
		} finally {
			try {
				table.close();
			} catch (IOException ioe) {
				logger.error("HbaseAdmin_" + "findByRowKey关闭table异常");
			}

		}
		return null;
	}

	/**
	 * get方式,通过rowKey、column查询
	 * 
	 * @param tablename
	 * @param rowKey
	 * @param column
	 * @throws IOException
	 */
	public static void findByRowKeyAndColumn(String tablename, String rowKey,
			String column) throws IOException {
		HTableInterface table = tablePool.getTable(tablename);
		Get get = new Get(Bytes.toBytes(rowKey));

		get.addColumn(Bytes.toBytes(rowKey), Bytes.toBytes(column));
		Result r = table.get(get);
		table.close();

		for (KeyValue kv : r.raw()) {
			System.out.println("column: " + kv.getFamilyLength());
			System.out.println("value: " + new String(kv.getValue()));
		}
	}

	/**
	 *  多条件检索
	 * @param tablename
	 * @param arr
	 *            每个字符串是一组判断条件: family,qualifier,value
	 * @throws IOException
	 */
	public static List<Result> findByFilter(String tablename, List<String> arr){
		HTableInterface table = tablePool.getTable(tablename);
		List<Result> retList = new ArrayList<Result>();
		FilterList filterList = new FilterList();
		Scan s1 = new Scan();
		for (String v : arr) { // 各个条件之间是“与”的关系
			String[] s = v.split(",");
			filterList.addFilter(new SingleColumnValueFilter(Bytes
					.toBytes(s[0]), Bytes.toBytes(s[1]), CompareOp.EQUAL, Bytes
					.toBytes(s[2])));
			// 添加下面这一行后,则只返回指定的cell,同一行中的其他cell不返回
			// s1.addColumn(Bytes.toBytes(s[0]), Bytes.toBytes(s[1]));
		}
		s1.setFilter(filterList);
		
		try{
			ResultScanner ResultScannerFilterList = table.getScanner(s1);
			table.close();
			for (Result rr = ResultScannerFilterList.next(); rr != null; 
			rr = ResultScannerFilterList.next()) {
				retList.add(rr);
			}
		}catch(Exception e){
			
		}
		return retList;
	}

	/**
	 * 通过rowkey范围检索
	 * @param tablename
	 * @param rowkey
	 * @param endkey
	 */
	public static List<Result> findByStartAndEndRowKey(String tablename,
			String rowkey, String endkey) {

		HTableInterface table = tablePool.getTable(tablename);

		Scan scan = new Scan();
		scan.setStartRow(Bytes.toBytesBinary(rowkey));
		scan.setStopRow(Bytes.toBytesBinary(endkey));

		ResultScanner scanner = null;
		List<Result> results = new LinkedList<Result>();
		try {
			scanner = table.getScanner(scan);
			for (Result result : scanner) {
				results.add(result);
			}
			System.out.println("results:" + results.size());
			return results;
		} catch (IOException ioe) {
			logger.error("System error!", ioe);
		} finally {
			if (scanner != null) {
				scanner.close();
			}
			try {
				table.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return null;

	}

	/**
	 * 显示所有数据
	 */
	public static void findAllData(String tablename) throws IOException {
		HTable table = new HTable(conf, tablename);
		Scan s = new Scan();
		ResultScanner rs = table.getScanner(s);
		if (rs == null) {
			System.exit(0);
		}
		for (Result r : rs) {
			for (KeyValue kv : r.raw()) {
				System.out.println(new String(kv.getRow()) + " ");
				System.out.println(new String(kv.getFamily()) + ":");
				System.out.println(new String(kv.getQualifier()) + " ");
				System.out.println(kv.getTimestamp() + " ");
				System.out.println(new String(kv.getValue()));
			}
		}
		table.close();
	}

	/**
	 * 插入一行记录
	 */
	public static void addRecord(String tablename, String rowKey,
			String family, String qualifier, String value) {
		try {
			HTableInterface table = tablePool.getTable(tablename);
			Put put = new Put(Bytes.toBytes(rowKey));
			put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes
					.toBytes(value));
			table.put(put);
			System.out.println("insert recored " + rowKey + " to table " + tablename
					+ " ok.");
			table.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 写入一行数据
	 * 
	 * @param tablename
	 * @param cfs
	 */
	public static void addRecord(String tablename, String[] cfs) {
		try {
			HTableInterface table = tablePool.getTable(tablename);
			Put put = new Put(Bytes.toBytes("rows1"));
			for (int j = 0; j < cfs.length; j++) {
				put.add(Bytes.toBytes(cfs[j]),
						Bytes.toBytes(String.valueOf(1)), Bytes
								.toBytes("value_1"));
				table.put(put);
				table.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除行
	 * 
	 * @param tablename
	 * @param rowkey
	 * @throws IOException
	 */
	public static void deleteRecordByRowKey(String tablename, String rowkey)
			throws IOException {
		HTableInterface table = tablePool.getTable(tablename);
		List<Delete> list = new ArrayList<Delete>();
		Delete d1 = new Delete(Bytes.toBytes(rowkey));
		list.add(d1);
		table.delete(list);
		table.close();
		System.out.println("删除行成功!");
	}

	

	/**
	 * 创建一张表
	 */
	public static void creatTable(String tablename) throws Exception {
		HBaseAdmin admin = new HBaseAdmin(conf);
		if (admin.tableExists(tablename)) {
			System.out.println("table   Exists!!!");
		} else {
			HTableDescriptor tableDesc = new HTableDescriptor(tablename);
			tableDesc.addFamily(new HColumnDescriptor("name:"));
			admin.createTable(tableDesc);
			System.out.println("create table ok, table name:" + tablename);
		}
	}

	public static void createTable(String tableName, String columnFamily)
			throws Exception {
		HBaseAdmin admin = new HBaseAdmin(conf);
		if (admin.tableExists(tableName)) {
			System.out.println("table already exists!");
			System.exit(0);
		} else {
			HTableDescriptor tableDesc = new HTableDescriptor(tableName);
			tableDesc.addFamily(new HColumnDescriptor(columnFamily + ":"));
			System.out.println("create table " + tableName);
		}
	}

	/**
	 * 创建一张表
	 */
	public static void creatTable(String tableName, String[] familys) {
		try {
			HBaseAdmin admin = new HBaseAdmin(conf);
			if (admin.tableExists(tableName)) {
				System.out.println("table already exists:" + tableName);
			} else {
				HTableDescriptor tableDesc = new HTableDescriptor(tableName);
				for (String column : familys) {
					tableDesc.addFamily(new HColumnDescriptor(column));
				}
				admin.createTable(tableDesc);
				System.out.println("create table:" + tableName + " success.");
			}

		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 删除表
	 */
	public static void deleteTable(String tableName) throws Exception {
		try {
			HBaseAdmin admin = new HBaseAdmin(conf);
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
			System.out.println("delete table " + tableName + " ok.");
		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			e.printStackTrace();
		}
	}

/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {

			testCreateTable2();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

         
       //

	public static void testCreateTable() {

		String tablename = "scores2";
		String[] familys = { "grade", "course" };
		try {
			HbaseAdmin.creatTable(tablename, familys);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	public static void testCreateTable2() throws Exception {
		String tablename = "scores1100208";
		String[] familys = { "grade", "course" };
		HbaseAdmin.creatTable(tablename, familys);

		// add record zkb
		HbaseAdmin.addRecord(tablename, "zkb", "grade", "", "5");
		HbaseAdmin.addRecord(tablename, "zkb", "course", "", "90");
		HbaseAdmin.addRecord(tablename, "zkb", "course", "math", "97");
		HbaseAdmin.addRecord(tablename, "zkb", "course", "art", "87");
		// add record baoniu
		HbaseAdmin.addRecord(tablename, "baoniu", "grade", "", "4");
		HbaseAdmin.addRecord(tablename, "baoniu", "course", "math", "89");

		System.out.println("===========get one record========");
		HbaseAdmin.findByRowKey(tablename, "zkb");

		System.out.println("===========show all record========");
		HbaseAdmin.findAllData(tablename);

		System.out.println("===========del one record========");
		HbaseAdmin.deleteRecordByRowKey(tablename, "baoniu");
		HbaseAdmin.findAllData(tablename);

		System.out.println("===========show all record========");
		HbaseAdmin.findAllData(tablename);
	}
}
最近下载更多
sunlzh888888  LV29 2023年3月3日
JingchuYize  LV1 2022年12月12日
小最迷弟  LV3 2022年5月16日
wsupsup  LV16 2021年5月21日
13940562934  LV22 2021年5月16日
sinbero  LV8 2020年11月10日
zuiwomengjiaren  LV11 2020年9月4日
newhaijun  LV15 2020年5月15日
ncd12320  LV8 2020年1月12日
luohaipeng  LV23 2019年11月20日
最近浏览更多
微微qwdrq  LV3 2023年9月21日
穿山甲1001  LV6 2023年6月25日
暂无贡献等级
sunlzh888888  LV29 2023年3月3日
JingchuYize  LV1 2022年12月12日
小最迷弟  LV3 2022年5月16日
xiaoding1999  LV7 2021年8月5日
871609222  LV3 2021年6月24日
wsupsup  LV16 2021年5月21日
13940562934  LV22 2021年5月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友