Hbase入門(五)——客戶端(Java,Shell,Thrift,Rest,MR,WebUI)

来源:https://www.cnblogs.com/tree1123/archive/2019/10/14/11670216.html
-Advertisement-
Play Games

Hbase的客戶端有原生java客戶端,Hbase Shell,Thrift,Rest,Mapreduce,WebUI等等。 下麵是這幾種客戶端的常見用法。 一、原生Java客戶端 原生java客戶端是hbase最主要,最高效的客戶端。 涵蓋了增刪改查等API,還實現了創建,刪除,修改表等DDL操作 ...


file

Hbase的客戶端有原生java客戶端,Hbase Shell,Thrift,Rest,Mapreduce,WebUI等等。

下麵是這幾種客戶端的常見用法。

file

一、原生Java客戶端

原生java客戶端是hbase最主要,最高效的客戶端。

涵蓋了增刪改查等API,還實現了創建,刪除,修改表等DDL操作。

配置java連接hbase

Java連接HBase需要兩個類:

  • HBaseConfiguration
  • ConnectionFactory

首先,配置一個hbase連接:

比如zookeeper的地址埠
hbase.zookeeper.quorum
hbase.zookeeper.property.clientPort

更通用的做法是編寫hbase-site.xml文件,實現配置文件的載入:

hbase-site.xml示例:

<configuration>

<property>
<name>hbase.master</name>
<value>hdfs://host1:60000</value>
</property>

<property>
<name>hbase.zookeeper.quorum</name>
<value>host1,host2,host3</value>
</property>

<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
</configuration>

隨後我們載入配置文件,創建連接:

 config.addResource(new Path(System.getenv("HBASE_CONF_DIR"), "hbase-site.xml"));
 Connection connection = ConnectionFactory.createConnection(config);

創建表

要創建表我們需要首先創建一個Admin對象

Admin admin = connection.getAdmin(); //使用連接對象獲取Admin對象
TableName tableName = TableName.valueOf("test");//定義表名

HTableDescriptor htd = new HTableDescriptor(tableName);//定義表對象

HColumnDescriptor hcd = new HColumnDescriptor("data");//定義列族對象

htd.addFamily(hcd); //添加

admin.createTable(htd);//創建表

HBase2.X創建表

HBase2.X 的版本中創建表使用了新的 API

TableName tableName = TableName.valueOf("test");//定義表名
//TableDescriptor對象通過TableDescriptorBuilder構建;
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("data")).build();//構建列族對象
tableDescriptor.setColumnFamily(family);//設置列族
admin.createTable(tableDescriptor.build());//創建表

添加數據

Table table = connection.getTable(tableName);//獲取Table對象
try {
    byte[] row = Bytes.toBytes("row1"); //定義行
    Put put = new Put(row);             //創建Put對象
    byte[] columnFamily = Bytes.toBytes("data");    //列
    byte[] qualifier = Bytes.toBytes(String.valueOf(1)); //列族修飾詞
    byte[] value = Bytes.toBytes("張三豐");    //值
    put.addColumn(columnFamily, qualifier, value);
    table.put(put);     //向表中添加數據

} finally {
    //使用完了要釋放資源
    table.close();
}

獲取指定行數據

//獲取數據
Get get = new Get(Bytes.toBytes("row1"));   //定義get對象
Result result = table.get(get);         //通過table對象獲取數據
System.out.println("Result: " + result);
//很多時候我們只需要獲取“值” 這裡表示獲取 data:1 列族的值
byte[] valueBytes = result.getValue(Bytes.toBytes("data"), Bytes.toBytes("1")); //獲取到的是位元組數組
//將位元組轉成字元串
String valueStr = new String(valueBytes,"utf-8");
System.out.println("value:" + valueStr);

掃描表中的數據

Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
try {
    for (Result scannerResult: scanner) {
        System.out.println("Scan: " + scannerResult);
         byte[] row = scannerResult.getRow();
         System.out.println("rowName:" + new String(row,"utf-8"));
    }
} finally {
    scanner.close();
}

刪除表

TableName tableName = TableName.valueOf("test");
admin.disableTable(tableName);  //禁用表
admin.deleteTable(tableName);   //刪除表

Hbase Java API表DDL完整示例:

package com.example.hbase.admin;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
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.io.compress.Compression.Algorithm;

public class Example {

  private static final String TABLE_NAME = "MY_TABLE_NAME_TOO";
  private static final String CF_DEFAULT = "DEFAULT_COLUMN_FAMILY";

  public static void createOrOverwrite(Admin admin, HTableDescriptor table) throws IOException {
    if (admin.tableExists(table.getTableName())) {
      admin.disableTable(table.getTableName());
      admin.deleteTable(table.getTableName());
    }
    admin.createTable(table);
  }

  public static void createSchemaTables(Configuration config) throws IOException {
    try (Connection connection = ConnectionFactory.createConnection(config);
         Admin admin = connection.getAdmin()) {

      HTableDescriptor table = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
      table.addFamily(new HColumnDescriptor(CF_DEFAULT).setCompressionType(Algorithm.NONE));

      System.out.print("Creating table. ");
      createOrOverwrite(admin, table);
      System.out.println(" Done.");
    }
  }

  public static void modifySchema (Configuration config) throws IOException {
    try (Connection connection = ConnectionFactory.createConnection(config);
         Admin admin = connection.getAdmin()) {

      TableName tableName = TableName.valueOf(TABLE_NAME);
      if (!admin.tableExists(tableName)) {
        System.out.println("Table does not exist.");
        System.exit(-1);
      }

      HTableDescriptor table = admin.getTableDescriptor(tableName);

      // 更新表格
      HColumnDescriptor newColumn = new HColumnDescriptor("NEWCF");
      newColumn.setCompactionCompressionType(Algorithm.GZ);
      newColumn.setMaxVersions(HConstants.ALL_VERSIONS);
      admin.addColumn(tableName, newColumn);

      // 更新列族
      HColumnDescriptor existingColumn = new HColumnDescriptor(CF_DEFAULT);
      existingColumn.setCompactionCompressionType(Algorithm.GZ);
      existingColumn.setMaxVersions(HConstants.ALL_VERSIONS);
      table.modifyFamily(existingColumn);
      admin.modifyTable(tableName, table);

      // 禁用表格
      admin.disableTable(tableName);

      // 刪除列族
      admin.deleteColumn(tableName, CF_DEFAULT.getBytes("UTF-8"));

      // 刪除表格(需提前禁用)
      admin.deleteTable(tableName);
    }
  }

  public static void main(String... args) throws IOException {
    Configuration config = HBaseConfiguration.create();

    //添加必要配置文件(hbase-site.xml, core-site.xml)
    config.addResource(new Path(System.getenv("HBASE_CONF_DIR"), "hbase-site.xml"));
    config.addResource(new Path(System.getenv("HADOOP_CONF_DIR"), "core-site.xml"));
    createSchemaTables(config);
    modifySchema(config);
  }
}

二、使用Hbase Shell工具操作Hbase

在 HBase 安裝目錄 bin/ 目錄下使用hbase shell命令連接正在運行的 HBase 實例。

$ ./bin/hbase shell
hbase(main):001:0>
預覽 HBase Shell 的幫助文本

輸入help並回車, 可以看到 HBase Shell 的基本信息和一些示例命令.

創建表

使用 create創建一個表 必須指定一個表名和列族名

hbase(main):001:0> create 'test', 'cf'
0 row(s) in 0.4170 seconds

=> Hbase::Table - test
表信息

使用 list 查看存在表

hbase(main):002:0> list 'test'
TABLE
test
1 row(s) in 0.0180 seconds

=> ["test"]
使用 describe 查看表細節及配置
hbase(main):003:0> describe 'test'
Table test is ENABLED
test
COLUMN FAMILIES DESCRIPTION
{NAME => 'cf', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE =>
'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'f
alse', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE
 => '65536'}
1 row(s)
Took 0.9998 seconds
插入數據

使用 put 插入數據

hbase(main):003:0> put 'test', 'row1', 'cf:a', 'value1'
0 row(s) in 0.0850 seconds

hbase(main):004:0> put 'test', 'row2', 'cf:b', 'value2'
0 row(s) in 0.0110 seconds

hbase(main):005:0> put 'test', 'row3', 'cf:c', 'value3'
0 row(s) in 0.0100 seconds
掃描全部數據

從 HBase 獲取數據的途徑之一就是 scan 。使用 scan 命令掃描表數據。你可以對掃描做限制。

hbase(main):006:0> scan 'test'
ROW                                      COLUMN+CELL
 row1                                    column=cf:a, timestamp=1421762485768, value=value1
 row2                                    column=cf:b, timestamp=1421762491785, value=value2
 row3                                    column=cf:c, timestamp=1421762496210, value=value3
3 row(s) in 0.0230 seconds
獲取一條數據

使用 get 命令一次獲取一條數據

hbase(main):007:0> get 'test', 'row1'
COLUMN                                   CELL
 cf:a                                    timestamp=1421762485768, value=value1
1 row(s) in 0.0350 seconds
禁用表

使用 disable 命令禁用表

hbase(main):008:0> disable 'test'
0 row(s) in 1.1820 seconds

hbase(main):009:0> enable 'test'
0 row(s) in 0.1770 seconds

使用 enable 命令啟用表

hbase(main):010:0> disable 'test'
0 row(s) in 1.1820 seconds
刪除表
hbase(main):011:0> drop 'test'
0 row(s) in 0.1370 seconds
退出 HBase Shell

使用quit命令退出命令行並從集群斷開連接。

三、使用Thrift客戶端訪問HBase

由於Hbase是用Java寫的,因此它原生地提供了Java介面,對非Java程式人員,怎麼辦呢?幸好它提供了thrift介面伺服器,因此也可以採用其他語言來編寫Hbase的客戶端,這裡是常用的Hbase python介面的介紹。其他語言也類似。

1.啟動thrift-server

要使用Hbase的thrift介面,必須將它的服務啟動,啟動Hbase的thrift-server進程如下:

cd /app/zpy/hbase/bin
./hbase-daemon.sh start thrift 
執行jps命令檢查:
34533 ThriftServer

thrift預設埠是9090,啟動成功後可以查看埠是否起來。

2.安裝thrift所需依賴

(1)安裝依賴

yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel openssl-devel

(2)安裝boost

wget http://sourceforge.net/projects/boost/files/boost/1.53.0/boost_1_53_0.tar.gz 
tar xvf boost_1_53_0.tar.gz 
cd boost_1_53_0 
./bootstrap.sh 
./b2 install

3.安裝thrift客戶端

官網下載 thrift-0.11.0.tar.gz,解壓並安裝

wget http://mirrors.hust.edu.cn/apache/thrift/0.11.0/thrift-0.11.0.tar.gz
tar xzvf thrift-0.11.0.tar.gz
cd thrift-0.11.0
mkdir /app/zpy/thrift
./configure --prefix=/app/zpy/thrift
make 
make install

make可能報錯如下:

g++: error: /usr/lib64/libboost_unit_test_framework.a: No such file or directory

解決:

find / -name libboost_unit_test_framework.*
cp /usr/local/lib/libboost_unit_test_framework.a  /usr/lib64/

4.使用python3連接Hbase

安裝所需包

pip install thrift
pip install hbase-thrift

python 腳本如下:

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

from hbase import Hbase
from hbase.ttypes import *

transport = TSocket.TSocket('localhost', 9090)
protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)
transport.open()
a = client.getTableNames()
print(a)

四、Rest客戶端

1、啟動REST服務 

  a.啟動一個非守護進程模式的REST伺服器(ctrl+c 終止)

     bin/hbase rest start 

  b.啟動守護進程模式的REST伺服器

     bin/hbase-daemon.sh start rest

預設啟動的是8080埠(可以使用參數在啟動時指定埠),可以被訪問。curl  http://:8080/

2、java調用示例:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
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.rest.client.Client;
import org.apache.hadoop.hbase.rest.client.Cluster;
import org.apache.hadoop.hbase.rest.client.RemoteHTable;
import org.apache.hadoop.hbase.util.Bytes;
import util.HBaseHelper;
import java.io.IOException;

/**
 * Created by root on 15-1-9.
 */
public class RestExample {
    public static void main(String[] args) throws IOException {
       Configuration conf = HBaseConfiguration.create();

       HBaseHelper helper = HBaseHelper.getHelper(conf);
       helper.dropTable("testtable");
       helper.createTable("testtable", "colfam1");
       System.out.println("Adding rows to table...");
       helper.fillTable("testtable", 1, 10, 5, "colfam1");

       Cluster cluster=new Cluster();
       cluster.add("hadoop",8080);

       Client client=new Client(cluster);
 

       Get get = new Get(Bytes.toBytes("row-30")); 
       get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-3"));
       Result result1 = table.get(get);
 
       System.out.println("Get result1: " + result1);

       Scan scan = new Scan();
       scan.setStartRow(Bytes.toBytes("row-10"));
       scan.setStopRow(Bytes.toBytes("row-15"));
       scan.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-5"));
       ResultScanner scanner = table.getScanner(scan); 
       for (Result result2 : scanner) {
         System.out.println("Scan row[" + Bytes.toString(result2.getRow()) +
                    "]: " + result2);
        }
    }
}

五、MapReduce操作Hbase

Apache MapReduce 是Hadoop提供的軟體框架,用來進行大規模數據分析.

mapred and mapreduce

與 MapReduce 一樣,在 HBase 中也有 2 種 mapreduce API 包.org.apache.hadoop.hbase.mapred and org.apache.hadoop.hbase.mapreduce.前者使用舊式風格的 API,後者採用新的模式.相比於前者,後者更加靈活。

HBase MapReduce 示例

HBase MapReduce 讀示例

Configuration config = HBaseConfiguration.create();
Job job = new Job(config, "ExampleRead");
job.setJarByClass(MyReadJob.class);     // class that contains mapper

Scan scan = new Scan();
scan.setCaching(500);        // 1 is the default in Scan, which will be bad for MapReduce jobs
scan.setCacheBlocks(false);  // don't set to true for MR jobs
// set other scan attrs
...

TableMapReduceUtil.initTableMapperJob(
  tableName,        // input HBase table name
  scan,             // Scan instance to control CF and attribute selection
  MyMapper.class,   // mapper
  null,             // mapper output key
  null,             // mapper output value
  job);
job.setOutputFormatClass(NullOutputFormat.class);   // because we aren't emitting anything from mapper

boolean b = job.waitForCompletion(true);
if (!b) {
  throw new IOException("error with job!");
}
public static class MyMapper extends TableMapper<Text, Text> {

  public void map(ImmutableBytesWritable row, Result value, Context context) throws InterruptedException, IOException {
    // process data for the row from the Result instance.
   }
}

HBase MapReduce 讀寫示例

Configuration config = HBaseConfiguration.create();
Job job = new Job(config,"ExampleReadWrite");
job.setJarByClass(MyReadWriteJob.class);    // class that contains mapper

Scan scan = new Scan();
scan.setCaching(500);        // 1 is the default in Scan, which will be bad for MapReduce jobs
scan.setCacheBlocks(false);  // don't set to true for MR jobs
// set other scan attrs

TableMapReduceUtil.initTableMapperJob(
  sourceTable,      // input table
  scan,             // Scan instance to control CF and attribute selection
  MyMapper.class,   // mapper class
  null,             // mapper output key
  null,             // mapper output value
  job);
TableMapReduceUtil.initTableReducerJob(
  targetTable,      // output table
  null,             // reducer class
  job);
job.setNumReduceTasks(0);

boolean b = job.waitForCompletion(true);
if (!b) {
    throw new IOException("error with job!");
}

六、Hbase Web UI

Hbase提供了一種Web方式的用戶介面,用戶可以通過Web界面查看Hbase集群的屬性等狀態信息,web頁面分為:Master狀態界面,和Zookeeper統計信息頁面。

預設訪問地址分別是:

ip:60010

ip::60030

ip:60010/zk.jsp

Master狀態界面會看到Master狀態的詳情。

該頁面大概分HBase集群信息,任務信息,表信息,RegionServer信息。每一部分又包含了一些具體的屬性。

RegionServer狀態界面會看到RegionServer狀態的詳情。

RegionServer的節點屬性信息,任務信息和Region信息。

Zookeeper統計信息頁面是非常簡單的半結構化文本列印信息。

file

更多實時計算,Hbase,Flink,Kafka等相關技術博文,歡迎關註實時流式計算

file

本文由博客一文多發平臺 OpenWrite 發佈!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 此題的題意很清晰: 日期相差一天(使用 ); 今天的溫度大於昨天。 ...
  • sql語句如下: SELECT * from A LEFT JOIN B ON A.aID = B.bID ...
  • 一、事務和隔離級別 事務的概念:事務是把對資料庫的一系列操作都看做一個整體,要麼全部成功,要麼全部失敗,利用事務我們可以保證資料庫的完整性,事務具有原子性。 隔離級別:隔離級別定義了事務與事務之間的隔離距離。 臟讀(dirty read):當一個事務讀取另一個事務未提交的修改時,產生臟讀。 不可重覆 ...
  • 一.mysql事件隔離級別 1未提交讀(READUNCOMMITTED) 另一個事務修改了數據,但尚未提交,而本事務中的SELECT會讀到這些未被提交的數據(臟讀)( 隔離級別最低,併發性能高 ) 2..提交讀(READCOMMITTED) 本事務讀取到的是最新的數據(其他事務提交後的)。問題是,在 ...
  • 前言 鏈表提供了高效的節點重排能力, 以及順序性的節點訪問方式, 並且可以通過增刪節點來靈活地調整鏈表的長度。 作為一種常用數據結構, 鏈表內置在很多高級的編程語言裡面, 因為 Redis 使用的 C 語言並沒有內置這種數據結構, 所以 Redis 構建了自己的鏈表實現。 大家可以把Redis的鏈表 ...
  • 1 Kylin是什麼 今天,隨著移動互聯網、物聯網、AI等技術的快速興起,數據成為了所有這些技術背後最重要,也是最有價值的“資產”。如何從數據中獲得有價值的信息?這個問題驅動了相關技術的發展,從最初的基於文件的檢索、分析程式,到數據倉庫理念的誕生,再到基於資料庫的商業智能分析。而現在,這一問題已經變 ...
  • 1、Redis 簡介 Redis 是一個支持數據結構更多的鍵值對資料庫。它的值不僅可以是字元串等基本數據 類型,也可以是類對象,更可以是 Set、List、計數器等高級的數據結構。 Memcached 也可以保存類似於 Set、List 這樣的結構,但是如果說要向 List 中增加元素, Memca ...
  • https://www.jb51.net/article/137925.htm https://blog.51cto.com/13706760/2171361 https://www.cnblogs.com/crazylqy/p/5542558.html https://blog.csdn.net/ ...
一周排行
    -Advertisement-
    Play Games
  • 1. 說明 /* Performs operations on System.String instances that contain file or directory path information. These operations are performed in a cross-pla ...
  • 視頻地址:【WebApi+Vue3從0到1搭建《許可權管理系統》系列視頻:搭建JWT系統鑒權-嗶哩嗶哩】 https://b23.tv/R6cOcDO qq群:801913255 一、在appsettings.json中設置鑒權屬性 /*jwt鑒權*/ "JwtSetting": { "Issuer" ...
  • 引言 集成測試可在包含應用支持基礎結構(如資料庫、文件系統和網路)的級別上確保應用組件功能正常。 ASP.NET Core 通過將單元測試框架與測試 Web 主機和記憶體中測試伺服器結合使用來支持集成測試。 簡介 集成測試與單元測試相比,能夠在更廣泛的級別上評估應用的組件,確認多個組件一起工作以生成預 ...
  • 在.NET Emit編程中,我們探討了運算操作指令的重要性和應用。這些指令包括各種數學運算、位操作和比較操作,能夠在動態生成的代碼中實現對數據的處理和操作。通過這些指令,開發人員可以靈活地進行算術運算、邏輯運算和比較操作,從而實現各種複雜的演算法和邏輯......本篇之後,將進入第七部分:實戰項目 ...
  • 前言 多表頭表格是一個常見的業務需求,然而WPF中卻沒有預設實現這個功能,得益於WPF強大的控制項模板設計,我們可以通過修改控制項模板的方式自己實現它。 一、需求分析 下圖為一個典型的統計表格,統計1-12月的數據。 此時我們有一個需求,需要將月份按季度劃分,以便能夠直觀地看到季度統計數據,以下為該需求 ...
  • 如何將 ASP.NET Core MVC 項目的視圖分離到另一個項目 在當下這個年代 SPA 已是主流,人們早已忘記了 MVC 以及 Razor 的故事。但是在某些場景下 SSR 還是有意想不到效果。比如某些靜態頁面,比如追求首屏載入速度的時候。最近在項目中回歸傳統效果還是不錯。 有的時候我們希望將 ...
  • System.AggregateException: 發生一個或多個錯誤。 > Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失敗。檢查輸出視窗瞭解更多詳細信息。 內部異常堆棧跟蹤的結尾 > (內部異常 #0) Microsoft ...
  • 引言 在上一章節我們實戰了在Asp.Net Core中的項目實戰,這一章節講解一下如何測試Asp.Net Core的中間件。 TestServer 還記得我們在集成測試中提供的TestServer嗎? TestServer 是由 Microsoft.AspNetCore.TestHost 包提供的。 ...
  • 在發現結果為真的WHEN子句時,CASE表達式的真假值判斷會終止,剩餘的WHEN子句會被忽略: CASE WHEN col_1 IN ('a', 'b') THEN '第一' WHEN col_1 IN ('a') THEN '第二' ELSE '其他' END 註意: 統一各分支返回的數據類型. ...
  • 在C#編程世界中,語法的精妙之處往往體現在那些看似微小卻極具影響力的符號與結構之中。其中,“_ =” 這一組合突然出現還真不知道什麼意思。本文將深入剖析“_ =” 的含義、工作原理及其在實際編程中的廣泛應用,揭示其作為C#語法奇兵的重要角色。 一、下劃線 _:神秘的棄元符號 下劃線 _ 在C#中並非 ...