使用Java对Hbase操作总结及示例代码

前面已经给大家讲解过如何使用Hbase建表,以及基本的操作和一些常用shell命令,今天就给大家介绍下如何使用java对Hbase进行各种操作。
没印象的话可以再去浏览下:
Hbase入门教程,shell命令大全讲解

Java操作Hbase主要方法:

1.Configuration
在使用Java API时,Client端需要知道HBase的配置环境,如存储地址,zookeeper等信息。
这些信息通过Configuration对象来封装,可通过如下代码构建该对象:

Configuration config = HBaseConfiguration.create();

在调用HBaseConfiguration.create()方法时,HBase首先会在classpath下查找hbase-site.xml文件,将里面的信息解析出来封装到Configuration对象中,如果hbase-site.xml文件不存在,则使用默认的hbase-core.xml文件。

2.HBaseAdmin
HBaseAdmin用于创建数据库表格,并管理表格的元数据信息,通过如下方法构建:
HBaseAdmin admin=new HBaseAdmin(config);

3.HTableDescriptor
在HTableDescriptor中,建立了一个表结构,HTableDescriptor封装表格对象,对表格的增删改查操作主要通过它来完成,构造方法如下:
HTableDescriptor table = new HTableDescriptor(TableName.valueOf(“表名”));

4.addFamily
addFamily用于建立表下的列簇,并存放到表结构,方法如下:
HColumnDescriptor base = new HColumnDescriptor(“列簇名”);
table.addFamily(base);

代码如下:
首先建一个maven工程,导入依赖包导pom.xml

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

1、创建表操作

public class HBaseClient {
 public void createTable() throws IOException {
 // 1. 创建配置
 Configuration conf = HBaseConfiguration.create();
 conf.set("hbase.zookeeper.quorum","ip1");
  //hbase主默认端口是60000
 conf.set("hbase.master","ip1:60000");
 //zookeeper客户端的端口号2181
 conf.set("hbase.zookeeper.property.clientPort","2181");
 // 2. 创建连接
 Connection conn = ConnectionFactory.createConnection(conf);
 //3.获得一个建表、删表的对象hbaseAdmin()是继承admin()
 Admin admin = conn.getAdmin();
 // 4. 创建表的描述信息
 HTableDescriptor student = new HTableDescriptor(TableName.valueOf("表名"));
 // 5. 添加列簇
 student.addFamily(new HColumnDescriptor("列簇名1"));
 student.addFamily(new HColumnDescriptor("列簇名2"));
 // 6. 调用API进行建表操作
 admin.createTable(student);
 }

}

2、判断表是否存在

public void isTableExists() throws IOException {
 // 1. 创建配置
 Configuration conf = HBaseConfiguration.create();
 conf.set("hbase.zookeeper.quorum","ip1");
 conf.set("hbase.zookeeper.property.clientPort","2181");
 // 2. 创建连接
 Connection conn = ConnectionFactory.createConnection(conf);
 // 3. 创建admin
 Admin admin = conn.getAdmin();
 // 4. 调用API进行判断表是否存在
 System.out.println(admin.tableExists(TableName.valueOf("表名")));
 }

3、向表中插入数据

public void putData2Table() throws IOException {
 // 1. 创建配置
 Configuration conf = HBaseConfiguration.create();
 conf.set("hbase.zookeeper.quorum","ip1");
 conf.set("hbase.zookeeper.property.clientPort","2181");
 // 2. 创建连接
 Connection conn = ConnectionFactory.createConnection(conf);
 // 3. 创建Table类
 Table student = conn.getTable(TableName.valueOf("表名"));
 // 4. 创建Put类
 Put put = new Put(Bytes.toBytes("1001"));
 // 5. 向Put中添加 列簇,列名,值 注意:需要转化成字节数组
 put.addColumn(Bytes.toBytes("列簇1"),Bytes.toBytes("列1"),Bytes.toBytes("zhangsan"));
 put.addColumn(Bytes.toBytes("列簇1"),Bytes.toBytes("列2"),Bytes.toBytes("female"));
 put.addColumn(Bytes.toBytes("列簇2"),Bytes.toBytes("列3"),Bytes.toBytes("math"));
 put.addColumn(Bytes.toBytes("列簇2"),Bytes.toBytes("列4"),Bytes.toBytes("89"));
 // 6.调用API进行插入数据
 student.put(put);
 }

4、查看一条数据

public void getDataFromTable() throws IOException {
 // 1. 创建配置
 Configuration conf = HBaseConfiguration.create();
 conf.set("hbase.zookeeper.quorum","ip1");
 conf.set("hbase.zookeeper.property.clientPort","2181");
 // 2. 创建连接
 Connection conn = ConnectionFactory.createConnection(conf);
 // 3. 创建Table类
 Table student = conn.getTable(TableName.valueOf("表名"));
 // 4. 创建 Get 类
 Get get = new Get(Bytes.toBytes("1001"));
 // 5.调用API进行获取数据
 Result result = student.get(get);
 // 6. 将返回的结果进行遍历输出
 Cell[] cells = result.rawCells();
 for (Cell cell : cells) {
  System.out.println("rowkey :"+Bytes.toString(CellUtil.cloneRow(cell)));
  System.out.println("列簇 :"+Bytes.toString(CellUtil.cloneFamily(cell)));
  System.out.println("列名 :"+Bytes.toString(CellUtil.cloneQualifier(cell)));
  System.out.println("值 :"+Bytes.toString(CellUtil.cloneValue(cell)));
  System.out.println("----------------");
 }
 }

5、删除表操作

public void dropTable() throws IOException {
 // 1. 创建配置
 Configuration conf = HBaseConfiguration.create();
 conf.set("hbase.zookeeper.quorum","ip1");
 conf.set("hbase.zookeeper.property.clientPort","2181");
 // 2. 创建连接
 Connection conn = ConnectionFactory.createConnection(conf);
 // 3. 创建admin
 Admin admin = conn.getAdmin();
 // 4. 调用API禁用表
 admin.disableTable(TableName.valueOf("表名"));
 // 5. 调用API删除表
 admin.deleteTable(TableName.valueOf("表名"));
 }
}

6、删除hbase中的table里面的rowkey

public static void deleteRow(String tableName,String rowKey) throws Exception{
 HTable hTable = new HTable(configuration,tableName);
 Delete delete = new Delete(rowKey.getBytes());
 List<Delete> list = new ArrayList<Delete>();
 list.add(delete);
 hTable.delete(list);
 } 

7、查询row = rowKey的数据

public static void getRow(String tableName,String rowKey) throws Exception{
 HTable hTable = new HTable(configuration, tableName);
 Get get = new Get(rowKey.getBytes());
 Result result = hTable.get(get);
 for(KeyValue value:result.raw()){
  System.out.println("cf:"+new String(value.getFamily())+new String(value.getQualifier())+"="+new String(value.getValue()));
 }
 }

8、查询rowkey在startRow和endRow之间的数据,及rowkey的范围查询
Put、Delete与Get对象都是Row的子类,从该继承关系中我们就可以了解到Get、Delete与Pu对象本身就只能进行单行的操作,
HBase客户端还提供了一套能够进行全表扫描的API,方便用户能够快速对整张表进行扫描,以获取想要的结果—scan:

 public static void getBetweenRow(String tableName,String startRow,String stopRow) throws Exception{
 HTable table = new HTable(configuration, tableName);
 Scan scan = new Scan();
 scan.addColumn("cf1".getBytes(), "colum1".getBytes());
 scan.addColumn("cf1".getBytes(), "colum2".getBytes());
 scan.addColumn("cf1".getBytes(), "colum3".getBytes());

	scan.setStartRow(startRow.getBytes());
	scan.setStopRow(stopRow.getBytes());

	ResultScanner scanner = table.getScanner(scan);
	
	for(Result result:scanner){
	 for(KeyValue value:result.raw()){
	  System.out.println("cf:"+new String(value.getFamily())+new String(value.getQualifier())+"="+new String(value.getValue()));
	 }
	}
}		 

9、批量写入

public <T> void puts(String tableName, Map<String, Object> items) {
 if (items == null || items.isEmpty()) {
  LOG.error("[HBase] Adding null/empty item map!");
  return;
 }
 int maxSize = 10000;
 Table table = null;
 try {
  table = con.getTable(TableName.valueOf(tableName));
  int eachSize = Math.min(maxSize, items.size());
  List<Put> puts = new ArrayList<Put>(eachSize);
  int handled = 0;
  
  for (Entry<String, Object> entry : items.entrySet()) {
  String ultimateRowKey = getHashedID(entry.getKey());
  Object value = entry.getValue();
  
  if (ultimateRowKey == null || ultimateRowKey.isEmpty()) {
   LOG.error("[HBase] Adding null/empty hashed key! Original key is " + entry.getKey());
   handled++;
   continue;
  }
	
		Put put = new Put(Bytes.toBytes(ultimateRowKey));
  put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("ab"), Bytes.toBytes(value .getAb()));
  put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("dt"), Bytes.toBytes(value .getDt()));
  put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("hb"), Bytes.toBytes(value .getHb()));
  
  Gson gson = new Gson();
  String valuestr = gson.toJson(value);
  put.addColumn(Bytes.toBytes(familyName2), Bytes.toBytes("js"), Bytes.toBytes(valuestr));
  puts.add(put);
  handled++;

		// 每隔10000,写一次
  if (handled == eachSize) {
   LOG.info("[HBase] Adding " + eachSize + "rows!");
   table.put(puts);
   puts = new ArrayList<Put>(eachSize);
  }
  }
  if (puts.size() > 0)
  table.put(puts);
 } catch (IOException e) {
 	LOG.error("[HBase] Error while putting data " + e.getMessage());
 } finally {
  try {
   if (table != null)
   table.close();
  } catch (IOException e) {
  LOG.error("[HBase] Error while closing table " + e.getMessage());
  }

	}
}