1. 程式人生 > >三、hbase JavaAPI

三、hbase JavaAPI

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); } } } }