1. 添加依赖
代码语言:javascript复制 <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. 删除数据
①删除某一行
代码语言:javascript复制 // 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();
}
②删除整个列组
代码语言:javascript复制 // 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();
}
③删除某一列
代码语言:javascript复制// 删除所有版本
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");
}
我们可以看到删除了最新的一条
代码语言:javascript复制// 删除最新版本
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");
}
- 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");
}
- 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!");
}
- 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);
}
本次的分享就到这里了