美文网首页
HBase开发 之 批量操作

HBase开发 之 批量操作

作者: 诺之林 | 来源:发表于2020-12-15 11:32 被阅读0次

本文的主线 项目 => 新增 => 删除

本文的示例代码参考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)

参考

相关文章

  • HBase开发 之 批量操作

    本文的主线 项目 => 新增 => 删除 本文的示例代码参考hbase-batch[http://git.nuoz...

  • HBase开发 之 表操作

    本文的主线 项目 => 新增 => 列表 => 详情 => 删除 本文的示例代码参考hbase-table[htt...

  • HBase开发 之 数据操作

    本文的主线 项目 => 新增 => 查询 => 详情 => 删除 本文的示例代码参考hbase-data[http...

  • Hbase批量处理操作batch

    在Hbase中,许多基于列表的操作,如delete(List deletes)和get(List ge...

  • HBase Java API 批量操作

    之前我们是这样获取数据的: 那么问题来了,我们想要获取多条数据,比如说查询1万条数据怎么办呢? 可能我们第一时间就...

  • hbase 服务化文档

    hbase 单独抽出平台 说白了 就是解决 hbase put ,scan 操作 ,开发比较吃力 ,不优雅等问题。...

  • HBase BulkLoad批量写入数据实战

    1.概述 在进行数据传输中,批量加载数据到HBase集群有多种方式,比如通过HBase API进行批量写入数据、使...

  • HBase学习目录

    Hbase安装Hbase Shell general操作 ddl操作 dml操作 内置过滤器samplehttp:...

  • hbase-spark bulk load(二)

    概述 之前写过spark批量导入Hbase的案例:Spark、BulkLoad Hbase、单列、多列,实现了多列...

  • RedisTemplate批量操作

    RedisTemplate批量添加操作教程,利用pipeline批量操作;multiSet()批量操作;for循环...

网友评论

      本文标题:HBase开发 之 批量操作

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