HBase開發 之 批量操作

本文的主線 項目 => 新增 => 刪除

本文的示例代碼參考hbase-batch

啓動HBase服務參考HBase搭建 之 單機模式

項目

spring init -target https://start.aliyun.com/ -b=2.2.10.RELEASE -j=1.8 -l=java -d=web --build=gradle hbase-batch && cd hbase-batch

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/hbasebatch/DataController.java
package com.example.hbasebatch;

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("/batch")
public class DataController {
    @PostMapping("/{tableName}")
    public Object[] create(@PathVariable("tableName") String tableName,
                         @RequestBody List<Map<String, String>> data) throws IOException, InterruptedException {
        List<Row> actions = new ArrayList<>();
        for (Map<String, String> item : data) {
            String rowKey = item.get("rowKey");
            String familyName = item.get("familyName");
            String qualifier = item.get("qualifier");
            String value = item.get("value");
            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(qualifier), Bytes.toBytes(value));
            actions.add(put);
        }

        Connection connection = ConnectionFactory.createConnection();
        Admin admin = connection.getAdmin();

        Object[] results = new Object[actions.size()];
        if (admin.tableExists(TableName.valueOf(tableName))) {
            Table table = connection.getTable(TableName.valueOf(tableName));
            table.batch(actions, results);
        }

        return results;
    }
}
./bin/hbase shell

create 'test', 'cf'
./gradlew bootrun

curl --request POST 'http://localhost:8080/batch/test' \
--header 'Content-Type: application/json' \
--data-raw '[
    {
        "rowKey": "row3",
        "familyName" : "cf",
        "qualifier" : "a",
        "value" : "value3"
    },
    {
        "rowKey": "row4",
        "familyName" : "cf",
        "qualifier" : "a",
        "value" : "value4"
    }
]'
# [{"exists":null,"stale":false,"row":null,"stats":null,"cursor":null,"partial":false,"empty":true,"map":null,"noVersionMap":null},{"exists":null,"stale":false,"row":null,"stats":null,"cursor":null,"partial":false,"empty":true,"map":null,"noVersionMap":null}]
./bin/hbase shell

scan 'test'
# row3                                                            column=cf:a, timestamp=1608002950704, value=value3
# row4                                                            column=cf:a, timestamp=1608002950704, value=value4

刪除

vim src/main/java/com/example/hbasebatch/DataController.java
package com.example.hbasebatch;

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("/batch")
public class DataController {
    // 省略了未修改代碼
    @DeleteMapping("/{tableName}")
    public Object[] delete(@PathVariable("tableName") String tableName,
                           @RequestBody List<String> data) throws IOException, InterruptedException {
        List<Row> actions = new ArrayList<>();
        for (String item : data) {
            String rowKey = item;
            Delete delete = new Delete(Bytes.toBytes(rowKey));
            actions.add(delete);
        }

        Connection connection = ConnectionFactory.createConnection();
        Admin admin = connection.getAdmin();

        Object[] results = new Object[actions.size()];
        if (admin.tableExists(TableName.valueOf(tableName))) {
            Table table = connection.getTable(TableName.valueOf(tableName));
            table.batch(actions, results);
        }

        return results;
    }
}
./gradlew bootrun

curl --request DELETE 'http://localhost:8080/batch/test' \
--header 'Content-Type: application/json' \
--data-raw '[
    "row3",
    "row4"
]'
# [{"exists":null,"stale":false,"row":null,"stats":null,"cursor":null,"partial":false,"empty":true,"map":null,"noVersionMap":null},{"exists":null,"stale":false,"row":null,"stats":null,"cursor":null,"partial":false,"empty":true,"map":null,"noVersionMap":null}]
./bin/hbase shell

scan 'test'
# 0 row(s)

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章