美文网首页
HBase开发 之 表操作

HBase开发 之 表操作

作者: 诺之林 | 来源:发表于2020-12-10 18:33 被阅读0次

    本文的主线 项目 => 新增 => 列表 => 详情 => 删除

    本文的示例代码参考hbase-table

    启动HBase服务参考HBase搭建 之 单机模式

    项目

    spring init -b=2.2.10.RELEASE -j=1.8 -l=java -d=web --build=gradle hbase-table && cd hbase-table
    
    vim build.gradle
    
    dependencies {
        implementation('org.springframework.boot:spring-boot-starter-web') {
            exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        }
        implementation('org.apache.hbase:hbase-client:2.2.6')
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
            exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
    }
    

    新增

    vim src/main/java/com/example/hbasetable/TableController.java
    
    package com.example.hbasetable;
    
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.springframework.web.bind.annotation.*;
    
    import java.io.IOException;
    import java.util.Map;
    
    @RestController
    @RequestMapping("/tables")
    public class TableController {
        @PostMapping
        public String create(@RequestBody Map<String, String> data) throws IOException {
            TableName tableName = TableName.valueOf(data.get("tableName"));
            String familyName = data.get("familyName");
    
            TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
            ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(familyName));
            tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
    
            Connection connection = ConnectionFactory.createConnection();
            Admin admin = connection.getAdmin();
            admin.createTable(tableDescriptorBuilder.build());
            return tableName.toString();
        }
    }
    
    ./gradlew bootrun
    
    curl -X POST  -H 'Content-Type: application/json' -d '{"tableName" : "test","familyName" : "cf"}' http://localhost:8080/tables
    # test
    
    ./bin/hbase shell
    
    list
    

    列表

    vim src/main/java/com/example/hbasetable/TableController.java
    
    package com.example.hbasetable;
    
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.springframework.web.bind.annotation.*;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    @RestController
    @RequestMapping("/tables")
    public class TableController {
        // 省略了未修改代码
        @GetMapping
        public List<String> index() throws IOException {
            Connection connection = ConnectionFactory.createConnection();
            Admin admin = connection.getAdmin();
    
            List<String> tableNameList = new ArrayList<>();
            for (TableName tableName : admin.listTableNames()) {
                tableNameList.add(tableName.toString());
            }
            return tableNameList;
        }
    }
    
    ./gradlew bootrun
    
    curl http://localhost:8080/tables
    # ["test"]
    

    详情

    vim src/main/java/com/example/hbasetable/TableController.java
    
    package com.example.hbasetable;
    
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.springframework.web.bind.annotation.*;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    @RestController
    @RequestMapping("/tables")
    public class TableController {
        // 省略了未修改代码
        @GetMapping("/{tableName}")
        public List<String> show(@PathVariable("tableName") String tableName) throws IOException {
            Connection connection = ConnectionFactory.createConnection();
            Admin admin = connection.getAdmin();
    
            List<String> columnFamilyList = new ArrayList<String>();
            for (TableDescriptor tableDescriptor : admin.listTableDescriptors()) {
                if (tableDescriptor.getTableName().toString().equals(tableName)) {
                    for (ColumnFamilyDescriptor columnFamilyDescriptor : tableDescriptor.getColumnFamilies()) {
                        columnFamilyList.add(columnFamilyDescriptor.getNameAsString());
                    }
                }
            }
            return columnFamilyList;
        }
    }
    
    ./gradlew bootrun
    
    curl http://localhost:8080/tables/test
    # ["cf"]
    

    删除

    vim src/main/java/com/example/hbasetable/TableController.java
    
    package com.example.hbasetable;
    
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.springframework.web.bind.annotation.*;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    @RestController
    @RequestMapping("/tables")
    public class TableController {
        // 省略了未修改代码
        @DeleteMapping("/{tableName}")
        public String delete(@PathVariable("tableName") String tableName) throws IOException {
            Connection connection = ConnectionFactory.createConnection();
            Admin admin = connection.getAdmin();
    
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
            return tableName;
        }
    }
    
    ./gradlew bootrun
    
    curl -X DELETE http://localhost:8080/tables/test
    # test
    

    参考

    相关文章

      网友评论

          本文标题:HBase开发 之 表操作

          本文链接:https://www.haomeiwen.com/subject/dgstgktx.html