API介绍
HBase Java类 | HBase数据模型 |
---|---|
HBaseAdmin | 数据库(DataBase) |
HBaseConfiguration | |
HTable | Table |
HTableDescriptor | 列族 |
Put | 列标识符 |
Get |
介绍相关的类:
类 | 描述 |
---|---|
HBaseConfiguration | 完成对HBase的配置,主要设置一些相关属性 |
HBaseAdmin | 用于管理Hbase数据库的表信息,用于操作DDL |
HTableDescriptor | 用于描述表的相关信息,例如可以获取表的名字,添加列族 |
HTable | 可以用于直接操作表,但是对于更新操作是非线程安全的。 |
Put | 执行插入数据操作的 |
Get | 用于获取行的相关信息。 |
这里基于IDEA去实行开发,所以我们只需要导入依赖
<dependencies>
<dependency>
<groupId>org.apache.hbasegroupId>
<artifactId>hbase-serverartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>org.apache.hbasegroupId>
<artifactId>hbase-clientartifactId>
<version>1.3.1version>
dependency>
dependencies>
抽取对象 避免多次创建对象 影响程序的效率
//获取Configuration对象 private static Configuration conf; private static Connection connection; private static Admin admin; //该对象用于操作和管理以及访问表 static{ //创建Configuration对象 conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "master,slave1,slave2"); try { connection = ConnectionFactory.createConnection(conf); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } }
public static boolean isTableExist(String tableName) throws IOException {
//2.执行对应的操作
boolean flag = admin.tableExists(TableName.valueOf(tableName));
return flag;
}
/**
* 创建表
* @param tableName 表名
* @param columnFamily 列族名 可以有若干个这里使用可变形参
*/
public static void createTable(String tableName,String... columnFamily){
try {
//1.判断表是否存在
if (isTableExist(tableName)){
System.out.println("表已经存在了");
return;
}
//2.不存在该表 去创建表
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
//创建多个列族
for (String cf : columnFamily){
descriptor.addFamily(new HColumnDescriptor(cf));
}
//3.创建表
admin.createTable(descriptor);
System.out.println("表:" + tableName + "创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除表
* @param tableName
* @throws IOException
*/
public static void dropTable(String tableName) throws IOException {
//1.表不存在情况下
if (!isTableExist(tableName)){
System.out.println(tableName + "表不存在!!");
return;
}
//2.删除表操作 在shell交互式命令下 删除操作的步骤:
//a.先禁用表 b.再删除
//2.1禁用表
admin.disableTable(TableName.valueOf(tableName));
//2.2删除表
admin.deleteTable(TableName.valueOf(tableName));
System.out.println(tableName + "表删除成功!!!");
}
public static void createNameSpace(String nameSpace){
try{
//1.创建命名空间描述器
NamespaceDescriptor build = NamespaceDescriptor.create(nameSpace).build();
admin.createNamespace(build);
System.out.println(nameSpace + "命名空间创建成功!!");
}catch (NamespaceExistException e){ //创建同一个命名空间 抛出的异常 在这捕捉
System.out.println("命名空间已存在!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 添加数据
* @param tableName
* @param rowKey
* @param columnFamily
* @param column
* @param value
*/
public static void putData(String tableName,String rowKey,String columnFamily,String column,String value){
try {
//1.创建HTable对象
HTable table = new HTable(conf,tableName);
//2 向表中插入数据
Put put = new Put(Bytes.toBytes(rowKey));
//2.1
put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
//3.完成插入操作
table.put(put);
System.out.println("数据插入成功!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
//1.批量插入数据
public static void putMoreData(String tableName,String[] rowKey,String[] columnFamily,String[] columnQualifier,String[] value) throws IOException {
//1.先判断表是否存在
if (!admin.tableExists(TableName.valueOf(tableName))){
System.out.println(tableName + "表不存在");
return;
}
//2.获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
int length = rowKey.length;
//3.创建List集合存放Put对象
List<Put> putList = new ArrayList<Put>();
for (int i = 0; i < length; i++){
Put put = new Put(Bytes.toBytes(rowKey[i]));
put.addColumn(Bytes.toBytes(columnFamily[i]),Bytes.toBytes(columnQualifier[i]),
Bytes.toBytes(value[i]));
putList.add(put);
}
//4.批量插入数据
table.put(putList);
table.close();
System.out.println("插入数据成功");
}
public static void main(String[] args) throws IOException {
putMoreData("stu3",new String[]{"1001","1002"},
new String[]{"info","info"},new String[]{"name","age"},
new String[]{"zhangsan","18"});
}
}
/**
* 删除数据 根据传入 行键 列族 列标识符 来删除数据
* shell : delete 'tableName' 'rowKey' '列族'
* @param tableName
*/
public static void delete(String tableName,String rowKey,String columnFamily,String qualifier) throws IOException {
if (!admin.tableExists(TableName.valueOf(tableName))){
System.out.println(tableName + "不存在");
return;
}
//1.创建表的操作对象 Table HTable
HTable table = new HTable(configuration,TableName.valueOf(tableName));
//2.创建Delete对象 (构建对象)
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualifier));
//3.删除
table.delete(delete);
System.out.println("删除成功...");
}
public static void deleteMultiRow(String tableName,String... rowKey) throws IOException {
//1.获取表操作对象
HTable table = new HTable(configuration, TableName.valueOf(tableName));
//2.创建List集合 存储Delete待删除的元素
List<Delete> deleteList = new ArrayList<Delete>();
//表不存在不执行删除操作
if (!admin.tableExists(TableName.valueOf(tableName))){
System.out.println(tableName + "该表不存在");
return;
}
//3.构建Delete对象
for (String row : rowKey){
Delete delete = new Delete(Bytes.toBytes(row));
deleteList.add(delete);
}
//4.执行删除
table.delete(deleteList);
table.close();
System.out.println("删除成功");
}
public static void close(){
if (admin != null){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
//1.测试表是否存在
boolean flag = isTableExists("stu");
System.out.println(flag);
//2.创建表
//createTable("test:stu3","info1","info2");
//3.删除表测试
//deleteTable("stu3");
//4.创建命名空间
//createNameSpace("test");
//5.插入数据
// putData("test:stu3","0001","info1","name","lisi");
//6.获取数据
// getData("test:stu3","0001","info1","name");
//7.scan扫描全表
// scanTable("test:stu3");
//8.插入数据
//putData("stu3","1001","info","name","lisi");
//9.删除多行数据
//deleteMultiRow("test:stu3","1003","1002");
//关闭资源
//10删除数据
delete("stu3","1001","info","sex");
close();
}