import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HbaseClientDMLTest {
private MultipleColumnPrefixFilter filter;
private SingleColumnValueFilter scv;
public static void main(String[] args) throws Exception {
HbaseClientDMLTest dmlTest = new HbaseClientDMLTest();
//todo:1-构建一个连接对象
Configuration conf = HBaseConfiguration.create();
//指定Hbase集群的服务端地址:给定ZK的地址
conf.set("hbase.zookeeper.quorum", "node1:2181,node2:2181,node3:2181");
Connection conn = ConnectionFactory.createConnection(conf);
//todo:2-所有的DML必须构建一个Table表的对象
//构建一个表的对象
TableName tbname = TableName.valueOf("itcast:t2");
Table table = conn.getTable(tbname);
//增
// dmlTest.putData(table);
//查
//dmlTest.getData(table);
//删除
// dmlTest.deleteData(table);
//scan查看
//dmlTest.scanData(table);
//scanWith【查询2021年2月和3月的数据】
//dmlTest.scanWithData(table);
//scan + filter【过滤器】【查询2021年的所有数据】
dmlTest.scanFilter(table);
//列值过滤器【查询所有age = 20的数据】
//dmlTest.SIngleCoulumnValueFilter(table);
//多列值过滤器【查询所有数据的name和age这两列】
// dmlTest.MultipleCoulumnValueFilter(table);
//组合过滤器Filterlist【列值过滤器+多列值过滤器】
//dmlTest.singandmultiple(table);
table.close();
conn.close();
}
private void singandmultiple(Table table) throws IOException {
Scan scan = new Scan();
FilterList list = new FilterList();
list.addFilter(filter);
list.addFilter(scv);
scan.setFilter(list);
ResultScanner sc = table.getScanner(scan);
for (Result result : sc) {
for (Cell cell : result.rawCells()) {
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell))
+ "\t" +
Bytes.toString(CellUtil.cloneFamily(cell))
+ "\t" +
Bytes.toString(CellUtil.cloneQualifier(cell))
+ "\t" +
Bytes.toString(CellUtil.cloneValue(cell))
+ "\t" +
cell.getTimestamp()
);
}
System.out.println("----------------------");
}
}
private void MultipleCoulumnValueFilter(Table table) throws IOException {
Scan scan = new Scan();
byte[][] prrfixes = {
Bytes.toBytes("name"),
Bytes.toBytes("age")
};
filter = new MultipleColumnPrefixFilter(prrfixes);
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell))
+ "\t" +
Bytes.toString(CellUtil.cloneFamily(cell))
+ "\t" +
Bytes.toString(CellUtil.cloneQualifier(cell))
+ "\t" +
Bytes.toString(CellUtil.cloneValue(cell))
+ "\t" +
cell.getTimestamp()
);
}
System.out.println("----------------------");
}
}
private void SIngleCoulumnValueFilter(Table table) throws IOException {
Scan scan = new Scan();
scv = new SingleColumnValueFilter(Bytes.toBytes("cf1"), Bytes.toBytes("age"), CompareOperator.EQUAL, Bytes.toBytes("20"));
scan.setFilter(scv);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell)) //获取这一列的rowkey,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneFamily(cell)) //获取这一列的列族,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneQualifier(cell)) //获取这一列的名称,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneValue(cell)) //获取这一列的值,转换为字符串类型
+ "\t" +
cell.getTimestamp() //获取时间戳
);
}
}
}
private void scanFilter(Table table) throws IOException {
Scan scan = new Scan();
PrefixFilter filter = new PrefixFilter(Bytes.toBytes("2021"));
scan.setFilter(filter);
ResultScanner results = table.getScanner(scan);
for (Result result : results) {
for (Cell cell : result.rawCells()) {
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell)) //获取这一列的rowkey,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneFamily(cell)) //获取这一列的列族,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneQualifier(cell)) //获取这一列的名称,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneValue(cell)) //获取这一列的值,转换为字符串类型
+ "\t" +
cell.getTimestamp() //获取时间戳
);
}
}
}
private void scanWithData(Table table) throws IOException {
Scan scan = new Scan();
scan.withStartRow(Bytes.toBytes("202102"));
scan.withStopRow(Bytes.toBytes("202104"));
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell)) //获取这一列的rowkey,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneFamily(cell)) //获取这一列的列族,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneQualifier(cell)) //获取这一列的名称,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneValue(cell)) //获取这一列的值,转换为字符串类型
+ "\t" +
cell.getTimestamp() //获取时间戳
);
}
System.out.println("----------------------------------------------------------------------");
}
}
private void scanData(Table table) throws IOException {
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell)) //获取这一列的rowkey,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneFamily(cell)) //获取这一列的列族,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneQualifier(cell)) //获取这一列的名称,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneValue(cell)) //获取这一列的值,转换为字符串类型
+ "\t" +
cell.getTimestamp() //获取时间戳
);
}
System.out.println("----------------------------------------------------------------------");
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
}
}
private void deleteData(Table table) throws IOException {
Delete delete = new Delete(Bytes.toBytes("20201001_888"));
Delete d1 = delete.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"));
table.delete(d1);
}
private void getData(Table table) throws IOException {
Get get = new Get(Bytes.toBytes("20201001_888"));
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell)) //获取这一列的rowkey,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneFamily(cell)) //获取这一列的列族,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneQualifier(cell)) //获取这一列的名称,转换为字符串类型
+ "\t" +
Bytes.toString(CellUtil.cloneValue(cell)) //获取这一列的值,转换为字符串类型
+ "\t" +
cell.getTimestamp()
);
//获取时间戳
}
}
private void putData(Table table) throws IOException {
Put put = new Put(Bytes.toBytes("20201001_888"));
put.addColumn(
Bytes.toBytes("cf1"),//指定列族
Bytes.toBytes("name"),//指定列的名称
Bytes.toBytes("laoba")//指定列的值
);
//step3:让表执行put操作
table.put(put);
}
}
Hbase的Java(DML)
猜你喜欢
转载自blog.csdn.net/qq_45769990/article/details/115085330
今日推荐
周排行