版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15014327/article/details/83317855
使用Java调用HBase的常用操作。首先,新建一个Maven项目,导入HBase依赖,编写Java操作HBase的工具包。
1.新建Maven项目,导入HBase相关依赖
<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>cn.lv</groupId>
<artifactId>hbase-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.7</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
<build>
<finalName>hbase-study</finalName>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.编写代码
package cn.lv;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;
@SuppressWarnings("all")
public class HBaseOperationUtil {
/**
* 1.配置文件 2.会话连接 3.管理对象
*/
private static Logger logger = Logger.getLogger(HBaseOperationUtil.class);
private static Configuration conf = null;
private static Connection conn = null;
private static Admin admin = null;
/**
* 初始化
*/
static {
try {
conf = HBaseConfiguration.create();
conn = ConnectionFactory.createConnection(conf);
admin = conn.getAdmin();
logger.info("init complete successfully!");
} catch (IOException e) {
logger.error("init error" + e.getMessage());
}
}
/**
* 创建表
*
* @param tb
* @param cf
* @throws IOException
*/
public static void createHTable(String tb, String cf) throws IOException {
TableName tableName = TableName.valueOf(tb);
if (!admin.tableExists(tableName)) {
HTableDescriptor desc = new HTableDescriptor(tableName);
HColumnDescriptor family = new HColumnDescriptor(cf);
desc.addFamily(family);
admin.createTable(desc);
logger.info(tb + " create success!");
} else {
logger.warn(tb + " is already exists!");
}
}
/**
* 删除表
*
* @param tb
* @throws IOException
*/
public static void deleteTable(String tb) throws IOException {
TableName tableName = TableName.valueOf(tb);
admin.deleteTable(tableName);
logger.info(tb + " delete success!");
}
/**
* 插入数据
*
* @param tb
* @param rowKey
* @param family
* @param column
* @param value
* @throws IOException
*/
public static void put(String tb, String rowKey, String family, String column, String value) throws IOException {
Table table = conn.getTable(TableName.valueOf(tb));
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
table.close();
logger.info("put table " + tb + " data success!");
}
/**
* 查询数据
*
* @param tb
* @param rowKey
* @param family
* @param qualifier
* @return
* @throws IOException
*/
public static String get(String tb, String rowKey, String family, String qualifier) throws IOException {
Table table = conn.getTable(TableName.valueOf(tb));
Get get = new Get(Bytes.toBytes(rowKey));
get.addFamily(Bytes.toBytes(family));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
Result result = table.get(get);
return Bytes.toString(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier)));
}
/**
* 范围扫描
*
* @param tb
* @param startRow
* @param endRow
* @param family
* @param qualifier
* @return
* @throws IOException
*/
public static List<String> scan(String tb, String startRow, String endRow, String family, String qualifier)
throws IOException {
Table table = conn.getTable(TableName.valueOf(tb));
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(startRow));
scan.setStopRow(Bytes.toBytes(endRow));
scan.addFamily(Bytes.toBytes(family));
scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
ResultScanner result = table.getScanner(scan);
List<String> list = new ArrayList<String>();
for (Result r : result) {
list.add(Bytes.toString(r.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier))));
}
return list;
}
/**
* 条件查询
*
* @param tb
* @param family
* @param qualifier
* @return
* @throws IOException
*/
public static List<String> scan(String tb, String family, String qualifier) throws IOException {
Table table = conn.getTable(TableName.valueOf(tb));
Scan scan = new Scan();
scan.addFamily(Bytes.toBytes(family));
scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
ResultScanner result = table.getScanner(scan);
List<String> list = new ArrayList<String>();
for (Result r : result) {
list.add(Bytes.toString(r.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier))));
}
return list;
}
/**
* 全表扫描
*
* @param tb
* @return
* @throws IOException
*/
public static List<String> scan(String tb) throws IOException {
Table table = conn.getTable(TableName.valueOf(tb));
Scan scan = new Scan();
ResultScanner result = table.getScanner(scan);
List<String> list = new ArrayList<String>();
for (Result r : result) {
List<Cell> cells = r.listCells();
for (Cell cell : cells) {
StringBuilder sb = new StringBuilder();
sb.append("rowKey:").append(Bytes.toString(CellUtil.cloneRow(cell))).append("\t");
sb.append("family:").append(Bytes.toString(CellUtil.cloneFamily(cell))).append(",");
sb.append("qualifier:").append(Bytes.toString(CellUtil.cloneQualifier(cell))).append("=");
sb.append("value:").append(Bytes.toString(CellUtil.cloneValue(cell)));
list.add(sb.toString());
}
}
return list;
}
/**
* 删除数据
*
* @param tb
* @param rowKey
* @param family
* @param qualifier
* @throws IOException
*/
public static void delete(String tb, String rowKey, String family, String qualifier) throws IOException {
Table table = conn.getTable(TableName.valueOf(tb));
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumn(family.getBytes(), qualifier.getBytes());
table.delete(delete);
}
/**
* 关闭
*
* @throws IOException
*/
public static void close() throws IOException {
admin.close();
conn.close();
}
public static void main(String[] args) throws IOException {
// 插入数据
put("lv:emp", "10002", "info", "name", "lisi");
put("lv:emp", "10002", "info", "age", "22");
// 查询数据,只取name
List<String> list = scan("lv:emp", "info", "name");
// 删除数据,删除10001的age列
delete("lv:emp", "10001", "info", "age");
}
}