今日完成,修改hbasebug,不过出现新的bug,但不影响hbase的正常使用.
顺利实现idea连接hbase数据库,同时顺利实现hbase数据库中数据的增删改查,以下是相关代码
package org.example; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.client.AsyncConnection; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import java.io.IOException; import java.util.concurrent.CompletableFuture; public class HbaseConection { //声明静态属性 public static Connection connection = null; static { try { connection = ConnectionFactory.createConnection(); } catch (IOException e) { System.out.println("连接获取失败"); throw new RuntimeException(e); } } public static void closeConnection() throws IOException { if(connection != null) connection.close(); } public static void main(String[] args) throws IOException { System.out.println(HbaseConection.connection); HbaseConection.closeConnection(); } }
package org.example; import org.apache.hadoop.hbase.NamespaceDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HBaseDDL { // 添加静态属性 connection 指向单例连接 public static Connection connection = HbaseConection.connection; //创建表格 public static void createNamespace(String namespace) throws IOException { Admin admin = connection.getAdmin(); NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(namespace); builder.addConfiguration("user","atguigu"); try { admin.createNamespace(builder.build()); } catch (IOException e) { System.out.println("命令空间已经存在"); e.printStackTrace(); } admin.close(); } //判断命名空间是否存在namespace命名空间,tablename表格名 public static boolean isTableExists(String namespace,String tablename) throws IOException { //获取admin Admin admin = connection.getAdmin(); boolean b = false; try { b = admin.tableExists(TableName.valueOf(namespace, tablename)); } catch (IOException e) { throw new RuntimeException(e); } admin.close(); return b; } public static void createTable(String namespace,String tableName,String... columnFamilies) throws IOException { //判断列族 if(columnFamilies.length == 0) { System.out.println("要求至少有一个列族"); return; } //判断表格是否存在 if(isTableExists(namespace,tableName)) { System.out.println("表格已经存在"); return; } //创建表格 columnFamilies列族名称,可有多个 Admin admin = connection.getAdmin(); //创建表格描述 TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(namespace, tableName)); for(String columnFamily : columnFamilies) { ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily)); columnFamilyDescriptorBuilder.setMaxVersions(5); tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build()); } try { admin.createTable(tableDescriptorBuilder.build()); } catch (IOException e) { System.out.println("表格已经存在"); throw new RuntimeException(e); } admin.close(); } public static void modifyTable(String namespace,String tableName,String columnFamily,int version) throws IOException { if(!isTableExists(namespace,tableName)) { return; } Admin admin = connection.getAdmin(); try { TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf(namespace, tableName)); TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(descriptor); ColumnFamilyDescriptor columnFamily1 = descriptor.getColumnFamily(Bytes.toBytes(columnFamily)); ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(columnFamily1); columnFamilyDescriptorBuilder.setMaxVersions(version); tableDescriptorBuilder.modifyColumnFamily(columnFamilyDescriptorBuilder.build()); admin.modifyTable(tableDescriptorBuilder.build()); } catch (IOException e) { throw new RuntimeException(e); } admin.close(); } public static boolean deleteTable(String namespace,String tablename) throws IOException { if(isTableExists(namespace,tablename)) { System.out.println("表格不存在,无法删除"); return false; } Admin admin = connection.getAdmin(); try { TableName tableName1 = TableName.valueOf(namespace, tablename); admin.disableTable(tableName1); admin.deleteTable(tableName1); } catch (IOException e) { throw new RuntimeException(e); } admin.close(); return true; } private static TableName getTableName(String namespace, String tablename) { return TableName.valueOf(namespace, tablename); } public static void main(String[] args) throws IOException { //测试命名空间 //createNamespace("atguigu"); //判断表格是否存在 //System.out.println(isTableExists("bigdata", "student")); //创建表格 createTable("atguigu","student","info1"); //修改表格 modifyTable("atguigu","student","info",6); System.out.println("其他代码"); HbaseConection.closeConnection(); } }
标签:总结,String,admin,每日,namespace,connection,IOException,static,大三 From: https://www.cnblogs.com/ewqewq/p/17694766.html