设为首页 加入收藏

TOP

第十三记·Java操作HBase详细代码
2019-02-19 13:45:03 】 浏览:99
Tags:十三 Java 操作 HBase 详细 代码
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014414323/article/details/81131321

XY个人记

在shell客户端上控制hbase就不详细的介绍了,关于环境搭建前几篇文章都有详细的介绍,下面来写一下Java代码,这里是官网上的API地址:https://hbase.apache.org/1.2/apidocs/index.html。

相关实现介绍

这里介绍Java操作HBase的代码有一下几个相关的功能:
1.建表
应用到对象org.apache.hadoop.hbase.client.Admin来操作
建表首先要创建一个namespace —— NamespaceDescriptor
还要创建列簇 —— HColumnDescriptor
然后建表 —— createTable
2.删表
同在shell里删除表一样,删除之前首先禁用表
同样用Admin,disableTable 禁用表
然后使用deleteTable删除表
3.新增数据(put也使用于修改)
新增修改数据主要是 org.apache.hadoop.hbase.client.Put
首先确认插入到的列簇 HColumnDescriptor
然后在指定表和列簇中插入数据put
通过org.apache.hadoop.hbase.client.Table的put来插入数据
4.删除数据
删除数据根据 org.apache.hadoop.hbase.client.Delete 来指定rowkey
通过org.apache.hadoop.hbase.client.Table 的delete来删除
5.get查询
在shell中我们知道get是不支持全表扫描的
通过org.apache.hadoop.hbase.client.Get指定rowkey
用get查询的时候我们先要获取结果集 org.apache.hadoop.hbase.client.Result
结果集转换最小单元org.apache.hadoop.hbase.Cell
通过org.apache.hadoop.hbase.CellUtil可以转换数据
6.scan查询
scan查询是支持全表扫面的
scan中我们通过org.apache.hadoop.hbase.client.Scan.Scan()来定义一个ResultScanner
通过遍历ResultScanner可以获得Result
这里就和get查询一样了,通过rawCells来获取到Cell[]
最后通过org.apache.hadoop.hbase.CellUtil可以转换数据

代码实现

下面是上面功能的完整代码实现

package com.hadoop.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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
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.Delete;
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.Test;

/**
 * HBase Java API
 * HBase 1.2.1
 * @author PXY
 *
 */
public class JavaHBaseAPI {
	
	public Connection getConnection() throws IOException{
		Connection conn = null;
		
		//读取配置文件
		Configuration conf = HBaseConfiguration.create();
		// 数据文件存储的路径
		conf.set("hbase.rootdir", "hdfs://hadoop01.com:8020/hbase");
		// zookeeper连接信息
		conf.set("hbase.zookeeper.quorum", "hadoop01.com");
		// 建立对hbase的链接
		conn = ConnectionFactory.createConnection(conf);
		
		//HBaseAdmin admin1 = new HBaseAdmin(conf);
		//admin = conn.getAdmin();
		
		return conn;
	}
	
	/**
	 * 创建表
	 * @throws IOException
	 */
	@Test
	public void createTable() throws IOException{
		Connection conn = getConnection();
		Admin admin = conn.getAdmin();
		
		//创建一个命名空间
		NamespaceDescriptor nsd = NamespaceDescriptor.create("hbaseAPI").build();
		admin.createNamespace(nsd); // 创建命名空间
		//admin.deleteNamespace("hbaseAPI"); // 删除命名空间
		
		TableName tableName = TableName.valueOf("hbaseAPI:tapi"); // 创建一个tableName
		HTableDescriptor htd = new HTableDescriptor(tableName);
		//创建列簇
		HColumnDescriptor colDesc = new HColumnDescriptor("info");
		
		htd.addFamily(colDesc); // 给表增加列簇
		
		admin.createTable(htd); // 创建表
		
		conn.close();
	}
	
	
	/**
	 * 删除表
	 * @throws IOException
	 */
	@Test
	public void dropTable() throws IOException{
		Connection conn = getConnection();
		
		Admin admin = conn.getAdmin();
		
		TableName tableName = TableName.valueOf("hbaseAPI:tapi");
		
		admin.disableTable(tableName);  //禁用表
		admin.deleteTable(tableName); //删除表
		
		conn.close();
	}
	
	/**
	 * 插入数据
	 * @throws IOException
	 */
	@Test
	public void putDate() throws IOException{
		Connection conn = getConnection();
		
		TableName tableName = TableName.valueOf("hbaseAPI:tapi");
		
		Put put = new Put(Bytes.toBytes("10002"));
		
		Table table = conn.getTable(tableName);
		
		HColumnDescriptor[] hcds = table.getTableDescriptor().getColumnFamilies();
		
		for (HColumnDescriptor hColumnDescriptor : hcds) {
			String hcd = hColumnDescriptor.getNameAsString();
			
			if("capi".contentEquals(hcd)){
				put.addColumn(Bytes.toBytes(hcd), Bytes.toBytes("name"), Bytes.toBytes("capi_2_zhangs"));
				put.addColumn(Bytes.toBytes(hcd), Bytes.toBytes("age"), Bytes.toBytes("capi_2_18"));
			} else if ("info".equals(hcd)){
				put.addColumn(Bytes.toBytes(hcd), Bytes.toBytes("name"), Bytes.toBytes("info_2_zhangs"));
				put.addColumn(Bytes.toBytes(hcd), Bytes.toBytes("age"), Bytes.toBytes("info_2_18"));
			}
			
		}
		
		// 也可以put List
		//List<Put> puts = new ArrayList<Put>();
		//puts.add(put);
		//table.put(puts);
		
		table.put(put);
		
		conn.close();
	}
	
	/**
	 * 删除数据
	 * @throws IOException
	 */
	@Test
	public void deleteDate() throws IOException{
		Connection conn = getConnection();
		TableName tableName = TableName.valueOf("hbaseAPI:tapi");
		
		Table table = conn.getTable(tableName);
		
		Delete delete = new Delete(Bytes.toBytes("10001"));
		
		//delete.addFamily(Bytes.toBytes("info"));
		//delete.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
		
		table.delete(delete);  // 删除
		
		conn.close();
	}
	
	
	/**
	 * get查询 不支持全表扫面
	 * @throws IOException
	 */
	@Test
	public void getDate() throws IOException{
		Connection conn = getConnection();
		Get get = new Get(Bytes.toBytes("10001"));
		Table table = conn.getTable(TableName.valueOf("hbaseAPI:tapi"));
		Result Result = table.get(get);
		
		Cell[] cells = Result.rawCells();
		
		for (Cell cell : cells) {
			System.out.println(Bytes.toString(CellUtil.cloneRow(cell))); //rowKey
			System.out.println(Bytes.toString(CellUtil.cloneFamily(cell))); //列簇
			System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell))); //名称
			System.out.println(Bytes.toString(CellUtil.cloneva lue(cell))); //值
			System.out.println(cell.getTimestamp()); //时间戳
		}
		
		conn.close();
	}
	
	/**
	 * scan 全表扫描
	 * @throws IOException
	 */
	@Test
	public void scanDate() throws IOException{
		Connection conn = getConnection();
		
		TableName tableName = TableName.valueOf("hbaseAPI:tapi");
		
		Table table = conn.getTable(tableName);
		
		Scan scan = new Scan();
		
		ResultScanner resultscan = table.getScanner(scan);
		
		for (Result result : resultscan) {
			Cell[] cells = result.rawCells();
			
			for (Cell cell : cells) {
				System.out.println(Bytes.toString(CellUtil.cloneRow(cell))); //rowKey
				System.out.println(Bytes.toString(CellUtil.cloneFamily(cell))); //列簇
				System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell))); //名称
				System.out.println(Bytes.toString(CellUtil.cloneva lue(cell))); //值
				System.out.println(cell.getTimestamp()); //时间戳
			}
			
		}
		
		conn.close();
	}
	
	
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇本地调试HBase源码 下一篇影响HBase insert性能的几个因素

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目