import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DMLTest01 {
public static void main(String[] args) throws IOException {
DMLTest01 test01 = new DMLTest01();
//构建链接对象
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node1:2181,node2:2181,node3:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//构建table表对象
TableName tableName = TableName.valueOf("111:01");
Table table = connection.getTable(tableName);
//数据的增删改查
//test01.putData(table);
//test01.getData(table);
//test01.delData(table);
//test01.scanData(table);
//test01.putListData(table);
//释放资源
table.close();
connection.close();
}
/**
* 批量写入数据到Hbase
* @param table
*/
private void putListData(Table table) throws IOException {
//step1:构建Put对象,一个Put对象用于表示一个Rowkey写入的数据
Put put1 = new Put(Bytes.toBytes("20210101_001"));
Put put2 = new Put(Bytes.toBytes("20210201_002"));
Put put3 = new Put(Bytes.toBytes("20210301_003"));
//step2:为Put添加这个Rowkey的每一列
put1.addColumn(Bytes.toBytes("basic"),Bytes.toBytes("name"),Bytes.toBytes("laoda"));
put1.addColumn(Bytes.toBytes("basic"),Bytes.toBytes("age"),Bytes.toBytes("18"));
put1.addColumn(Bytes.toBytes("other"),Bytes.toBytes("phone"),Bytes.toBytes("110"));
put1.addColumn(Bytes.toBytes("other"),Bytes.toBytes("addr"),Bytes.toBytes("shanghai"));
put2.addColumn(Bytes.toBytes("basic"),Bytes.toBytes("name"),Bytes.toBytes("laoer"));
put2.addColumn(Bytes.toBytes("basic"),Bytes.toBytes("age"),Bytes.toBytes("20"));
put2.addColumn(Bytes.toBytes("other"),Bytes.toBytes("phone"),Bytes.toBytes("120"));
put3.addColumn(Bytes.toBytes("basic"),Bytes.toBytes("name"),Bytes.toBytes("laosan"));
put3.addColumn(Bytes.toBytes("basic"),Bytes.toBytes("age"),Bytes.toBytes("22"));
put3.addColumn(Bytes.toBytes("other"),Bytes.toBytes("addr"),Bytes.toBytes("beijing"));
//step3:将多个Put封装到List中
List<Put> puts = new ArrayList<Put>();
puts.add(put1);
puts.add(put2);
puts.add(put3);
//step4:执行PutList
table.put(puts);
}
/**
* scan查询数据
* @param table
*/
private void scanData(Table table) throws IOException {
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
//rowkey值
System.out.println(result.getRow());
for (Cell cell : result.rawCells()) {
System.out.println(
//rowkey
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()
);
}
}
}
/**
* 删除数据
* @param table
*/
private void delData(Table table) throws IOException {
Delete delete = new Delete(Bytes.toBytes("20210318_01"));
//删除一个版本
delete.addColumn(Bytes.toBytes("aa"),Bytes.toBytes("name"));
//删除所有版本
delete.addColumns(Bytes.toBytes("aa"),Bytes.toBytes("name"));
table.delete(delete);
}
/**
* get方法查询数据
* @param table
*/
private void getData(Table table) throws IOException {
Get get = new Get(Bytes.toBytes("20210318_01"));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println(
//rowkey
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()
);
}
}
/**
* 写入数据到Hbase
* @param table
*/
private void putData(Table table) throws IOException {
Put put = new Put(Bytes.toBytes("20210318_01"));
put.addColumn(Bytes.toBytes("aa"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
table.put(put);
}
}
HBase JavaAPI:DML(数据的增删改查)
Guess you like
Origin blog.csdn.net/tian_1_2_3/article/details/115505645
Recommended
Ranking