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.MasterNotRunningException;
import org.apache.hadoop.hbase.TableExistsException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.protobuf.generated.ZooKeeperProtos.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class a {
private static Connection connection = null;
private static Admin admin = null;
private static Configuration configuration = null;
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "192.168.8.10");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (Exception e) {
e.printStackTrace();
}
}
private static void close(Connection connection,Admin admin) {
if(connection!=null) {
try {
connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
if(admin!=null) {
try {
admin.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
//旧的API方法
//判断是否存在
public static boolean tableExist(String tableName) throws IOException, IOException, IOException {
//Hbase配置文件
HBaseConfiguration configuration = new HBaseConfiguration();
configuration.set("hbase.zookeeper.quorum", "192.168.8.10");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.master", "192.168.8.10:9001");
//获取Hbase管理员对象,对表进行操作
HBaseAdmin admin = new HBaseAdmin(configuration);
//执行
boolean tableExists = admin.tableExists(tableName);
//关闭资源
admin.close();
//返回
return tableExists;
}
//新的API方法
//判断是否存在
public static boolean tableExist1(String tableName) throws IOException {
//Hbase配置文件
//HBaseConfiguration configuration = new HBaseConfiguration();
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "192.168.8.10");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.master", "192.168.8.10:9001");
//获取连接对象
Connection connection = ConnectionFactory.createConnection(configuration);
Admin admin = connection.getAdmin();
//执行
boolean tableExists = admin.tableExists(TableName.valueOf(tableName));
//关闭资源
admin.close();
//返回
return tableExists;
}
//创建表
public static void createTable(String tableName , String... cfs) throws IOException {
//判断表是否存在
if(tableExist(tableName)) {
System.out.println("表已经存在!!");
return;
}
//创建表操作
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
//添加列族
for (String cf : cfs) {
//创建列描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
//设置版本号
hColumnDescriptor.setMaxVersions(3);
hTableDescriptor.addFamily(hColumnDescriptor);
}
//创建表操作
admin.createTable(hTableDescriptor);
System.out.println("表创建成功!!");
}
//删除表
public static void deleteTable(String tableName) throws IOException {
//判断表是否存在
if(!tableExist(tableName)) {
return;
//System.out.println("表不存在");
}
//使表不可用
admin.disableTables(tableName.valueOf(tableName));
//执行删除操作
admin.deleteTables(tableName.valueOf(tableName));
System.out.println("表已经删除!!");
}
//增/改 --> 改,就是对同一个列族,重复添加,两者代码一样,在main中重复添加
public static void putData(String tableName,String rowKey,String cf,String cn,String value) throws IOException {
//获取表(table)对象
org.apache.hadoop.hbase.client.Table table = connection.getTable(TableName.valueOf(tableName));
//HTable table = new HTable(configuration,TableName.valueOf(tableName));
//创建put对象
Put put = new Put(Bytes.toBytes(rowKey));
//添加数据 ,一条一条插入
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
//添加数据,插入多条数据(1.方法写死了)
// put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
// put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
// put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
//执行添加操作,把put封装成一个对象,放到list集合中
table.put(put);
table.close();
}
//删
public static void delete(String tableName,String rowKey,String cf,String cn) throws IOException {
//获取表(table)对象
org.apache.hadoop.hbase.client.Table table = connection.getTable(TableName.valueOf(tableName));
//HTable table = new HTable(configuration,TableName.valueOf(tableName));
//创建delete对象
Delete delete = new Delete(Bytes.toBytes(rowKey));
//delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn)); //addColumn--删除最新的版本,基本不用
delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn)); //addColumns-删除所有的版本,公司基本用这个
//删除多条数据(1)
//delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
//delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
//delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
//执行删除操作,方法二,多个列数据传入集合,一起删除
table.delete(delete);
table.close();
}
//查(1) --获取所有的数据
//全表扫描
public static void scanTable(String tableName) throws IOException {
//获取表(table)对象
//Table table = connection.getTable(TableName.valueOf(tableName)); //这句报错,所以用下面的
org.apache.hadoop.hbase.client.Table table = connection.getTable(TableName.valueOf(tableName));
//构造扫描器
Scan scan = new Scan();
//startrow,stoprow
//scan.setStartRow(null);
//scan.setStopRow(null);
ResultScanner results = table.getScanner(scan);
//遍历数据并打印 scan是扫描所有的rowKey,所有有2个for循环
for (Result result : results) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("RK:"+Bytes.toString(CellUtil.cloneRow(cell))
+",CF:"+Bytes.toString(CellUtil.cloneFamily(cell))
+",CN:"+Bytes.toString(CellUtil.cloneQualifier(cell))
+",VALUE:"+Bytes.toString(CellUtil.cloneValue(cell)) );
}
}
table.close();
}
//查(2) --获取一个rowkey上所有的值
//获取 指定列族 ,列 的数据
public static void getData(String tableName,String rowKey,String cf,String cn) throws IOException {
//获取表(table)对象
org.apache.hadoop.hbase.client.Table table = connection.getTable(TableName.valueOf(tableName));
//创建get对象
Get get = new Get(Bytes.toBytes(rowKey));
//这里额外添加的代码,是为了指定到 列 和 列族
//get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
//get.addFamily(null);
//指定获取的版本
//get.setMaxVersions(); //默认版本
//get.setMaxVersions(3); //指定最大版本为3
//获取数据的操作
Result result = table.get(get);
//遍历数据并打印 --少一个for循环,是因为这里是唯一的rowKey,所以rowKey不需要循环遍历了
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("RK:"+Bytes.toString(CellUtil.cloneRow(cell))
+",CF:"+Bytes.toString(CellUtil.cloneFamily(cell))
+",CN:"+Bytes.toString(CellUtil.cloneQualifier(cell))
+",VALUE:"+Bytes.toString(CellUtil.cloneValue(cell)) );
}
table.close();
}
public static void main(String[] args) throws IOException {
//测试表是否存在
// System.out.println(tableExist("student")); //存在为true
//测试创建表,多个列族用逗号分隔
// createTable("staff", "info1");
//测试删除表
// deleteTable("staff");
//测试表是否存在
System.out.println(tableExist("staff"));
//测试增加数据
// putData("student", "1001", "info", "name", "zhangsan"); //一条一条插入
//测试修改数据
// putData("student", "1001", "info", "name", "lisi");
//测试删除操作
delete("student", "1001", "", ""); //后面两个值,如果不写,就随便写没有的值或者不写
//测试查询数据(scan)
//scanTable("student");
//测试查询数据(get)
getData("student", "1003", "info", "name"); //后面两个值,如果不写,就随便写没有的值或者不写
//关闭资源
close(connection,admin);
}
}

本文详细介绍了HBase数据库中表的创建、删除、数据的增删改查等核心操作,并提供了具体的Java代码实现,帮助读者深入理解HBase的使用。
1070

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



