【hdfs】【hbase】【大数据技术基础】实践二 HBase Java API编程

  •         //查询Score表中,行键为95001,列族为sname的值(因为sname列族下没有子列所以第四个参数为空)
  •         //getData("Score", "95001", "sname", "");
  •         //删除Score表
  •         //deleteTable("Score");
  •     }
  •     //建立连接
  •     public static void init(){
  •         configuration  = HBaseConfiguration.create();
  •         configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
  •         try{
  •             connection= ConnectionFactory.createConnection(configuration);
  •             admin = connection.getAdmin();
  •         }catch (IOException e){
  •             e.printStackTrace();
  •         }
  •     }
  •     //关闭连接
  •     public static void close(){
  •         try{
  •             if(admin != null){
  •                 admin.close();
  •             }
  •             if(null != connection){
  •                 connection.close();
  •             }
  •         }catch (IOException e){
  •             e.printStackTrace();
  •         }
  •     }
  •     /**
  •      * 建表。HBase的表中会有一个系统默认的属性作为主键,主键无需自行创建,默认为put命令操作中表名后第一个数据,因此此处无需创建id列
  •      * @param myTableName 表名
  •      * @param colFamily 列族名
  •      * @throws IOException
  •      */
  •     public static void createTable(String myTableName,String[] colFamily) throws IOException {
  •         init();
  •         TableName tableName = TableName.valueOf(myTableName);
  •         if(admin.tableExists(tableName)){
  •             System.out.println("talbe is exists!");
  •         }else {
  •             HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
  •             for(String str:colFamily){
  •                 HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
  •                 hTableDescriptor.addFamily(hColumnDescriptor);
  •             }
  •             admin.createTable(hTableDescriptor);
  •             System.out.println("create table success");
  •         }
  •         close();
  •     }
  •     /**
  •      * 删除指定表
  •      * @param tableName 表名
  •      * @throws IOException
  •      */
  •     public static void deleteTable(String tableName) throws IOException {
  •         init();
  •         TableName tn = TableName.valueOf(tableName);
  •         if (admin.tableExists(tn)) {
  •             admin.disableTable(tn);
  •             admin.deleteTable(tn);
  •         }
  •         close();
  •     }
  •     /**
  •      * 查看已有表
  •      * @throws IOException
  •      */
  •     public static void listTables() throws IOException {
  •         init();
  •         HTableDescriptor hTableDescriptors[] = admin.listTables();
  •         for(HTableDescriptor hTableDescriptor :hTableDescriptors){
  •             System.out.println(hTableDescriptor.getNameAsString());
  •         }
  •         close();
  •     }
  •     /**
  •      * 向某一行的某一列插入数据
  •      * @param tableName 表名
  •      * @param rowKey 行键
  •      * @param colFamily 列族名
  •      * @param col 列名(如果其列族下没有子列,此参数可为空)
  •      * @param val 值
  •      * @throws IOException
  •      */
  •     public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
  •         init();
  •         Table table = connection.getTable(TableName.valueOf(tableName));
  •         Put put = new Put(rowKey.getBytes());
  •         put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
  •         table.put(put);
  •         table.close();
  •         close();
  •     }
  •     /**
  •      * 删除数据
  •      * @param tableName 表名
  •      * @param rowKey 行键
  •      * @param colFamily 列族名
  •      * @param col 列名
  •      * @throws IOException
  •      */
  •     public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
  •         init();
  •         Table table = connection.getTable(TableName.valueOf(tableName));
  •         Delete delete = new Delete(rowKey.getBytes());
  •         //删除指定列族的所有数据
  •         //delete.addFamily(colFamily.getBytes());
  •         //删除指定列的数据
  •         //delete.addColumn(colFamily.getBytes(), col.getBytes());
  • 上一篇:12 Node.js API接口开发


    下一篇:FastDDS服务发现之PDP的收发-EDP发送