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();

		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值