HBase:REST服務(wù)器

2018-08-10 11:18 更新

REST

REST代表狀態(tài)轉(zhuǎn)移,它于2000年在Roy Fielding的博士論文中引入,他是HTTP規(guī)范的主要作者之一。

REST本身超出了本文檔的范圍,但通常,REST允許通過與URL本身綁定的API進(jìn)行客戶端-服務(wù)器交互。本節(jié)討論如何配置和運(yùn)行HBase附帶的REST服務(wù)器,該服務(wù)器將HBase表,行,單元和元數(shù)據(jù)作為URL指定的資源公開。

啟動(dòng)和停止REST服務(wù)器

包含的REST服務(wù)器可以作為守護(hù)程序運(yùn)行,該守護(hù)程序啟動(dòng)嵌入式Jetty servlet容器并將servlet部署到其中。使用以下命令之一在前臺(tái)或后臺(tái)啟動(dòng)REST服務(wù)器。端口是可選的,默認(rèn)為8080。

# Foreground
$ bin/hbase rest start -p <port>

# Background, logging to a file in $HBASE_LOGS_DIR
$ bin/hbase-daemon.sh start rest -p <port>

要停止REST服務(wù)器,請(qǐng)?jiān)谇芭_(tái)運(yùn)行時(shí)使用Ctrl-C,如果在后臺(tái)運(yùn)行則使用以下命令。

$ bin/hbase-daemon.sh stop rest

配置REST服務(wù)器和客戶端

有關(guān)為SSL配置REST服務(wù)器和客戶端以及為REST服務(wù)器配置doAs模擬的信息,請(qǐng)參閱配置Thrift網(wǎng)關(guān)以代表客戶端進(jìn)行身份驗(yàn)證以及Securing Apache HBase章節(jié)的其他部分。

使用REST端點(diǎn)

以下示例使用占位符服務(wù)器http://example.com:8000,并且可以使用curl或wget命令運(yùn)行以下命令。您可以通過不為純文本添加頭信息來請(qǐng)求純文本(默認(rèn)),XML或JSON輸出,或者為XML添加頭信息“Accept:text / xml”,為JSON添加“Accept:application / json”或?yàn)閰f(xié)議緩沖區(qū)添加“Accept: application/x-protobuf”。

除非指定,否則使用GET請(qǐng)求進(jìn)行查詢,PUT或POST請(qǐng)求進(jìn)行創(chuàng)建或修改,DELETE用于刪除。

群集范圍的端點(diǎn)
端點(diǎn)HTTP動(dòng)詞描述示例

/version/cluster

GET

在此群集上運(yùn)行的HBase版本

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/version/cluster"

/status/cluster

GET

群集狀態(tài)

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/status/cluster"

/

GET

所有非系統(tǒng)表的列表

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/"
命名空間端點(diǎn)
端點(diǎn)HTTP動(dòng)詞描述示例

/namespaces

GET

列出所有命名空間

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/namespaces/"

/namespaces/namespace

GET

描述特定的命名空間

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/namespaces/special_ns"

/namespaces/namespace

POST

創(chuàng)建一個(gè)新的命名空間

curl -vi -X POST \
  -H "Accept: text/xml" \
  "example.com:8000/namespaces/special_ns"

/namespaces/namespace/tables

GET

列出特定命名空間中的所有表

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/namespaces/special_ns/tables"

/namespaces/namespace

PUT

更改現(xiàn)有命名空間。目前尚未使用

curl -vi -X PUT \
  -H "Accept: text/xml" \
  "http://example.com:8000/namespaces/special_ns

/namespaces/namespace

DELETE

刪除命名空間。命名空間必須為空

curl -vi -X DELETE \
  -H "Accept: text/xml" \
  "example.com:8000/namespaces/special_ns"
表端點(diǎn)
端點(diǎn)HTTP動(dòng)詞描述示例

/table/schema

GET

描述指定表的架構(gòu)

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/users/schema"

/table/schema

POST

使用提供的架構(gòu)片段更新現(xiàn)有表

curl -vi -X POST \
  -H "Accept: text/xml" \
  -H "Content-Type: text/xml" \
  -d '<?xml version="1.0" encoding="UTF-8"?><TableSchema name="users"><ColumnSchema name="cf" KEEP_DELETED_CELLS="true" /></TableSchema>' \
  "http://example.com:8000/users/schema"

/table/schema

PUT

創(chuàng)建新表,或替換現(xiàn)有表的架構(gòu)

curl -vi -X PUT \
  -H "Accept: text/xml" \
  -H "Content-Type: text/xml" \
  -d '<?xml version="1.0" encoding="UTF-8"?><TableSchema name="users"><ColumnSchema name="cf" /></TableSchema>' \
  "http://example.com:8000/users/schema"

/table/schema

DELETE

刪除表格。您必須使用端點(diǎn)/table/schema/,而不僅僅是table/

curl -vi -X DELETE \
  -H "Accept: text/xml" \
  "http://example.com:8000/users/schema"

/table/regions

GET

列出表區(qū)域

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/users/regions
Get操作的端點(diǎn)
端點(diǎn)HTTP動(dòng)詞描述示例

/table/row

GET

獲取單行的所有列。值為Base-64編碼。這需要“Accept”請(qǐng)求標(biāo)頭,其類型可以包含多個(gè)列(如xml,json或protobuf)。

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/users/row1"

/table/row/column:qualifier/timestamp

GET

獲取單個(gè)列的值。值為Base-64編碼。

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/users/row1/cf:a/1458586888395"

/table/row/column:qualifier

GET

獲取單個(gè)列的值。值為Base-64編碼。

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/users/row1/cf:a"

curl -vi -X GET \
  -H "Accept: text/xml" \
   "http://example.com:8000/users/row1/cf:a/"

/table/row/column:qualifier/?v=number_of_versions

GET

Multi-獲取給定單元格的指定數(shù)量的版本。值為Base-64編碼。

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/users/row1/cf:a?v=2"
 Scan操作的端點(diǎn)
端點(diǎn)HTTP動(dòng)詞描述示例

/table/scanner/

PUT

獲取Scanner對(duì)象。所有其他掃描操作都需要。將批處理參數(shù)調(diào)整為掃描應(yīng)在批處理中返回的行數(shù)。請(qǐng)參閱下一個(gè)向掃描儀添加過濾器的示例。掃描程序端點(diǎn)URL將作為HTTP響應(yīng)中的Location返回。此表中的其他示例假定掃描程序端點(diǎn)為:http://example.com:8000/users/scanner/145869072824375522207。

curl -vi -X PUT \
  -H "Accept: text/xml" \
  -H "Content-Type: text/xml" \
  -d '<Scanner batch="1"/>' \
  "http://example.com:8000/users/scanner/"

/table/scanner/

PUT

要向掃描儀對(duì)象提供過濾器或以任何其他方式配置掃描儀,您可以創(chuàng)建文本文件并將過濾器添加到文件中。例如,要僅返回以<codeph> u123 </ codeph>開頭并使用批量大小為100的行,過濾器文件將如下所示:

[source,xml] ---- <Scanner batch =“100”> <filter> {“type”:“PrefixFilter”,“value”:“u123”} </ filter> </ Scanner> ----

將文件傳遞給curl請(qǐng)求的-d參數(shù)。

curl -vi -X PUT \
  -H "Accept: text/xml" \
  -H "Content-Type:text/xml" \
  -d @filter.txt \
  "http://example.com:8000/users/scanner/"

/table/scanner/scanner-id

GET

從掃描儀獲取下一批。單元格值是字節(jié)編碼的。如果掃描儀已耗盡,則返回HTTP狀態(tài)204。

curl -vi -X GET \
  -H "Accept: text/xml" \
  "http://example.com:8000/users/scanner/145869072824375522207"

table/scanner/scanner-id

DELETE

刪除掃描儀并釋放它使用的資源。

curl -vi -X DELETE \
  -H "Accept: text/xml" \
  "http://example.com:8000/users/scanner/145869072824375522207"
Put操作的端點(diǎn)
端點(diǎn)HTTP動(dòng)詞描述示例

/table/row_key

PUT

在表中寫一行。行、列限定符和值必須均為Base-64編碼。要對(duì)字符串進(jìn)行編碼,請(qǐng)使用base64命令行實(shí)用程序。要解碼字符串,請(qǐng)使用base64 -d。有效負(fù)載位于--data參數(shù)中,/users/fakerow值為占位符。通過將多行添加到<CellSet>元素中來插入多行。您還可以將要插入的數(shù)據(jù)保存到文件中,并使用-d @filename.txt語法將其傳遞給參數(shù)-d。

curl -vi -X PUT \
  -H "Accept: text/xml" \
  -H "Content-Type: text/xml" \
  -d '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><CellSet><Row key="cm93NQo="><Cell column="Y2Y6ZQo=">dmFsdWU1Cg==</Cell></Row></CellSet>' \
  "http://example.com:8000/users/fakerow"

curl -vi -X PUT \
  -H "Accept: text/json" \
  -H "Content-Type: text/json" \
  -d '{"Row":[{"key":"cm93NQo=", "Cell": [{"column":"Y2Y6ZQo=", "$":"dmFsdWU1Cg=="}]}]}'' \
  "example.com:8000/users/fakerow"

REST XML架構(gòu)

<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="RESTSchema">

  <element name="Version" type="tns:Version"></element>

  <complexType name="Version">
    <attribute name="REST" type="string"></attribute>
    <attribute name="JVM" type="string"></attribute>
    <attribute name="OS" type="string"></attribute>
    <attribute name="Server" type="string"></attribute>
    <attribute name="Jersey" type="string"></attribute>
  </complexType>

  <element name="TableList" type="tns:TableList"></element>

  <complexType name="TableList">
    <sequence>
      <element name="table" type="tns:Table" maxOccurs="unbounded" minOccurs="1"></element>
    </sequence>
  </complexType>

  <complexType name="Table">
    <sequence>
      <element name="name" type="string"></element>
    </sequence>
  </complexType>

  <element name="TableInfo" type="tns:TableInfo"></element>

  <complexType name="TableInfo">
    <sequence>
      <element name="region" type="tns:TableRegion" maxOccurs="unbounded" minOccurs="1"></element>
    </sequence>
    <attribute name="name" type="string"></attribute>
  </complexType>

  <complexType name="TableRegion">
    <attribute name="name" type="string"></attribute>
    <attribute name="id" type="int"></attribute>
    <attribute name="startKey" type="base64Binary"></attribute>
    <attribute name="endKey" type="base64Binary"></attribute>
    <attribute name="location" type="string"></attribute>
  </complexType>

  <element name="TableSchema" type="tns:TableSchema"></element>

  <complexType name="TableSchema">
    <sequence>
      <element name="column" type="tns:ColumnSchema" maxOccurs="unbounded" minOccurs="1"></element>
    </sequence>
    <attribute name="name" type="string"></attribute>
    <anyAttribute></anyAttribute>
  </complexType>

  <complexType name="ColumnSchema">
    <attribute name="name" type="string"></attribute>
    <anyAttribute></anyAttribute>
  </complexType>

  <element name="CellSet" type="tns:CellSet"></element>

  <complexType name="CellSet">
    <sequence>
      <element name="row" type="tns:Row" maxOccurs="unbounded" minOccurs="1"></element>
    </sequence>
  </complexType>

  <element name="Row" type="tns:Row"></element>

  <complexType name="Row">
    <sequence>
      <element name="key" type="base64Binary"></element>
      <element name="cell" type="tns:Cell" maxOccurs="unbounded" minOccurs="1"></element>
    </sequence>
  </complexType>

  <element name="Cell" type="tns:Cell"></element>

  <complexType name="Cell">
    <sequence>
      <element name="value" maxOccurs="1" minOccurs="1">
        <simpleType><restriction base="base64Binary">
        </simpleType>
      </element>
    </sequence>
    <attribute name="column" type="base64Binary" />
    <attribute name="timestamp" type="int" />
  </complexType>

  <element name="Scanner" type="tns:Scanner"></element>

  <complexType name="Scanner">
    <sequence>
      <element name="column" type="base64Binary" minOccurs="0" maxOccurs="unbounded"></element>
    </sequence>
    <sequence>
      <element name="filter" type="string" minOccurs="0" maxOccurs="1"></element>
    </sequence>
    <attribute name="startRow" type="base64Binary"></attribute>
    <attribute name="endRow" type="base64Binary"></attribute>
    <attribute name="batch" type="int"></attribute>
    <attribute name="startTime" type="int"></attribute>
    <attribute name="endTime" type="int"></attribute>
  </complexType>

  <element name="StorageClusterVersion" type="tns:StorageClusterVersion" />

  <complexType name="StorageClusterVersion">
    <attribute name="version" type="string"></attribute>
  </complexType>

  <element name="StorageClusterStatus"
    type="tns:StorageClusterStatus">
  </element>

  <complexType name="StorageClusterStatus">
    <sequence>
      <element name="liveNode" type="tns:Node"
        maxOccurs="unbounded" minOccurs="0">
      </element>
      <element name="deadNode" type="string" maxOccurs="unbounded"
        minOccurs="0">
      </element>
    </sequence>
    <attribute name="regions" type="int"></attribute>
    <attribute name="requests" type="int"></attribute>
    <attribute name="averageLoad" type="float"></attribute>
  </complexType>

  <complexType name="Node">
    <sequence>
      <element name="region" type="tns:Region"
   maxOccurs="unbounded" minOccurs="0">
      </element>
    </sequence>
    <attribute name="name" type="string"></attribute>
    <attribute name="startCode" type="int"></attribute>
    <attribute name="requests" type="int"></attribute>
    <attribute name="heapSizeMB" type="int"></attribute>
    <attribute name="maxHeapSizeMB" type="int"></attribute>
  </complexType>

  <complexType name="Region">
    <attribute name="name" type="base64Binary"></attribute>
    <attribute name="stores" type="int"></attribute>
    <attribute name="storefiles" type="int"></attribute>
    <attribute name="storefileSizeMB" type="int"></attribute>
    <attribute name="memstoreSizeMB" type="int"></attribute>
    <attribute name="storefileIndexSizeMB" type="int"></attribute>
  </complexType>

</schema>

REST Protobufs架構(gòu)

message Version {
  optional string restVersion = 1;
  optional string jvmVersion = 2;
  optional string osVersion = 3;
  optional string serverVersion = 4;
  optional string jerseyVersion = 5;
}

message StorageClusterStatus {
  message Region {
    required bytes name = 1;
    optional int32 stores = 2;
    optional int32 storefiles = 3;
    optional int32 storefileSizeMB = 4;
    optional int32 memstoreSizeMB = 5;
    optional int32 storefileIndexSizeMB = 6;
  }
  message Node {
    required string name = 1;    // name:port
    optional int64 startCode = 2;
    optional int32 requests = 3;
    optional int32 heapSizeMB = 4;
    optional int32 maxHeapSizeMB = 5;
    repeated Region regions = 6;
  }
  // node status
  repeated Node liveNodes = 1;
  repeated string deadNodes = 2;
  // summary statistics
  optional int32 regions = 3;
  optional int32 requests = 4;
  optional double averageLoad = 5;
}

message TableList {
  repeated string name = 1;
}

message TableInfo {
  required string name = 1;
  message Region {
    required string name = 1;
    optional bytes startKey = 2;
    optional bytes endKey = 3;
    optional int64 id = 4;
    optional string location = 5;
  }
  repeated Region regions = 2;
}

message TableSchema {
  optional string name = 1;
  message Attribute {
    required string name = 1;
    required string value = 2;
  }
  repeated Attribute attrs = 2;
  repeated ColumnSchema columns = 3;
  // optional helpful encodings of commonly used attributes
  optional bool inMemory = 4;
  optional bool readOnly = 5;
}

message ColumnSchema {
  optional string name = 1;
  message Attribute {
    required string name = 1;
    required string value = 2;
  }
  repeated Attribute attrs = 2;
  // optional helpful encodings of commonly used attributes
  optional int32 ttl = 3;
  optional int32 maxVersions = 4;
  optional string compression = 5;
}

message Cell {
  optional bytes row = 1;       // unused if Cell is in a CellSet
  optional bytes column = 2;
  optional int64 timestamp = 3;
  optional bytes data = 4;
}

message CellSet {
  message Row {
    required bytes key = 1;
    repeated Cell values = 2;
  }
  repeated Row rows = 1;
}

message Scanner {
  optional bytes startRow = 1;
  optional bytes endRow = 2;
  repeated bytes columns = 3;
  optional int32 batch = 4;
  optional int64 startTime = 5;
  optional int64 endTime = 6;
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)