Hbase java api
This commit is contained in:
38
code/Hbase/hbase-java-api-1.x/pom.xml
Normal file
38
code/Hbase/hbase-java-api-1.x/pom.xml
Normal file
@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.heibaiying</groupId>
|
||||
<artifactId>hbase-java-api-1.x</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.hbase</groupId>
|
||||
<artifactId>hbase-client</artifactId>
|
||||
<version>1.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,267 @@
|
||||
package com.heibaiying;
|
||||
|
||||
import javafx.util.Pair;
|
||||
import org.apache.hadoop.conf.Configuration;
|
||||
import org.apache.hadoop.hbase.HBaseConfiguration;
|
||||
import org.apache.hadoop.hbase.HColumnDescriptor;
|
||||
import org.apache.hadoop.hbase.HTableDescriptor;
|
||||
import org.apache.hadoop.hbase.TableName;
|
||||
import org.apache.hadoop.hbase.client.*;
|
||||
import org.apache.hadoop.hbase.filter.FilterList;
|
||||
import org.apache.hadoop.hbase.util.Bytes;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class HBaseUtils {
|
||||
|
||||
private static Connection connection;
|
||||
|
||||
static {
|
||||
Configuration configuration = HBaseConfiguration.create();
|
||||
configuration.set("hbase.zookeeper.property.clientPort", "2181");
|
||||
// 如果是集群 则主机名用逗号分隔
|
||||
configuration.set("hbase.zookeeper.quorum", "hadoop001");
|
||||
try {
|
||||
connection = ConnectionFactory.createConnection(configuration);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建HBase表
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param columnFamilies 列族的数组
|
||||
*/
|
||||
public static boolean createTable(String tableName, List<String> columnFamilies) {
|
||||
try {
|
||||
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
|
||||
if (admin.tableExists(tableName)) {
|
||||
return false;
|
||||
}
|
||||
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
|
||||
columnFamilies.forEach(columnFamily -> {
|
||||
HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamily);
|
||||
columnDescriptor.setMaxVersions(1);
|
||||
tableDescriptor.addFamily(columnDescriptor);
|
||||
});
|
||||
admin.createTable(tableDescriptor);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除hBase表
|
||||
*
|
||||
* @param tableName 表名
|
||||
*/
|
||||
public static boolean deleteTable(String tableName) {
|
||||
try {
|
||||
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
|
||||
// 删除表前需要先禁用表
|
||||
admin.disableTable(tableName);
|
||||
admin.deleteTable(tableName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rowKey 唯一标识
|
||||
* @param columnFamilyName 列族名
|
||||
* @param qualifier 列标识
|
||||
* @param value 数据
|
||||
*/
|
||||
public static boolean putRow(String tableName, String rowKey, String columnFamilyName, String qualifier,
|
||||
String value) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Put put = new Put(Bytes.toBytes(rowKey));
|
||||
put.addColumn(Bytes.toBytes(columnFamilyName), Bytes.toBytes(qualifier), Bytes.toBytes(value));
|
||||
table.put(put);
|
||||
table.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 插入数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rowKey 唯一标识
|
||||
* @param columnFamilyName 列族名
|
||||
* @param pairList 列标识和值的集合
|
||||
*/
|
||||
public static boolean putRow(String tableName, String rowKey, String columnFamilyName, List<Pair<String, String>> pairList) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Put put = new Put(Bytes.toBytes(rowKey));
|
||||
pairList.forEach(pair -> put.addColumn(Bytes.toBytes(columnFamilyName), Bytes.toBytes(pair.getKey()), Bytes.toBytes(pair.getValue())));
|
||||
table.put(put);
|
||||
table.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据rowKey获取指定行的数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rowKey 唯一标识
|
||||
*/
|
||||
public static Result getRow(String tableName, String rowKey) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Get get = new Get(Bytes.toBytes(rowKey));
|
||||
return table.get(get);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取指定行指定列(cell)的最新版本的数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rowKey 唯一标识
|
||||
* @param columnFamily 列族
|
||||
* @param qualifier 列标识
|
||||
*/
|
||||
public static String getCell(String tableName, String rowKey, String columnFamily, String qualifier) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Get get = new Get(Bytes.toBytes(rowKey));
|
||||
if (!get.isCheckExistenceOnly()) {
|
||||
get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier));
|
||||
Result result = table.get(get);
|
||||
byte[] resultValue = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier));
|
||||
return Bytes.toString(resultValue);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检索全表
|
||||
*
|
||||
* @param tableName 表名
|
||||
*/
|
||||
public static ResultScanner getScanner(String tableName) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Scan scan = new Scan();
|
||||
return table.getScanner(scan);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检索表中指定数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param filterList 过滤器
|
||||
*/
|
||||
|
||||
public static ResultScanner getScanner(String tableName, FilterList filterList) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Scan scan = new Scan();
|
||||
scan.setFilter(filterList);
|
||||
return table.getScanner(scan);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检索表中指定数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param startRowKey 起始RowKey
|
||||
* @param endRowKey 终止RowKey
|
||||
* @param filterList 过滤器
|
||||
*/
|
||||
|
||||
public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey,
|
||||
FilterList filterList) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Scan scan = new Scan();
|
||||
scan.setStartRow(Bytes.toBytes(startRowKey));
|
||||
scan.setStopRow(Bytes.toBytes(endRowKey));
|
||||
scan.setFilter(filterList);
|
||||
return table.getScanner(scan);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定行记录
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rowKey 唯一标识
|
||||
*/
|
||||
public static boolean deleteRow(String tableName, String rowKey) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Delete delete = new Delete(Bytes.toBytes(rowKey));
|
||||
table.delete(delete);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除指定行的指定列
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rowKey 唯一标识
|
||||
* @param familyName 列族
|
||||
* @param qualifier 列标识
|
||||
*/
|
||||
public static boolean deleteColumn(String tableName, String rowKey, String familyName,
|
||||
String qualifier) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Delete delete = new Delete(Bytes.toBytes(rowKey));
|
||||
delete.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(qualifier));
|
||||
table.delete(delete);
|
||||
table.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
package com.heibaiying;
|
||||
|
||||
import javafx.util.Pair;
|
||||
import org.apache.hadoop.hbase.client.Result;
|
||||
import org.apache.hadoop.hbase.client.ResultScanner;
|
||||
import org.apache.hadoop.hbase.filter.CompareFilter;
|
||||
import org.apache.hadoop.hbase.filter.FilterList;
|
||||
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
|
||||
import org.apache.hadoop.hbase.util.Bytes;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class HBaseUtilsTest {
|
||||
|
||||
private static final String TABLE_NAME = "class";
|
||||
private static final String TEACHER = "teacher";
|
||||
private static final String STUDENT = "student";
|
||||
|
||||
@Test
|
||||
public void createTable() {
|
||||
// 新建表
|
||||
List<String> columnFamilies = Arrays.asList(TEACHER, STUDENT);
|
||||
boolean table = HBaseUtils.createTable(TABLE_NAME, columnFamilies);
|
||||
System.out.println("表创建结果:" + table);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertData() {
|
||||
List<Pair<String, String>> pairs1 = Arrays.asList(new Pair<>("name", "Tom"),
|
||||
new Pair<>("age", "22"),
|
||||
new Pair<>("gender", "1"));
|
||||
HBaseUtils.putRow(TABLE_NAME, "rowKey1", STUDENT, pairs1);
|
||||
|
||||
List<Pair<String, String>> pairs2 = Arrays.asList(new Pair<>("name", "Jack"),
|
||||
new Pair<>("age", "33"),
|
||||
new Pair<>("gender", "2"));
|
||||
HBaseUtils.putRow(TABLE_NAME, "rowKey2", STUDENT, pairs2);
|
||||
|
||||
List<Pair<String, String>> pairs3 = Arrays.asList(new Pair<>("name", "Mike"),
|
||||
new Pair<>("age", "44"),
|
||||
new Pair<>("gender", "1"));
|
||||
HBaseUtils.putRow(TABLE_NAME, "rowKey3", STUDENT, pairs3);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getRow() {
|
||||
Result result = HBaseUtils.getRow(TABLE_NAME, "rowKey1");
|
||||
if (result != null) {
|
||||
System.out.println(Bytes
|
||||
.toString(result.getValue(Bytes.toBytes(STUDENT), Bytes.toBytes("name"))));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCell() {
|
||||
String cell = HBaseUtils.getCell(TABLE_NAME, "rowKey2", STUDENT, "age");
|
||||
System.out.println("cell age :" + cell);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getScanner() {
|
||||
ResultScanner scanner = HBaseUtils.getScanner(TABLE_NAME);
|
||||
if (scanner != null) {
|
||||
scanner.forEach(result -> System.out.println(Bytes.toString(result.getRow()) + "->" + Bytes
|
||||
.toString(result.getValue(Bytes.toBytes(STUDENT), Bytes.toBytes("name")))));
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getScannerWithFilter() {
|
||||
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
|
||||
SingleColumnValueFilter nameFilter = new SingleColumnValueFilter(Bytes.toBytes(STUDENT),
|
||||
Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("Jack"));
|
||||
filterList.addFilter(nameFilter);
|
||||
ResultScanner scanner = HBaseUtils.getScanner(TABLE_NAME, filterList);
|
||||
if (scanner != null) {
|
||||
scanner.forEach(result -> System.out.println(Bytes.toString(result.getRow()) + "->" + Bytes
|
||||
.toString(result.getValue(Bytes.toBytes(STUDENT), Bytes.toBytes("name")))));
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteColumn() {
|
||||
boolean b = HBaseUtils.deleteColumn(TABLE_NAME, "rowKey2", STUDENT, "age");
|
||||
System.out.println("删除结果: " + b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteRow() {
|
||||
boolean b = HBaseUtils.deleteRow(TABLE_NAME, "rowKey2");
|
||||
System.out.println("删除结果: " + b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteTable() {
|
||||
boolean b = HBaseUtils.deleteTable(TABLE_NAME);
|
||||
System.out.println("删除结果: " + b);
|
||||
}
|
||||
}
|
37
code/Hbase/hbase-java-api-2.x/pom.xml
Normal file
37
code/Hbase/hbase-java-api-2.x/pom.xml
Normal file
@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.heibaiying</groupId>
|
||||
<artifactId>hbase-java-api-2.x</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.hbase</groupId>
|
||||
<artifactId>hbase-client</artifactId>
|
||||
<version>2.1.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,266 @@
|
||||
package com.heibaiying;
|
||||
|
||||
import javafx.util.Pair;
|
||||
import org.apache.hadoop.conf.Configuration;
|
||||
import org.apache.hadoop.hbase.HBaseConfiguration;
|
||||
import org.apache.hadoop.hbase.TableName;
|
||||
import org.apache.hadoop.hbase.client.*;
|
||||
import org.apache.hadoop.hbase.filter.FilterList;
|
||||
import org.apache.hadoop.hbase.util.Bytes;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class HBaseUtils {
|
||||
|
||||
private static Connection connection;
|
||||
|
||||
static {
|
||||
Configuration configuration = HBaseConfiguration.create();
|
||||
configuration.set("hbase.zookeeper.property.clientPort", "2181");
|
||||
// 如果是集群 则主机名用逗号分隔
|
||||
configuration.set("hbase.zookeeper.quorum", "hadoop001");
|
||||
try {
|
||||
connection = ConnectionFactory.createConnection(configuration);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建HBase表
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param columnFamilies 列族的数组
|
||||
*/
|
||||
public static boolean createTable(String tableName, List<String> columnFamilies) {
|
||||
try {
|
||||
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
|
||||
if (admin.tableExists(TableName.valueOf(tableName))) {
|
||||
return false;
|
||||
}
|
||||
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
|
||||
columnFamilies.forEach(columnFamily -> {
|
||||
ColumnFamilyDescriptorBuilder cfDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily));
|
||||
cfDescriptorBuilder.setMaxVersions(1);
|
||||
ColumnFamilyDescriptor familyDescriptor = cfDescriptorBuilder.build();
|
||||
tableDescriptor.setColumnFamily(familyDescriptor);
|
||||
});
|
||||
admin.createTable(tableDescriptor.build());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除hBase表
|
||||
*
|
||||
* @param tableName 表名
|
||||
*/
|
||||
public static boolean deleteTable(String tableName) {
|
||||
try {
|
||||
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
|
||||
// 删除表前需要先禁用表
|
||||
admin.disableTable(TableName.valueOf(tableName));
|
||||
admin.deleteTable(TableName.valueOf(tableName));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rowKey 唯一标识
|
||||
* @param columnFamilyName 列族名
|
||||
* @param qualifier 列标识
|
||||
* @param value 数据
|
||||
*/
|
||||
public static boolean putRow(String tableName, String rowKey, String columnFamilyName, String qualifier,
|
||||
String value) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Put put = new Put(Bytes.toBytes(rowKey));
|
||||
put.addColumn(Bytes.toBytes(columnFamilyName), Bytes.toBytes(qualifier), Bytes.toBytes(value));
|
||||
table.put(put);
|
||||
table.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 插入数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rowKey 唯一标识
|
||||
* @param columnFamilyName 列族名
|
||||
* @param pairList 列标识和值的集合
|
||||
*/
|
||||
public static boolean putRow(String tableName, String rowKey, String columnFamilyName, List<Pair<String, String>> pairList) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Put put = new Put(Bytes.toBytes(rowKey));
|
||||
pairList.forEach(pair -> put.addColumn(Bytes.toBytes(columnFamilyName), Bytes.toBytes(pair.getKey()), Bytes.toBytes(pair.getValue())));
|
||||
table.put(put);
|
||||
table.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据rowKey获取指定行的数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rowKey 唯一标识
|
||||
*/
|
||||
public static Result getRow(String tableName, String rowKey) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Get get = new Get(Bytes.toBytes(rowKey));
|
||||
return table.get(get);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取指定行指定列(cell)的最新版本的数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rowKey 唯一标识
|
||||
* @param columnFamily 列族
|
||||
* @param qualifier 列标识
|
||||
*/
|
||||
public static String getCell(String tableName, String rowKey, String columnFamily, String qualifier) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Get get = new Get(Bytes.toBytes(rowKey));
|
||||
if (!get.isCheckExistenceOnly()) {
|
||||
get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier));
|
||||
Result result = table.get(get);
|
||||
byte[] resultValue = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier));
|
||||
return Bytes.toString(resultValue);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检索全表
|
||||
*
|
||||
* @param tableName 表名
|
||||
*/
|
||||
public static ResultScanner getScanner(String tableName) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Scan scan = new Scan();
|
||||
return table.getScanner(scan);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检索表中指定数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param filterList 过滤器
|
||||
*/
|
||||
|
||||
public static ResultScanner getScanner(String tableName, FilterList filterList) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Scan scan = new Scan();
|
||||
scan.setFilter(filterList);
|
||||
return table.getScanner(scan);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检索表中指定数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param startRowKey 起始RowKey
|
||||
* @param endRowKey 终止RowKey
|
||||
* @param filterList 过滤器
|
||||
*/
|
||||
|
||||
public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey,
|
||||
FilterList filterList) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Scan scan = new Scan();
|
||||
scan.withStartRow(Bytes.toBytes(startRowKey));
|
||||
scan.withStopRow(Bytes.toBytes(endRowKey));
|
||||
scan.setFilter(filterList);
|
||||
return table.getScanner(scan);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定行记录
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rowKey 唯一标识
|
||||
*/
|
||||
public static boolean deleteRow(String tableName, String rowKey) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Delete delete = new Delete(Bytes.toBytes(rowKey));
|
||||
table.delete(delete);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除指定行指定列
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rowKey 唯一标识
|
||||
* @param familyName 列族
|
||||
* @param qualifier 列标识
|
||||
*/
|
||||
public static boolean deleteColumn(String tableName, String rowKey, String familyName,
|
||||
String qualifier) {
|
||||
try {
|
||||
Table table = connection.getTable(TableName.valueOf(tableName));
|
||||
Delete delete = new Delete(Bytes.toBytes(rowKey));
|
||||
delete.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(qualifier));
|
||||
table.delete(delete);
|
||||
table.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
package heibaiying;
|
||||
|
||||
import com.heibaiying.HBaseUtils;
|
||||
import javafx.util.Pair;
|
||||
import org.apache.hadoop.hbase.CompareOperator;
|
||||
import org.apache.hadoop.hbase.client.Result;
|
||||
import org.apache.hadoop.hbase.client.ResultScanner;
|
||||
import org.apache.hadoop.hbase.filter.CompareFilter;
|
||||
import org.apache.hadoop.hbase.filter.FilterList;
|
||||
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
|
||||
import org.apache.hadoop.hbase.util.Bytes;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class HBaseUtilsTest {
|
||||
|
||||
private static final String TABLE_NAME = "class";
|
||||
private static final String TEACHER = "teacher";
|
||||
private static final String STUDENT = "student";
|
||||
|
||||
@Test
|
||||
public void createTable() {
|
||||
// 新建表
|
||||
List<String> columnFamilies = Arrays.asList(TEACHER, STUDENT);
|
||||
boolean table = HBaseUtils.createTable(TABLE_NAME, columnFamilies);
|
||||
System.out.println("表创建结果:" + table);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertData() {
|
||||
List<Pair<String, String>> pairs1 = Arrays.asList(new Pair<>("name", "Tom"),
|
||||
new Pair<>("age", "22"),
|
||||
new Pair<>("gender", "1"));
|
||||
HBaseUtils.putRow(TABLE_NAME, "rowKey1", STUDENT, pairs1);
|
||||
|
||||
List<Pair<String, String>> pairs2 = Arrays.asList(new Pair<>("name", "Jack"),
|
||||
new Pair<>("age", "33"),
|
||||
new Pair<>("gender", "2"));
|
||||
HBaseUtils.putRow(TABLE_NAME, "rowKey2", STUDENT, pairs2);
|
||||
|
||||
List<Pair<String, String>> pairs3 = Arrays.asList(new Pair<>("name", "Mike"),
|
||||
new Pair<>("age", "44"),
|
||||
new Pair<>("gender", "1"));
|
||||
HBaseUtils.putRow(TABLE_NAME, "rowKey3", STUDENT, pairs3);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getRow() {
|
||||
Result result = HBaseUtils.getRow(TABLE_NAME, "rowKey1");
|
||||
if (result != null) {
|
||||
System.out.println(Bytes
|
||||
.toString(result.getValue(Bytes.toBytes(STUDENT), Bytes.toBytes("name"))));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCell() {
|
||||
String cell = HBaseUtils.getCell(TABLE_NAME, "rowKey2", STUDENT, "age");
|
||||
System.out.println("cell age :" + cell);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getScanner() {
|
||||
ResultScanner scanner = HBaseUtils.getScanner(TABLE_NAME);
|
||||
if (scanner != null) {
|
||||
scanner.forEach(result -> System.out.println(Bytes.toString(result.getRow()) + "->" + Bytes
|
||||
.toString(result.getValue(Bytes.toBytes(STUDENT), Bytes.toBytes("name")))));
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getScannerWithFilter() {
|
||||
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
|
||||
SingleColumnValueFilter nameFilter = new SingleColumnValueFilter(Bytes.toBytes(STUDENT),
|
||||
Bytes.toBytes("name"), CompareOperator.EQUAL, Bytes.toBytes("Jack"));
|
||||
filterList.addFilter(nameFilter);
|
||||
ResultScanner scanner = HBaseUtils.getScanner(TABLE_NAME, filterList);
|
||||
if (scanner != null) {
|
||||
scanner.forEach(result -> System.out.println(Bytes.toString(result.getRow()) + "->" + Bytes
|
||||
.toString(result.getValue(Bytes.toBytes(STUDENT), Bytes.toBytes("name")))));
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteColumn() {
|
||||
boolean b = HBaseUtils.deleteColumn(TABLE_NAME, "rowKey2", STUDENT, "age");
|
||||
System.out.println("删除结果: " + b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteRow() {
|
||||
boolean b = HBaseUtils.deleteRow(TABLE_NAME, "rowKey2");
|
||||
System.out.println("删除结果: " + b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteTable() {
|
||||
boolean b = HBaseUtils.deleteTable(TABLE_NAME);
|
||||
System.out.println("删除结果: " + b);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user