HBase第一章:集群搭建
HBase第二章:API操作(一)
HBase第二章:API操作(二)
上次博客我们记录了DDL的操作api,这次博客我们主要记录一下DML的操作api。
HBaseDML.java
package com.atguigu;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.ColumnValueFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBaseDML {
//添加静态属性 connection 指向单例连接
public static Connection connection=HBaseConnection.connection;
/**
* 插入数据
* @param namespace 命名空间名称
* @param tableName 表格名称
* @param rowkey 主键
* @param columFamily 列族名称
* @param columnName 列名
* @param value 值
*/
public static void putCell(String namespace ,String tableName ,String rowkey ,String columFamily,String columnName ,String value) throws IOException {
//1.获取table
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
//2.调用相关方法插入数据
//2.1创建put对象
Put put = new Put(Bytes.toBytes(rowkey));
//2.2给put对象添加数据
put.addColumn(Bytes.toBytes(columFamily),Bytes.toBytes(columnName),Bytes.toBytes(value));
//2.3将对象写入对应的方法
try {
table.put(put);
} catch (IOException e) {
throw new RuntimeException(e);
}
//3.关闭table
table.close();
}
/**
* 读取数据 读取对应的一行中的某一列
* @param namespace 命名空间名称
* @param tableName 表格名称
* @param rowkey 主键
* @param columFamily 列族名称
* @param columnName 列名
*/
public static void getCells(String namespace ,String tableName ,String rowkey ,String columFamily,String columnName) throws IOException {
//1.获取table
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
//2.创建get对象
Get get = new Get(Bytes.toBytes(rowkey));
//如果直接调用 get 方法读取数据 此时读一整行数据
//如果想读取某一列的数据 需要添加对应的参数
get.addColumn(Bytes.toBytes(columFamily),Bytes.toBytes(columnName));
//设置读取数据的版本(默认最新版)
get.readAllVersions();
try {
//读取数据 得到 result 对象
Result result = table.get(get);
//处理数据
Cell[] cells = result.rawCells();
//测试方法: 直接把读取的数据打印到控制台
//如果是实际开发 需要再额外写方法 对应处理数据
for (Cell cell : cells) {
String value = new String(CellUtil.cloneValue(cell));
System.out.println(value);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
//3.关闭table
table.close();
}
/**
* 扫描数据
* @param namespace 命名空间
* @param tableName 表格名称
* @param startRow 开始的row 包含的
* @param stopRow 结束的row 不包含
*/
public static void scanRows(String namespace ,String tableName ,String startRow,String stopRow) throws IOException {
//1.获取table
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
//创建scan对象
Scan scan = new Scan();
// 如果此时直接调用 会直接扫描整张表
// 添加参数 来控制扫描的数据
// 起始行(默认包含)
scan.withStartRow(Bytes.toBytes(startRow));
// 结束行(默认不包含)
scan.withStopRow(Bytes.toBytes(stopRow));
try {
// 读取多行数据,获取scanner
ResultScanner scanner = table.getScanner(scan);
// result 来记录一行数据 cell 数组
// ResultScanner 来记录多行数据 result 的数组
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.print(new String(CellUtil.cloneRow(cell)) + "-" +
new String(CellUtil.cloneFamily(cell)) + "-" +
new String(CellUtil.cloneQualifier(cell)) + "-" +
new String(CellUtil.cloneValue(cell)) + "\t");
}
System.out.println();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
//3.关闭table
table.close();
}
/**
* 带过滤的扫描
* @param namespace 命名空间
* @param tableName 表格名称
* @param startRow 开始row
* @param stopRow 结束 row
* @param columFamily 列族名称
* @param columnName 列名
* @param value value值
*/
public static void filterScan(String namespace ,String tableName ,String startRow,String stopRow,String columFamily,String columnName ,String value) throws IOException {
// 1. 获取 table
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
// 2. 创建 scan 对象
Scan scan = new Scan();
// 如果此时直接调用 会直接扫描整张表
// 添加参数 来控制扫描的数据
// 起始行(默认包含)
scan.withStartRow(Bytes.toBytes(startRow));
// 结束行(默认不包含)
scan.withStopRow(Bytes.toBytes(stopRow));
// 可以添加多个过滤
FilterList filterList = new FilterList();
// 创建过滤器
// (1) 结果只保留当前列的数据
ColumnValueFilter columnValueFilter = new ColumnValueFilter(
Bytes.toBytes(columFamily),
Bytes.toBytes(columnName),
CompareOperator.EQUAL,
Bytes.toBytes(value)
);
// (2) 结果保留整行数据
// 结果同时会保留没有当前列的数据
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
Bytes.toBytes(columFamily),
Bytes.toBytes(columnName),
CompareOperator.EQUAL,
Bytes.toBytes(value)
);
// 本身可以添加多个过滤器
filterList.addFilter(singleColumnValueFilter);
// 添加过滤
scan.setFilter(filterList);
try {
// 读取多行数据 获得 scanner
ResultScanner scanner = table.getScanner(scan);
// result 来记录一行数据 cell 数组
// ResultScanner 来记录多行数据 result 的数组
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.print(new String(CellUtil.cloneRow(cell)) + "-" +
new String(CellUtil.cloneFamily(cell)) + "-" +
new String(CellUtil.cloneQualifier(cell)) + "-" +
new String(CellUtil.cloneValue(cell)) + "\t");
}
System.out.println();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
//3.关闭table
table.close();
}
/**
* 删除一行中的一列数据
* @param namespace 命名空间
* @param tableName 表格名称
* @param rowkey 主键
* @param columFamily 列族名称
* @param columnName 列名
*/
public static void deleteColum(String namespace ,String tableName,String rowkey,String columFamily,String columnName) throws IOException {
// 1.获取 table
Table table = connection.getTable(TableName.valueOf(namespace, tableName));
// 2.创建 Delete 对象
Delete delete = new Delete(Bytes.toBytes(rowkey));
// 3.添加删除信息
// 3.1 删除单个版本
delete.addColumn(Bytes.toBytes(columFamily),Bytes.toBytes(columnName));
// 3.2 删除所有版本
delete.addColumns(Bytes.toBytes(columFamily),Bytes.toBytes(columnName));
try {
//4.删除数据
table.delete(delete);
} catch (IOException e) {
throw new RuntimeException(e);
}
//5.关闭资源
table.close();
}
public static void main(String[] args) throws IOException {
// putCell("atguigu","bigdata","2021","info","name","zhangsan");
// putCell("atguigu","bigdata","2021","name","age","20");
// putCell("atguigu","bigdata","2021","name","name","wangwu");
// putCell("atguigu","bigdata","2022","info","age","22");
// putCell("atguigu","bigdata","2022","info","name","dahai");
// putCell("atguigu","bigdata","2022","name","name","songsong");
// getCells("atguigu","bigdata","2021","info","name");
// scanRows("atguigu","bigdata","2021","2022");
// filterScan("atguigu","bigdata","2021","2022","info","name","zhangsan");
// deleteColum("atguigu","bigdata","2021","info","name");
System.out.println("其它代码");
HBaseConnection.closeConnection();
}
}
我们先用上次写的DDL随便创建一个表,命名空间等参数可以自定义。
createTable("atguigu","bigdata","info","name");
describe 'atguigu:bigdata’查看信息。
scan 'atguigu:bigdata’查看详细信息
随便插点
putCell("atguigu","bigdata","2021","info","name","zhangsan");
putCell("atguigu","bigdata","2021","name","age","20");
putCell("atguigu","bigdata","2021","name","name","wangwu");
putCell("atguigu","bigdata","2022","info","age","22");
putCell("atguigu","bigdata","2022","info","name","dahai");
putCell("atguigu","bigdata","2022","name","name","songsong");
查询表中
rowkey=“2021”
columFamily=“info”
columnName=“name”
getCells("atguigu","bigdata","2021","info","name");
查询rowkey=2021的数据
scanRows("atguigu","bigdata","2021","2022");
filterScan("atguigu","bigdata","2021","2022","info","name","zhangsan");
这里要解释一下这个查询结果,第一个就是我们要得结果,但是为什么会有后边两个结果呢。
我们给过滤查询传入了四个参数,我们只会对含有这四个参数的值进行过滤,如果不完全包含这个参数则直接保留。
deleteColum("atguigu","bigdata","2021","info","name");
hbase的学习还有一些进阶内容,但考虑到博主是初学,没有必要学习太深,所以hbase的内容就学习到这里。