一、HBase实现增删改查功能
package com.zzz.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*
* 1、HBaseConfiguration:封装了HBase集群的配置信息(代码运行所需要的环境)
* 2、HBaseAdmin:HBase系统管理员的角色,提供了对数据表进行操作或者管理的一些方法
* 3、HTable:封装了整个表的信息(表名、列簇的信息),提供了操作该表数据的所有的业务方法
* 4、HTableDescriptor:所有的列簇的信息(一到多个HColumnDescriptor)
* 5、HColumnDescriptor:一个列簇的信息
* 6、Cell:封装了一个column的信息:行键、列簇、列、值、时间戳
* 7、Put:插入操作所需要的所有的相关信息
* 8、Delete:删除操作所需要的所有的相关信息
* 9、Get:封装查询条件
* 10、Scan:封装所有的查询信息,和Get有一点不同:Scan可以设置Filter
* 11、Result:封装了一个rowKey所对应的所有的数据信息
* 12、ResultScanner:封装了多个Result对应的结果集,本质上就是多个rowKey所对应的信息
*/
public class HBaseOperate {
public static void main(String[] args) throws IOException {
//==========================1、判断表是否存在=======================
//System.out.println(isTableExist("student"));
//==========================2、创建表==============================
//创建只有一个列簇的表
//createTable("student4","info");
//创建有多个列簇的表
//createTable("student3","base_info","extra_info");
//==========================3、删除表==============================
//dropTable("student3");
//==========================4、向表中插入数据========================
//addData("student4", "007", "info", "name", "zhoujiu");//若数据已存在就是修改,不存在就是增加
//addData("student4", "007", "info", "age", "29");
//addData("student4", "007", "info", "school", "QHU");
//==========================5、获取所有数据(行)======================
//getAllData("student4");
//==========================6、获取某行数据==========================
//getRowData("student4","001");
//==========================7、获取某行指定的数据=====================
//getRowQualifierData("student4","001","info","name");
//==========================8、删除单行或多行数据=====================
//deleteRowsData("student4","006","007");
//==========================9、删除某行指定的数据,比如指定某个列簇的某个列限定符=====================
//deleteRowsQualifierData("student4","005","info","name");
//==========================10、获取某行至某行的数据===================
//getSSRowData("student4","001","004");//左闭右开
//==========================11、对值进行模糊扫描/过滤查询=======================
getValueFilterData("student4","2");//查询出值 value 中含有 U 的结果
}
//获取Configuration对象
public static Configuration conf;
static{
//使用HBaseConfiguration的单例方法实例化
//HBaseConfiguration:封装了HBase集群的配置信息(代码运行所需要的环境)
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop0:2181,hadoop1:2181,hadoop2:2181");
}
//1、判断表是否存在
public static boolean isTableExist(String tableName) throws IOException {
//在HBase中管理、访问表需要先创建HBaseAdmin对象
//HBaseAdmin:HBase系统管理员的角色,对数据表进行操作或者管理的一些方法
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = (HBaseAdmin)connection.getAdmin();
return admin.tableExists(TableName.valueOf(tableName));
}
//2、创建表,String...columnFamily是不定长参数
public static void createTable(String tableName, String...columnFamily) throws IOException {
//创建HBaseAdmin对象,以获取对数据表进行操作或者管理的一些方法
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = (HBaseAdmin) connection.getAdmin();
//判断表是否存在
if (isTableExist(tableName)){
System.out.println("table:" + tableName + "已存在");
}else{
//创建表属性HTableDescriptor对象,表名需要转字节类型
//HTableDescriptor:所有的列簇的信息(一到多个HColumnDescriptor)
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
//创建多个列簇
for (String cf : columnFamily){
descriptor.addFamily(new HColumnDescriptor(cf));
}
//根据对表的配置,创建表
admin.createTable(descriptor);
System.out.println("table:" + tableName + "创建成功");
}
}
//3、删除表:先disable再drop(delete)
public static void dropTable(String tableName) throws IOException {
//创建HBaseAdmin对象,以获取对数据表进行操作或者管理的一些方法
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
//判断表是否存在,若存在,先disable再delete
if (isTableExist(tableName)){
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("table:" + tableName + "删除成功");
}else{
System.out.println("table:" + tableName + "不存在");
}
}
//4、向表中插入数据
public static void addData(String tableName,String rowKey,String columnFamily,String column,String value) throws IOException {
//创建HTable对象,以获取操作该表数据的一些方法
Connection connection = ConnectionFactory.createConnection(conf);
Table hTable = connection.getTable(TableName.valueOf(tableName));
//向表中插入数据:先实例化一个Put对象,作为数据的载体
Put put = new Put(Bytes.toBytes(rowKey));
//向put对象中组装数据
put.addColumn(columnFamily.getBytes(),column.getBytes(),value.getBytes());
//向表中放入数据
hTable.put(put);
hTable.close();
System.out.println("插入数据成功!");
}
//5、获取所有数据,也就是获取所有行
public static void getAllData(String tableName) throws IOException {
//创建HTable对象,以获取操作该表数据的一些方法
Connection connection = ConnectionFactory.createConnection(conf);
Table hTable = connection.getTable(TableName.valueOf(tableName));
//得到用于扫描region的对象scan
//Scan:封装所有的查询信息。和Get有一点不同:Scan可以设置Filter
Scan scan = new Scan();
//使用HTable得到resultScanner实现类的对象
//ResultScanner:封装了多个Result的结果集,本质上不就是多个rowKey所对应的信息
ResultScanner resultScanner = hTable.getScanner(scan);
for (Result result : resultScanner) {
//Cell:封装了Column的所有的信息:RowKey、column qualifier、value、时间戳
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("行键:" + 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();
}
}
}
//6、获取某行数据
public static void getRowData(String tableName,String rowKey) throws IOException {
//创建HTable对象,以获取操作该表数据的一些方法
Connection connection = ConnectionFactory.createConnection(conf);
Table hTable = connection.getTable(TableName.valueOf(tableName));
//得到用于扫描一行的对象get
Get get = new Get(Bytes.toBytes(rowKey));//这里的get是行键rowKey的载体
Result result = hTable.get(get); //使用get方法获取get中的一行数据
//Cell:封装了Column的所有的信息:RowKey、column qualifier、value、时间戳
Cell[] cells = result.rawCells();
//循环获取所有信息
for (Cell cell : cells) {
System.out.println("行键:" + Bytes.toString(result.getRow()));
System.out.println("列簇:" + Bytes.toString(CellUtil.cloneFamily(cell)))

本文详细介绍了如何使用HBase的Java API实现增删改查功能,以及通过MapReduce与HBase的集成,包括官方HBase-MapReduce任务的执行、自定义HBase MapReduce的实现。此外,还深入探讨了HBase过滤器的使用,包括过滤器的种类、操作符和比较器,提供了具体的操作示例。
最低0.47元/天 解锁文章
680

被折叠的 条评论
为什么被折叠?



