大家好,我是不温卜火,是一名计算机学院大数据专业大二的学生,昵称来源于成语—​​不温不火​​​,本意是​​希望自己性情温和​​​。作为一名互联网行业的小白,博主写博客一方面是为了记录自己的学习过程,另一方面是总结自己所犯的错误希望能够帮助到很多和自己一样处于起步阶段的萌新。但由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教! 

  此篇为大家带来的是Hbase简单的API操作。


目录


标注:
此处为反爬虫标记:读者可自行忽略

HBase快速入门系列(6) | Hbase简单的API操作_hadoop

 ​

1. 添加依赖

<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>


<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>

<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>D:/java/jdk-1.8.0/lib/tools.jar</systemPath>
</dependency>
</dependencies>

2. HBaseAPI

  • 1. 获取Configuration对象
/**
* @author 卜温不火
* @create 2020-05-12 10:48
*/
private static Connection connection = null;

static{

try {
Configuration conf = HBaseConfiguration.create();
//使用HBaseConfiguration的单例方法实例化
conf.set("hbase.zookeeper.quorum", "hadoop002,hadoop003,hadoop004");
conf.set("hbase.zookeeper.property.clientPort", "2181");
connection= ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}

}
  • 2. 判断表是否存在
// 判断表是否存在
public static boolean tableExists(String tableName) throws IOException{

Admin admin = connection.getAdmin();

try {
return admin.tableExists(TableName.valueOf(tableName));
}finally {
admin.close();
}

}
  • 3. 创建表
// 创建ddl
public static void creatTable(String tableName,String... families)throws IOException {

Admin admin = connection.getAdmin();

try {
if (admin.tableExists(TableName.valueOf(tableName))){
System.out.println("表:"+tableName+" 已经存在");
return;
}

HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));

for (String family : families){
HColumnDescriptor familyDesc = new HColumnDescriptor(family);
desc.addFamily(familyDesc);
}

admin.createTable(desc);
}finally {
admin.close();
}

}
  • 4. 删除表
// 删除表
public static void dropTable(String tableName) throws IOException{

Admin admin = connection.getAdmin();

try{
if(!admin.tableExists(TableName.valueOf(tableName))){
System.out.println("表" + tableName + "删除成功!");
}else{
System.out.println("表" + tableName + "不存在!");
}
admin.disableTable(TableName.valueOf(tableName));

admin.deleteTable(TableName.valueOf(tableName));
}finally {
admin.close();
}

}
  • 5. 向表中插入数据
// 往表中插入数据
public static void putCell(String tableName,String rowKey,String family,String column,String value) throws IOException {

if(!tableExists(tableName)){
return;
}
Table table = connection.getTable(TableName.valueOf(tableName));
try{


Put put = new Put(Bytes.toBytes(rowKey));

put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));

table.put(put);
}finally {
table.close();
}

}
  • 6. 删除数据

①删除某一行

// 1. 删除某一行
public static void deleteRow(String tableName,String rowKey) throws IOException {

Table table = connection.getTable(TableName.valueOf(tableName));

Delete delete = new Delete(Bytes.toBytes(rowKey));

table.delete(delete);

table.close();
}

②删除整个列组

// 2. 删除整个列组
public static void deleteFamily(String tableName,String rowKey,String family) throws IOException {

Table table = connection.getTable(TableName.valueOf(tableName));

Delete delete = new Delete(Bytes.toBytes(rowKey));

delete.addFamily(Bytes.toBytes(family));

table.delete(delete);

table.close();
}

③删除某一列

// 删除所有版本
public static void deleteCell(String tableName,String rowKey,String family,String column) throws IOException {

Table table = connection.getTable(TableName.valueOf(tableName));

Delete delete = new Delete(Bytes.toBytes(rowKey));

delete.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));

table.delete(delete);

table.close();
}
// 测试
public static void main(String[] args)throws IOException{

putCell("student","1003","info", "name","buwen");
putCell("student","1003","info", "name","bubuhuo");

deleteCell("student","1003","info", "name");
}

HBase快速入门系列(6) | Hbase简单的API操作_hadoop_02

HBase快速入门系列(6) | Hbase简单的API操作_zookeeper_03

我们可以看到删除了最新的一条

// 删除最新版本
public static void deleteCells(String tableName,String rowKey,String family,String column) throws IOException {

Table table = connection.getTable(TableName.valueOf(tableName));

Delete delete = new Delete(Bytes.toBytes(rowKey));

delete.addColumns(Bytes.toBytes(family),Bytes.toBytes(column));

table.delete(delete);

table.close();
}

// 测试
public static void main(String[] args)throws IOException{

putCell("student","1003","info", "name","buwen");
putCell("student","1003","info", "name","bubuhuo");

deleteCells("student","1003","info", "name");
}

HBase快速入门系列(6) | Hbase简单的API操作_hbase_04

HBase快速入门系列(6) | Hbase简单的API操作_hbase_05

  • 7. 查看一行数据
// 查找一行数据
public static void getRow(String tableName,String rowKey) throws IOException {

Table table = connection.getTable(TableName.valueOf(tableName));

Get get = new Get(Bytes.toBytes(rowKey));

Result result = table.get(get);

Cell[] cells = result.rawCells();

for (Cell cell : cells){

byte[] columnBytes = CellUtil.cloneQualifier(cell);

String columnStr = Bytes.toString(columnBytes);

byte[] valueBytes = CellUtil.cloneValue(cell);

String valueStr = Bytes.toString(valueBytes);

System.out.println(columnStr + ":" + valueStr);
}
table.close();
}

public static void main(String[] args)throws IOException{

putCell("student","1003","info", "age","18");
putCell("student","1003","info", "gender","boy");

// deleteCells("student","1003","info", "name");
getRow("student","1003");
}

HBase快速入门系列(6) | Hbase简单的API操作_数据_06

  • 8. 查看指定范围内数据
public static void getRows(String tableName,String startRow,String stopRow) throws IOException {

Table table = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow));

ResultScanner scanner = table.getScanner(scan);

for (Result result : scanner) {

for (Cell cell : result.rawCells()) {

String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
String column = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));

System.out.println(rowKey + "-" + column + ":" + value);
}
}

scanner.close();
table.close();

}

public static void main(String[] args)throws IOException{
getRows("student","1002","1003!");

}

HBase快速入门系列(6) | Hbase简单的API操作_数据_07

  • 9. 过滤(单个)
public static  void getRowsByColumn(String tableName,String family,String column, String value) throws IOException {

Table table = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan();

SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter(Bytes.toBytes(family), Bytes.toBytes(column), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(value));
filter.setFilterIfMissing(true); // 过滤器
scan.setFilter(filter);

ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {

for (Cell cell : result.rawCells()) {

String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
String columnStr = Bytes.toString(CellUtil.cloneQualifier(cell));
String valueStr = Bytes.toString(CellUtil.cloneValue(cell));

System.out.println(rowKey + "-" + columnStr + ":" + valueStr);
}
}

scanner.close();
table.close();

}

public static void main(String[] args) throws IOException {
getRowsByColumn("student","info","age","18");

}

}
  • 10. 过滤(多个)
//过滤(多个)
public static void getRowsByColumns(String tableName, String family, Map<String,String> map) throws IOException {

Table table = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan();

SingleColumnValueExcludeFilter filter1 = new SingleColumnValueExcludeFilter(Bytes.toBytes(family), Bytes.toBytes(map.keySet().toArray()[0].toString()), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(map.get(map.keySet().toArray()[0].toString())));
filter1.setFilterIfMissing(true);

SingleColumnValueExcludeFilter filter2 = new SingleColumnValueExcludeFilter(Bytes.toBytes(family), Bytes.toBytes(map.keySet().toArray()[1].toString()), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(map.get(map.keySet().toArray()[1].toString())));
filter2.setFilterIfMissing(true);

FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);

filterList.addFilter(filter1);
filterList.addFilter(filter2);

scan.setFilter(filterList);

// da yin
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {

for (Cell cell : result.rawCells()) {

String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
String columnStr = Bytes.toString(CellUtil.cloneQualifier(cell));
String valueStr = Bytes.toString(CellUtil.cloneValue(cell));

System.out.println(rowKey + "-" + columnStr + ":" + valueStr);
}
}

scanner.close();
table.close();

}

public static void main(String[] args) throws IOException {

// putCell("student","1005","info", "name","lixuefang");
// putCell("student","1005","info", "gender","girl");
// putCell("student","1005","info", "age","18");

HashMap<String,String>map = new HashMap<String, String>();

map.put("age","18");
map.put("gender","girl");

getRowsByColumns("student","info",map);

}

HBase快速入门系列(6) | Hbase简单的API操作_zookeeper_08

  本次的分享就到这里了,


HBase快速入门系列(6) | Hbase简单的API操作_zookeeper_09


  好书不厌读百回,熟读课思子自知。而我想要成为全场最靓的仔,就必须坚持通过学习来获取更多知识,用知识改变命运,用博客见证成长,用行动证明我在努力。
  如果我的博客对你有帮助、如果你喜欢我的博客内容,请​​“点赞” “评论”“收藏”​​一键三连哦!听说点赞的人运气不会太差,每一天都会元气满满呦!如果实在要白嫖的话,那祝你开心每一天,欢迎常来我博客看看。
  码字不易,大家的支持就是我坚持下去的动力。点赞后不要忘了​​关注​​我哦!