HBase看完这篇你还有什么不懂?(HBase API-1.2常用类及java代码实现)

一、基本类

1、Admin类

管理可以用于创建、删除、列出、启用和禁用表、添加和删除表列族以及其他管理操作。

2、Connection类

一个集群连接,封装了与实际服务器的低级别单独连接以及与zookeeper的连接。 连接通过ConnectionFactory类实例化。 连接的生命周期由调用者管理,使用后必须调用close()连接以释放资源。

3、Table类

用于与单个HBase表通信。 从Connection获取实例并在之后调用close()。

表可用于从表中Get,Put,Delete或Scan数据。

4、Put类

用于执行单行的Put操作。

要执行Put,请实例化一个Put对象,其中要插入的行和要插入的每个列,如果设置时间戳,请执行add。

5、Get类

用于对单行执行Get操作。

获取行的所有内容,请使用要获取的行实例化Get对象。 要进一步缩小获取内容的范围,请使用以下方法。

1、获取特定系列的所有列,请为要检索的每个系列执行addFamily()

2、获取特定列,请为要检索的每个列执行addColumn()

3、仅检索特定版本时间戳范围内的列,请执行setTimeRange()

4、仅检索具有特定时间戳的列,请执行setTimestamp()

5、限制要返回的每个列的版本数,请执行setMaxVersions()

6、添加过滤器,请调用setFilter()

6、Scan类

用于执行扫描操作。

除实例化外,所有操作都与Get相同。 可以定义可选的startRow和stopRow,而不是指定单个行。 如果未指定行,则扫描程序将遍历所有行。

1、从Table的所有行获取所有列,请创建一个没有约束的实例; 使用Scan()构造函数。 要将扫描约束到特定列族,请为扫描实例上的每个族调用addFamily()

2、获取特定列,请为要检索的每列调用addColumn()

3、仅检索特定版本时间戳范围内的列,请调用setTimeRange()

4、仅检索具有特定时间戳的列,请调用setTimestamp()

5、限制要返回的每列的版本数,请调用setMaxVersions()

6、限制每次调用next()返回的最大值数,请调用setBatch()

二、java代码实现

package com.dashujuxiansheng.hbase;
//更多大数据知识请点击大数据先生的博客了解:https://blog.csdn.net/weixin_42312342
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.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.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.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HBaseDemo {

	Admin admin = null;
	Connection conn = null;
	TableName tn = TableName.valueOf("phone");
	Table table = null;

	@Before
	public void init() throws Exception {
		// 创建配置文件对象
		Configuration conf = new Configuration();
		// 添加连接zookeeper的属性
		conf.set("hbase.zookeeper.quorum", "node001,node002,node003");
		// 通过工厂类创建连接
		conn = ConnectionFactory.createConnection(conf);
		// 获取表的操作对象
		admin = conn.getAdmin();
		table = conn.getTable(tn);
	}

	/**
	 * 创建表
	 * 
	 * @throws Exception
	 */
	@Test
	public void createTable() throws Exception {
		// 表描述对象
		HTableDescriptor desc = new HTableDescriptor(tn);
		// 列族描述对象
		HColumnDescriptor family = new HColumnDescriptor("cf".getBytes());
		desc.addFamily(family);
		// 创建
		if (admin.tableExists(tn)) {
                  //禁用表,再删除
			admin.disableTable(tn);
			admin.deleteTable(tn);
		}
		admin.createTable(desc);
	}

	/**
	 * 插入数据
	 * 
	 * @throws Exception
	 */
	@Test
	public void insertDB() throws Exception {
		Put put = new Put("2222".getBytes());
		put.addColumn("cf".getBytes(), "name".getBytes(), "lisi".getBytes());
		put.addColumn("cf".getBytes(), "age".getBytes(), "124".getBytes());
		put.addColumn("cf".getBytes(), "gender".getBytes(), "women".getBytes());
		table.put(put);
	}

	/**
	 * 获取数据
	 * 
	 * @throws Exception
	 */
	@Test
	public void get() throws Exception {
		Get get = new Get("1111".getBytes());
		get.addColumn("cf".getBytes(), "name".getBytes());
		get.addColumn("cf".getBytes(), "age".getBytes());
		get.addColumn("cf".getBytes(), "gender".getBytes());
		Result rs = table.get(get);
		// System.out.println(Bytes.toString((rs.getValue("cf".getBytes(),
		// "name".getBytes()))));
		// System.out.println(Bytes.toString((rs.getValue("cf".getBytes(),
		// "age".getBytes()))));
		// System.out.println(Bytes.toString((rs.getValue("cf".getBytes(),
		// "gender".getBytes()))));
		Cell cell1 = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());
		Cell cell2 = rs.getColumnLatestCell("cf".getBytes(), "age".getBytes());
		Cell cell3 = rs.getColumnLatestCell("cf".getBytes(), "gender".getBytes());
		String name = Bytes.toString(CellUtil.cloneValue(cell1));
		String age = Bytes.toString(CellUtil.cloneValue(cell2));
		String gender = Bytes.toString(CellUtil.cloneValue(cell3));
		System.out.println(name + "--" + age + "--" + gender);
	}
	
	/**
	 * 查询全部数据
	 * @throws Exception 
	 */
	@Test
	public void scan() throws Exception{
		Scan scan = new Scan();
//		scan.setStartRow(startRow);
//		scan.setStopRow(stopRow);
		ResultScanner rss = table.getScanner(scan);
		for (Result rs : rss) {
			Cell cell1 = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());
			Cell cell2 = rs.getColumnLatestCell("cf".getBytes(), "age".getBytes());
			Cell cell3 = rs.getColumnLatestCell("cf".getBytes(), "gender".getBytes());
			String name = Bytes.toString(CellUtil.cloneValue(cell1));
			String age = Bytes.toString(CellUtil.cloneValue(cell2));
			String gender = Bytes.toString(CellUtil.cloneValue(cell3));
			System.out.println(name + "--" + age + "--" + gender);
		}
		rss.close();
	}
	
	@After
	public void destory() throws Exception {
		admin.close();
		conn.close();
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42312342/article/details/89817967
今日推荐