简单总结了下,内容源自网络。
public static boolean isTableExist(String tableName) throws IOException { // 创建配置信息并配置 Configuration configuration = HbaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "97,98,99"); // 获取与Hbase的连接 Connection connection = ConnectionFactory.createConnection(configuration); //获取DDL *** 作对象 Admin admin = connection.getAdmin(); //判断表是否存在 boolean exits = admin.tableExists(TableName.valueOf(tableName)); //关闭连接 admin.close(); connection.close(); return exits; } //创建表 public static void createTable(String tableName,String... cfs) throws IOException { // 首先判断列族信息是否有误 if(cfs.length <= 0){ System.out.println("请设置列族信息!"); return; } // 判断表是否存在 if (isTableExist(tableName)){ System.out.println("该表已经存在!"); return; } // 创建配置信息并配置 Configuration configuration = HbaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "97,98,99"); // 获取与Hbase的连接 Connection connection = ConnectionFactory.createConnection(configuration); //获取DDL操作对象 Admin admin = connection.getAdmin(); //创建表描述器构造器 TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)); //循环添加列族信息 for (String cf:cfs){ // 创建列表述 ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)); tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build()); } // 关闭创建表的操作 admin.createTable(tableDescriptorBuilder.build()); //关闭资源 admin.close(); connection.close(); } //TODO 插入数据 public static void putDate(String tableName,String rowKey,String cf,String cn,String value) throws IOException { // 创建配置信息并配置 Configuration put_configuration = HbaseConfiguration.create(); put_configuration.set("hbase.zookeeper.quorum", "97,98,99"); // 获取与Hbase的连接 Connection put_connection = ConnectionFactory.createConnection(put_configuration); // 获取表的连接 Table puttable = put_connection.getTable(TableName.valueOf(tableName)); // 创建put对象 Put put = new Put(Bytes.toBytes(rowKey)); // 放入数据 put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value)); // 执行插入数据操作 puttable.put(put); //关闭资源 puttable.close(); put_connection.close(); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)