hbase是Java编写的,当然也提供了Java的API来操作hbase。
如果你是使用虚拟机来安装配置hbase那么你需要配置一下hostname,不然JavaAPI访问虚拟机的时候会无法连接,请参考:
https://www.cnblogs.com/lay2017/p/9953371.html
同时请注意关闭防火墙,如果你的虚拟机启动会默认开启防火墙的话,你需要关闭。
一、依赖
hbase客户端依赖如下:
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.2.0</version> </dependency>
注意:hbase官方版本截止本文已经是2.1.1,但是这里使用1.2.0是由于官方文档并没有及时更新文档,所以对于client的使用你只能看到javadocs,很不方便。
二、代码示例
以下的代码约200行,但内容并不复杂,仅有以下三块内容
1、在static块里面初始化了hbase的连接
2、main方法里面调用增删改查等JavaAPI接口
3、最后还有一个close方法
我们先看一下输出:
完整代码如下
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import java.io.IOException; import java.util.Arrays; /** * @Description java api * @Author lay * @Date 2018/11/12 13:10 */ public class HbaseJavaApiDemo { private static Configuration configuration; private static Connection connection; private static Admin admin; private static final String ENCODE = "UTF-8"; static { // 创建configuration configuration = HBaseConfiguration.create(); // 设置HBase的zk地址和端口 configuration.set("hbase.zookeeper.quorum", "master"); configuration.set("hbase.zookeeper.property.clientPort", "2181"); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { try { String table = "t_user"; String row = "row1"; String columnFamily = "cf_name"; String column = "firstName"; String value = "lay"; deleteTable(table); createOrOverrideTable(table, columnFamily); listTables(); putData(table, row, columnFamily, column, value); getData(table, row, columnFamily, column); scanData(table); deleteData(table, row, columnFamily, column); } finally { close(); } } /** * 创建表 * @param table 表名 * @param columnFamily 列簇 * @throws IOException */ public static void createOrOverrideTable(String table, String columnFamily) throws IOException { TableName tableName = TableName.valueOf(table); HTableDescriptor tableDescriptor = new HTableDescriptor(tableName); // 添加一个列簇 HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnFamily); tableDescriptor.addFamily(hColumnDescriptor); // 存在则删除 deleteTable(table); admin.createTable(tableDescriptor); System.out.println(table + " 表创建完成"); } /** * 列出所有表 * @throws IOException */ public static void listTables() throws IOException { HTableDescriptor[] hTableDescriptors = admin.listTables(); System.out.println("列出所有的表:"); for (HTableDescriptor t : hTableDescriptors) { System.out.println(t.getTableName()); } } /** * 删除表 * @param table 表名 * @throws IOException */ public static void deleteTable(String table) throws IOException { TableName tableName = TableName.valueOf(table); if (admin.tableExists(tableName)) { admin.disableTable(tableName); admin.deleteTable(tableName); System.out.println(table + " 存在并执行删除"); } } /** * 添加数据 * @param table 表名 * @param row 行 * @param columnFamily 列簇 * @param column 列 * @param value 值 * @throws IOException */ public static void putData(String table, String row, String columnFamily, String column, String value) throws IOException { TableName tableName = TableName.valueOf(table); Put put = new Put(row.getBytes(ENCODE)); put.addColumn(columnFamily.getBytes(ENCODE), column.getBytes(ENCODE), value.getBytes(ENCODE)); Table iTable = connection.getTable(tableName); iTable.put(put); iTable.close(); System.out.println("数据添加完毕"); } /** * 查询数据 * @param table * @param row * @param columnFamily * @param column * @throws IOException */ public static void getData(String table, String row, String columnFamily, String column) throws IOException { TableName tableName = TableName.valueOf(table); Get get = new Get(row.getBytes(ENCODE)); Table iTable = connection.getTable(tableName); Result result = iTable.get(get); byte[] data = result.getValue(columnFamily.getBytes(ENCODE), column.getBytes(ENCODE)); System.out.println("查询的数据:" + new String(data)); iTable.close(); } /** * 删除数据 * @param table * @param row * @param columnFamily * @param column * @throws IOException */ public static void deleteData(String table, String row, String columnFamily, String column) throws IOException { TableName tableName = TableName.valueOf(table); Delete delete = new Delete(row.getBytes(ENCODE)); delete.addColumn(columnFamily.getBytes(ENCODE), column.getBytes(ENCODE)); Table iTable = connection.getTable(tableName); iTable.delete(delete); iTable.close(); System.out.println("数据删除完毕"); } /** * 扫描数据 * @param table * @throws IOException */ public static void scanData(String table) throws IOException { TableName tableName = TableName.valueOf(table); Scan scan = new Scan(); Table iTable = connection.getTable(tableName); ResultScanner resultScanner = iTable.getScanner(scan); for (Result r : resultScanner) { Cell[] cells = r.rawCells(); System.out.println("遍历的数据结果:"); Arrays.stream(cells).forEach(cell -> { String value = new String(CellUtil.cloneValue(cell)); System.out.println(value); }); } iTable.close(); } /** * 关闭admin和connection */ public static void close() { if (admin != null) { try { admin.close(); } catch (IOException e) { throw new RuntimeException(e); } } if (connection != null) { try { connection.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }