Hbase API 的简单使用

public class HbaseTableTest {
	private static Configuration conf;
	static TableName tableName = TableName.valueOf("hbase_table");
	static {
		conf = HBaseConfiguration.create();
	}

	public static void createTable() {
		Connection conn = null;
		HBaseAdmin admin = null;

		try {
			conn = ConnectionFactory.createConnection(conf);
			admin = (HBaseAdmin) conn.getAdmin();
			if (admin.tableExists(tableName)) {
				admin.disableTable(tableName);
				admin.deleteTable(tableName);
			}

			HTableDescriptor descTable = new HTableDescriptor(tableName);
			HColumnDescriptor family1 = new HColumnDescriptor("family1");
			HColumnDescriptor family2 = new HColumnDescriptor("family2");
			HColumnDescriptor family3 = new HColumnDescriptor("family3");
			descTable.addFamily(family1);
			descTable.addFamily(family2);
			descTable.addFamily(family3);
			admin.createTable(descTable);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeIO(conn);
			IOUtils.closeIO(admin);
		}
	}

	/**
	 * 插入单行数据
	 */
	public static void putSingleRow() {
		Connection conn = null;
		HTable table = null;

		try {
			conn = ConnectionFactory.createConnection(conf);
			table = (HTable) conn.getTable(tableName);
			Put put = new Put(Bytes.toBytes("20170610-1"));
			put.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
			put.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("age"), Bytes.toBytes("12"));
			put.addColumn(Bytes.toBytes("family2"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
			put.addColumn(Bytes.toBytes("family3"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"));
			table.put(put);

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeIO(conn);
			IOUtils.closeIO(table);
		}
	}

	/**
	 * 插入多行数据
	 */
	public static void putMoreRow() {
		Connection conn = null;
		HTable table = null;

		try {
			conn = ConnectionFactory.createConnection(conf);
			table = (HTable) conn.getTable(tableName);
			Put put1 = new Put(Bytes.toBytes("20170610-1"));
			put1.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
			put1.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("age"), Bytes.toBytes("12"));
			put1.addColumn(Bytes.toBytes("family2"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
			put1.addColumn(Bytes.toBytes("family3"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"));

			Put put2 = new Put(Bytes.toBytes("20170610-2"));
			put2.addColumn(Bytes.toBytes("family2"), Bytes.toBytes("age"), Bytes.toBytes("45"));
			put2.addColumn(Bytes.toBytes("family3"), Bytes.toBytes("age"), Bytes.toBytes("52"));

			List<Put> putList = new ArrayList<Put>();
			putList.add(put1);
			putList.add(put2);
			table.put(putList);

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeIO(conn);
			IOUtils.closeIO(table);
		}
	}

	/**
	 * 查询单行数据
	 */
	public static void querySingleRow() {
		Connection conn = null;
		HTable table = null;

		try {
			conn = ConnectionFactory.createConnection(conf);
			table = (HTable) conn.getTable(tableName);
			Get get = new Get(Bytes.toBytes("20170610-1"));
			// get.addFamily(Bytes.toBytes("family1"));
			Result result = table.get(get);
			Cell[] rawCells = result.rawCells();
			for (Cell cell : rawCells) {
				System.out.println("row=" + new String(CellUtil.cloneRow(cell)) + " family="
						+ new String(CellUtil.cloneFamily(cell)) + " Qualifier="
						+ new String(CellUtil.cloneQualifier(cell)) + " Value="
						+ new String(CellUtil.cloneValue(cell)));
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeIO(conn);
			IOUtils.closeIO(table);
		}
	}

	/**
	 * 查询多行
	 */
	public static void queryMoreRow() {
		Connection conn = null;
		HTable table = null;
		ResultScanner scanner = null;
		try {
			conn = ConnectionFactory.createConnection(conf);
			table = (HTable) conn.getTable(tableName);
			Scan scan = new Scan();
			// scan.setStartRow(Bytes.toBytes("20170610-1"));
			// scan.setStopRow(Bytes.toBytes("20170610-2"));
			scan.setStopRow(Bytes.toBytes("20170610-z")); // z 表示扫描到最后
			scanner = table.getScanner(scan);
			for (Result result : scanner) {
				Cell[] rawCells = result.rawCells();
				for (Cell cell : rawCells) {
					System.out.println("row=" + new String(CellUtil.cloneRow(cell)) + " family="
							+ new String(CellUtil.cloneFamily(cell)) + " Qualifier="
							+ new String(CellUtil.cloneQualifier(cell)) + " Value="
							+ new String(CellUtil.cloneValue(cell))+" Timestamp="+cell.getTimestamp());
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeIO(scanner);
			IOUtils.closeIO(conn);
			IOUtils.closeIO(table);
		}
	}
	
	/**
	 * 删除一行数据
	 */
	public static void deleteSingleRow() {
		Connection conn = null;
		HTable table = null;

		try {
			conn = ConnectionFactory.createConnection(conf);
			table = (HTable) conn.getTable(tableName);
			Delete delete = new Delete(Bytes.toBytes("20170610-2"));
			table.delete(delete);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeIO(conn);
			IOUtils.closeIO(table);
		}
	}

	
	/**
	 * 删除一列数据
	 * 如果一个数据有多个版本,只会删除最新的
	 */
	public static void deleteQualifier() {
		Connection conn = null;
		HTable table = null;

		try {
			conn = ConnectionFactory.createConnection(conf);
			table = (HTable) conn.getTable(tableName);
			Delete delete = new Delete(Bytes.toBytes("20170610-1"));
			delete.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("age"));
			table.delete(delete);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeIO(conn);
			IOUtils.closeIO(table);
		}
	}
	
	/**
	 * 删除列簇
	 */
	public static void deleteColumnFamily() {
		Connection conn = null;
		HBaseAdmin admin = null;

		try {
			conn = ConnectionFactory.createConnection(conf);
			admin = (HBaseAdmin) conn.getAdmin();
			admin.deleteColumn("hbase_table", "family3");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeIO(conn);
			IOUtils.closeIO(admin);
		}
	}
	
	public static void deleteTable() {
		Connection conn = null;
		HBaseAdmin admin = null;

		try {
			conn = ConnectionFactory.createConnection(conf);
			admin = (HBaseAdmin) conn.getAdmin();
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeIO(conn);
			IOUtils.closeIO(admin);
		}
	}
	public static void main(String[] args) {
//		 createTable();
//		 putSingleRow();
//		 putMoreRow();
//		 querySingleRow();
//		deleteSingleRow();
		//deleteQualifier();
//		deleteColumnFamily();
//		queryMoreRow();
		deleteTable();
	}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值