本文的主线 项目 => 新增 => 查询 => 详情 => 删除
本文的示例代码参考hbase-data
启动HBase服务参考HBase搭建 之 单机模式
项目
spring init -b=2.2.10.RELEASE -j=1.8 -l=java -d=web --build=gradle hbase-data && cd hbase-data
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/hbasedata/DataController.java
package com.example.hbasedata;
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
public class DataController {
@PostMapping("/{tableName}")
public String create(@PathVariable("tableName") String tableName,
@RequestBody Map<String, String> data) throws IOException {
String rowKey = data.get("rowKey");
String familyName = data.get("familyName");
String qualifier = data.get("qualifier");
String value = data.get("value");
Connection connection = ConnectionFactory.createConnection();
Admin admin = connection.getAdmin();
if (admin.tableExists(TableName.valueOf(tableName))) {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
}
return data.toString();
}
}
./bin/hbase shell
create 'test', 'cf'
./gradlew bootrun
curl -X POST -H 'Content-Type: application/json' -d '{"rowKey" : "row1","familyName" : "cf","qualifier" : "a","value" : "value1"}' http://localhost:8080/test
# {rowKey=row1, familyName=cf, qualifier=a, value=value1}
curl -X POST -H 'Content-Type: application/json' -d '{"rowKey" : "row2","familyName" : "cf","qualifier" : "a","value" : "value2"}' http://localhost:8080/test
# {rowKey=row2, familyName=cf, qualifier=a, value=value2}
./bin/hbase shell
scan 'test'
# row1 column=cf:a, timestamp=1607673937167, value=value1
# row2 column=cf:a, timestamp=1607673943275, value=value2
查询
vim src/main/java/com/example/hbasedata/DataController.java
package com.example.hbasedata;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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
public class DataController {
// 省略了未修改代码
@GetMapping("/{tableName}")
public List<String> index(@PathVariable("tableName") String tableName,
@RequestParam("familyName") String familyName,
@RequestParam("qualifier") String qualifier) throws IOException {
Connection connection = ConnectionFactory.createConnection();
Admin admin = connection.getAdmin();
List<String> dataList = new ArrayList<>();
if (admin.tableExists(TableName.valueOf(tableName))) {
Table table = connection.getTable(TableName.valueOf(tableName));
ResultScanner scanner = table.getScanner(new Scan());
for (Result result : scanner) {
for (Cell cell : result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes(qualifier))) {
dataList.add(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
return dataList;
}
}
./gradlew bootrun
curl http://localhost:8080/test?familyName=cf&qualifier=a
# ["value1","value2"]
详情
vim src/main/java/com/example/hbasedata/DataController.java
package com.example.hbasedata;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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
public class DataController {
// 省略了未修改代码
@GetMapping("/{tableName}/{rowKey}")
public List<String> show(@PathVariable("tableName") String tableName,
@PathVariable("rowKey") String rowKey,
@RequestParam("familyName") String familyName,
@RequestParam("qualifier") String qualifier) throws IOException {
Connection connection = ConnectionFactory.createConnection();
Admin admin = connection.getAdmin();
List<String> columnList = new ArrayList<String>();
if (admin.tableExists(TableName.valueOf(tableName))) {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
for (Cell cell : result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes(qualifier))) {
columnList.add(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
return columnList;
}
}
./gradlew bootrun
curl http://localhost:8080/test/row1?familyName=cf&qualifier=a
# ["value1"]
删除
vim src/main/java/com/example/hbasedata/DataController.java
package com.example.hbasedata;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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
public class DataController {
// 省略了未修改代码
@DeleteMapping("/{tableName}/{rowKey}")
public String show(@PathVariable("tableName") String tableName,
@PathVariable("rowKey") String rowKey) throws IOException {
Connection connection = ConnectionFactory.createConnection();
Admin admin = connection.getAdmin();
if (admin.tableExists(TableName.valueOf(tableName))) {
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
}
return rowKey;
}
}
./gradlew bootrun
curl -X DELETE http://localhost:8080/test/row1
# row1
curl -X DELETE http://localhost:8080/test/row2
# row2
./bin/hbase shell
scan 'test'
# 0 row(s)
网友评论