HBase java API 的使用范例(增,删,查,扫描)

编辑pom.xml

<dependency>
  <groupId>org.apache.hbase</groupId>
  <artifactId>hbase-server</artifactId>
  <version>1.2.0</version>
</dependency>

<dependency>
  <groupId>org.apache.hbase</groupId>
  <artifactId>hbase-client</artifactId>
  <version>1.2.0</version>
</dependency>

java文件

package com.cenzhongman.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.util.Bytes;
import org.apache.hadoop.io.IOUtils;

/**
 * 
 * 演示 HBase 的 JDBC 连接及 CRUD 操作
 * 
 * @author cen
 *
 */

public class HBaseOperation {
	static HTable table = null;
	static String TABLE_NAME = "user";

	/**
	 * getHTablebyTbaleName
	 * 
	 * @param tableName
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static HTable getHTable() {
		// 1.get instance of Default Configuration 读取 core-site.xml hdfs-site.xml 信息
		// Configuration conf = new Configuration();//HDFS中获取core-site.xml hdfs-site.xml
		// 的方法
		// conf.addResource("hbase-default.xml");
		// conf.addResource("hbase-site.xml");
		// Hbase封装的获取方法(详见源码),增加了读取HBase 的配置文件
		Configuration conf = HBaseConfiguration.create();

		// 2.get Table instance
		try {
			table = new HTable(conf, TABLE_NAME);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return table;
	}

	/**
	 * getData by table,rowKey,columns[{cfs}][columns]
	 * 
	 * @param table
	 * @param rowKey
	 * @param columns
	 */
	public static void getData(String rowKey, String[][] columns) {

		try {
			table = getHTable();
			// create get with RowKey 选定Get的RowKey
			Get get = new Get(Bytes.toBytes(rowKey));

			if (columns != null) {
				// add column 增加列查询条件
				for (int i = 0; i < columns[0].length; i++) {
					for (int j = 0; j < columns[1].length; j++) {
						get.addColumn(Bytes.toBytes(columns[0][i]), Bytes.toBytes(columns[1][j]));
					}
				}
			}
			// get Result
			Result result = null;
			result = table.get(get);

			// Key:rowKwy + cf + c + version + type +
			// value:value
			for (Cell cell : result.rawCells()) {
				System.out.println(
						Bytes.toString(CellUtil.cloneFamily(cell)) + ":" + Bytes.toString(CellUtil.cloneQualifier(cell))
								+ "->" + Bytes.toString(CellUtil.cloneValue(cell)));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeStream(table);
		}
	}

	public static void getData(String rowKey) {
		getData(rowKey, null);
	}

	/**
	 * putData to HBase by table,rowKey,column,value 通常一张表的表名和列簇都设置常量 使用Map比数组更简单
	 * 
	 * @param table
	 * @param rowKey
	 * @param column
	 * @param value
	 */
	@SuppressWarnings("deprecation")
	public static void putData(String rowKey, String cf, String column, String value) {

		try {
			table = getHTable();
			// create put with rowKey
			Put put = new Put(Bytes.toBytes(rowKey));

			// 增加列数据
			put.add(Bytes.toBytes(cf), Bytes.toBytes(column), Bytes.toBytes(value));

			// put The data to put.
			// 实际使用list<Puts>
			table.put(put);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeStream(table);
		}
	}

	/**
	 * 删除一张表中的数据
	 * 
	 * @param table
	 * @param rowKey
	 * @param cf
	 * @param column
	 */
	public static void deleteData(String rowKey, String cf, String column) {
		// create Delete
		try {
			table = getHTable();
			Delete delete = new Delete(Bytes.toBytes(rowKey));

			// 需要 delete 的 data
			// delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(column));//删除最新的版本
			delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(column));// 删除所有的版本

			table.delete(delete);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeStream(table);
		}
	}

	/**
	 * scan data from HBase 包头不包尾
	 */
	public static void scanData() {
		scanData(null, null, null, null);
	}

	public static void scanData(String startRow) {
		scanData(startRow, null, null, null);
	}

	public static void scanData(String startRow, String stopRow) {
		scanData(startRow, stopRow, null, null);
	}

	public static void scanData(String startRow, String stopRow, String family, String qualifier) {
		ResultScanner resultScanner = null;

		try {
			Scan scan = new Scan();

			//三种方式
			//1.范围扫描可用构造函数来设置
//			Scan scan2 = new Scan(startRow, stopRow)
			
			//2.设置起始范围
			if (startRow != null) {
				scan.setStartRow(Bytes.toBytes(startRow));
			}
			if (stopRow != null) {
				scan.setStopRow(Bytes.toBytes(stopRow));
			}

			// 列过滤条件
			if (family != null) {
				if (qualifier != null) {
					scan.addFamily(Bytes.toBytes(family));
				}
				scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
			}
			
			//3.使用过滤器
			/*
			 * 使用Filter查询速度会下降
			 * PrefixFilter : 前缀过滤
			 * PageFilter : 分页过滤
			 */
//			scan.setFilter(filter);
			
			//设置缓存
//			scan.setCacheBlocks(cacheBlocks);//把常用的数据缓存到 RegionServer 的 BlocksCache 中
//			scan.setCaching(caching);//每一次扫描时获取列的数目

			table = getHTable();
			resultScanner = table.getScanner(scan);
			for (Result result : resultScanner) {
				System.out.println("Row:" + Bytes.toString(result.getRow()));
				for (Cell cell : result.rawCells()) {
					System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)) + ":"
							+ Bytes.toString(CellUtil.cloneQualifier(cell)) + "->"
							+ Bytes.toString(CellUtil.cloneValue(cell)));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeStream(resultScanner);
			IOUtils.closeStream(table);
		}

	}

	public static void main(String[] args) {
		String rowKey = "00002";
		String cf = "info";
		String column = "name";
		String value = "gugjhg";
		putData(rowKey, cf, column, value);

		// deleteData(rowKey, cf, column);

		// String[][] columns = { { "info" }, { "name" ,"tel"} };
		// getData(rowKey);

		scanData();
		System.out.println("finish");
	}
}
posted @ 2017-08-02 19:34  岑忠满  阅读(470)  评论(0编辑  收藏  举报