Hbase JAVA API 编程

1.hbase 的API编程

1.1进行kerberos认证获取连接

/**
	 * 进行kerberos认证获取连接
	 * @return
	 * @throws IOException
	 * @throws InterruptedException
	 */
	private static Connection getConnection() throws IOException, InterruptedException {

		String user = "bdp/admin@NBDP.COM";
		String keytabPath = "D:\\bdp.keytab";
		String realm = "NBDP.COM";
		String kdc = "host165";

		System.setProperty("sun.security.krb5.debug", "false");
		System.setProperty("java.security.krb5.kdc", kdc);
		System.setProperty("java.security.krb5.realm", realm);

		final Configuration conf = HBaseConfiguration.create();
		//设置连接信息
		conf.set("hbase.zookeeper.quorum", "172.21.72.165");
		conf.set("hbase.zookeeper.property.clientPort", "2181");
		conf.set("hbase.client.retries.number", "2");

		//设置kerberos认证配置
		conf.set("hadoop.security.authentication", "kerberos");
		conf.set("hadoop.security.authorization", "true");
		conf.set("hbase.security.authentication", "kerberos");
		conf.set("hbase.security.authorization", "true");
		conf.set("hbase.master.kerberos.principal", "hbase/_HOST@" + realm);
		conf.set("hbase.regionserver.kerberos.principal", "hbase/_HOST@" + realm);
		conf.set("hbase.thrift.kerberos.principal", "hbase/_HOST@" + realm);

		UserGroupInformation.setConfiguration(conf);
		UserGroupInformation userGroupInformation = UserGroupInformation.loginUserFromKeytabAndReturnUGI(user,
				keytabPath);
		UserGroupInformation.setLoginUser(userGroupInformation);

		boolean hasKerberosCredentials = userGroupInformation.hasKerberosCredentials();
		if (!hasKerberosCredentials) {
			throw new RuntimeException("认证失败!");
		}

		Connection connection = userGroupInformation.doAs(new PrivilegedExceptionAction<Connection>() {
			@Override
			public Connection run() throws Exception {
				//获取连接
				Connection connection = ConnectionFactory.createConnection(conf);
				return connection;
			}
		});

		return connection;
	}

1.2创建hbase命名空间,即库名

	/**
	 * 创建hbase命名空间
	 * @param connection
	 * @throws IOException
	 */
	public static void createSchema(Connection connection) throws IOException {

		String namespace = "smart_test";

		Admin admin = connection.getAdmin();

		NamespaceDescriptor[] namespaceDescriptorList = admin.listNamespaceDescriptors();
		for (int i = 0; i < namespaceDescriptorList.length; i++) {
			System.out.println("当前存在的命名空间是:" + namespaceDescriptorList[i].getName());
		}

		NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(namespace).build();
		admin.createNamespace(namespaceDescriptor);
	}

1.3创建hbase表

	/**
	 * 创建HBASE表
	 * @param connection
	 * @throws IOException
	 */
	public static void createTable(Connection connection) throws IOException {
		Admin admin = connection.getAdmin();

		TableName myuser = TableName.valueOf("smart_test:user");
		//添加列族
		HTableDescriptor hTableDescriptor = new HTableDescriptor(myuser);
		HColumnDescriptor info = new HColumnDescriptor("info");
		HColumnDescriptor course = new HColumnDescriptor("course");
		hTableDescriptor.addFamily(info);
		hTableDescriptor.addFamily(course);
		//创建表
		admin.createTable(hTableDescriptor);
	}

1.4删除hbase表

	/**
	 * 删除删除hbase表
	 * @param connection
	 * @throws IOException
	 */
	public static void deleteTable(Connection connection) throws IOException {
		Admin admin = connection.getAdmin();
		admin.disableTable(TableName.valueOf("smart_test:user"));
		admin.deleteTable(TableName.valueOf("smart_test:user"));
	}

1.5单条记录插入数据

	/**
	 * 单条记录插入数据
	 * @param connection
	 * @throws IOException
	 */
	public static void insert(Connection connection) throws IOException {

		Table table = connection.getTable(TableName.valueOf("student"));
		Put put = new Put(Bytes.toBytes("7"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("小杜"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("19"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("洛溪大路二街403"));
		table.put(put);

	}

1.6批量插入数据

	/**
	 * 批量插入数据
	 * @param connection
	 * @throws IOException
	 */
	public static void batchInsert(Connection connection) throws IOException {

		Table table = connection.getTable(TableName.valueOf("student"));

		List<Put> list = new ArrayList<Put>();
		Put put = new Put(Bytes.toBytes("8"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("小黄"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("17"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("广州市会江镇"));
		list.add(put);

		Put put2 = new Put(Bytes.toBytes("9"));
		put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("小杜"));
		put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(20));
		put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("洛溪大路中心二街2栋403"));
		list.add(put2);

		table.put(list);

	}

1.7单条记录查询

	/**
	 * 根据rowKey进行查询
	 * @param connection
	 * @param rowKey
	 * @throws IOException
	 */
	public static void queryByRowKey(Connection connection, String rowKey) throws IOException {
		Table table = connection.getTable(TableName.valueOf("student"));
		Get get = new Get("1".getBytes());
		Result result = table.get(get);
		KeyValue[] raw = result.raw();
		for (KeyValue kv : raw) {
			System.out.print(new String(kv.getRow()) + " ");
			System.out.print(new String(kv.getFamily()) + ":");
			System.out.print(new String(kv.getQualifier()) + " = ");
			System.out.print(new String(kv.getValue()));
			System.out.print(" timestamp = " + kv.getTimestamp() + "\n");
		}
	}

1.8批量查询


	/**
	 * 批量查询
	 * @param connection
	 * @throws Exception
	 */
	public static void batchQuery(Connection connection) throws Exception {

		Table table = connection.getTable(TableName.valueOf("student"));
		Scan scan = new Scan();
		scan.setStartRow("1".getBytes());
		scan.setStopRow("9".getBytes());
		ResultScanner scanner = table.getScanner(scan);
		for (Result row : scanner) {
			byte[] rowKeyBytes = row.getRow();
			byte[] nameBytes = row.getValue("info".getBytes(), "name".getBytes());
			byte[] ageBytes = row.getValue("info".getBytes(), "age".getBytes());
			byte[] addressBytes = row.getValue("info".getBytes(), "address".getBytes());

			String rowKey = Bytes.toString(rowKeyBytes);
			String name = Bytes.toString(nameBytes);
			String age = Bytes.toString(ageBytes);
			String address = Bytes.toString(addressBytes);

			System.out.print(rowKey);
			System.out.print(name);
			System.out.print(age);
			System.out.print(address);
			System.out.println();

		}
	}

1.9把查询结果封装成Map返回

	/**
	 * 把查询结果封装成Map
	 * @param connection
	 * @throws Exception
	 */
	public static void batchQueryMap(Connection connection) throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Table table = connection.getTable(TableName.valueOf("student"));
		Scan scan = new Scan();
		scan.setStartRow("1".getBytes());
		scan.setStopRow("9".getBytes());
		ResultScanner scanner = table.getScanner(scan);
		for (Result result : scanner) {
			Cell[] rawCells = result.rawCells();

			Map<String, Object> map = new HashMap<String, Object>();
			for (Cell cell : rawCells) {
				byte[] rowKeyBytes = cell.getRow();
				byte[] qualifierBytes = cell.getQualifier();
				byte[] valueBytes = cell.getValue();

				String rowKey = Bytes.toString(rowKeyBytes);
				String qualifier = Bytes.toString(qualifierBytes);
				String value = Bytes.toString(valueBytes);

				map.put("rowKey", rowKey);
				map.put(qualifier, value);

			}
			list.add(map);
		}

		System.out.println(list);
	}

1.10删除记录

	/**
	 * 删除记录
	 * @param connection
	 * @throws IOException
	 */
	public static void deleteByRowKey(Connection connection) throws IOException {
		Table table = connection.getTable(TableName.valueOf("student"));
		Delete del = new Delete(Bytes.toBytes("9"));
		table.delete(del);
	}

1.11 RowFilter使用正则过滤查询

	/**
	 * RowFilter过滤器查询
	 * @param connection
	 * @throws Exception
	 */
	public static void batchQueryRowFilter(Connection connection) throws Exception {

		//匹配以1开头的ROWKEY
		RowFilter rowFilter = new RowFilter(RowFilter.CompareOp.EQUAL, new RegexStringComparator("^1"));

		Table table = connection.getTable(TableName.valueOf("student"));
		Scan scan = new Scan();
		scan.setStartRow("1".getBytes());
		scan.setStopRow("9".getBytes());
		scan.setFilter(rowFilter);

		ResultScanner scanner = table.getScanner(scan);
		for (Result row : scanner) {
			byte[] rowKeyBytes = row.getRow();
			byte[] nameBytes = row.getValue("info".getBytes(), "name".getBytes());
			byte[] ageBytes = row.getValue("info".getBytes(), "age".getBytes());
			byte[] addressBytes = row.getValue("info".getBytes(), "address".getBytes());

			String rowKey = Bytes.toString(rowKeyBytes);
			String name = Bytes.toString(nameBytes);
			String age = Bytes.toString(ageBytes);
			String address = Bytes.toString(addressBytes);

			System.out.print(rowKey);
			System.out.print(name);
			System.out.print(age);
			System.out.print(address);
			System.out.println();

		}
	}

1.12多条件过滤器查询

	/**
	 * @param connection
	 * @throws Exception
	 */
	public static void batchQueryFilter(Connection connection) throws Exception {
		//多列值前缀过滤器
		byte[][] prefixes = new byte[][] { Bytes.toBytes("name"), Bytes.toBytes("age"), Bytes.toBytes("address") };
		MultipleColumnPrefixFilter multipleColumnPrefixFilter = new MultipleColumnPrefixFilter(prefixes);
		//单列值过滤器,过滤出地址包含luoxi的
		SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("info"),
				Bytes.toBytes("address"), CompareOp.EQUAL, Bytes.toBytes("luoxi"));

		FilterList filterList = new FilterList(Operator.MUST_PASS_ALL);
		filterList.addFilter(singleColumnValueFilter);
		filterList.addFilter(multipleColumnPrefixFilter);
		Table table = connection.getTable(TableName.valueOf("student"));
		Scan scan = new Scan();
		scan.setStartRow("1".getBytes());
		scan.setStopRow("9".getBytes());
		scan.setFilter(filterList);

		ResultScanner scanner = table.getScanner(scan);
		for (Result row : scanner) {
			byte[] rowKeyBytes = row.getRow();
			byte[] nameBytes = row.getValue("info".getBytes(), "name".getBytes());
			byte[] ageBytes = row.getValue("info".getBytes(), "age".getBytes());
			byte[] addressBytes = row.getValue("info".getBytes(), "address".getBytes());

			String rowKey = Bytes.toString(rowKeyBytes);
			String name = Bytes.toString(nameBytes);
			String age = Bytes.toString(ageBytes);
			String address = Bytes.toString(addressBytes);

			System.out.print(rowKey);
			System.out.print(name);
			System.out.print(age);
			System.out.print(address);
			System.out.println();

		}
	}

 

<think>好的,用户想要查找关于HBase Java API编程实践和使用指南,包括示例代码。首先,我需要回顾之前的引用内容,看看有哪些相关的信息可以整合。 引用[1]提到将HBase Java API的操作封装成工具类,提高代码的可维护性。引用[2]和[3]提供了编程示例,对应HBase Shell的命令,如create、put、get等。引用[4]则涉及环境启动步骤。用户需要具体的实践指导和代码示例,所以应该结合这些引用内容,整理出步骤化的教程。 接下来,我需要确定用户可能的背景。他们可能已经安装了Hadoop和HBase,但需要确认环境配置是否正确。然后,用户可能需要从创建表、插入数据到查询等基本操作,以及如何封装工具类。此外,用户可能还关心代码结构和最佳实践,比如如何解耦工具类和业务逻辑,这点引用[1]已经提到。 需要确保示例代码符合Java API的使用方法,比如正确使用Configuration对象、Connection的管理,以及各个类如Admin、Table的正确用法。同时,要提醒用户异常处理和资源关闭,避免连接泄漏。 可能还需要注意版本兼容性,但引用中没有提到具体版本,所以保持通用。最后,用户可能需要进一步的问题,比如性能优化或高级功能,所以相关问题的部分需要涵盖这些方面。</think>### HBase Java API编程实践指南 #### 一、环境准备(前置条件) 1. 启动HDFS和HBase服务(需配置环境变量) ```shell # 根据引用[4]的操作指引 cd /usr/local/hadoop ./sbin/start-dfs.sh cd /usr/local/hbase ./bin/start-hbase.sh ``` 2. Maven依赖配置(核心依赖项) ```xml <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.4.9</version> </dependency> ``` #### 二、工具类封装实践(引用[1]最佳实践) ```java public class HBaseJavaAPIUtils { private static Connection connection; // 初始化连接池 static { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "node1,node2,node3"); try { connection = ConnectionFactory.createConnection(config); } catch (IOException e) { e.printStackTrace(); } } // 创建表方法 public static void createTable(String tableName, String... colFamilies) throws IOException { try (Admin admin = connection.getAdmin()) { TableDescriptorBuilder tableDesc = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)); for (String cf : colFamilies) { ColumnFamilyDescriptorBuilder colDesc = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)); tableDesc.setColumnFamily(colDesc.build()); } admin.createTable(tableDesc.build()); } } // 其他方法(put/get/scan等)按相同模式封装 } ``` #### 三、基础操作示例(对应Shell命令实现) 1. **创建表**(对应`create`命令) ```java public class HBaseDemo { public static void main(String[] args) throws IOException { HBaseJavaAPIUtils.createTable("Student", "Info", "Grades"); } } ``` 2. **插入数据**(对应`put`命令) ```java public static void putData(String tableName, String rowKey, String colFamily, String qualifier, String value) throws IOException { try (Table table = connection.getTable(TableName.valueOf(tableName))) { Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(qualifier), Bytes.toBytes(value)); table.put(put); } } ``` 3. **查询数据**(对应`get`命令) ```java public static Result getData(String tableName, String rowKey) throws IOException { try (Table table = connection.getTable(TableName.valueOf(tableName))) { Get get = new Get(Bytes.toBytes(rowKey)); return table.get(get); } } ``` #### 四、工程化实践要点 1. **连接管理**:使用单例模式维护Connection对象(创建成本高) 2. **资源释放**:所有`Table`和`Admin`对象必须放在try-with-resources语句中 3. **字节转换**:统一使用`Bytes.toBytes()`进行数据类型转换 4. **异常处理**:建议封装自定义异常体系(如HBaseOpsException) #### 五、调试与验证(引用[3]方法) 1. 在Eclipse/IntelliJ中通过`Run As > Java Application`执行程序 2. 使用HBase Shell验证操作结果: ```shell hbase> list hbase> scan 'Student' ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值