溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Hbase1.0.0-官方文檔-java-api-基礎

發(fā)布時間:2020-04-11 00:23:58 來源:網(wǎng)絡 閱讀:3487 作者:海德堡絕塵 欄目:大數(shù)據(jù)

@see 67.1.1. API as of HBase 1.0.0
@see 67.1.1. API as of HBase 1.0.0_detail

It’s been cleaned up and users are returned Interfaces to work against rather than particular types.
In HBase 1.0, obtain a Connection object from ConnectionFactory and thereafter, get from it instances of Table, Admin, and RegionLocator on an as-need basis.
When done, close the obtained instances.

舊的API已被清理,用戶使用接口,而不是特定的類來操作API。
在HBase 1.0中,從_ConnectionFactory獲取一個Connection對象,然后根據(jù)需要從Table,AdminRegionLocator_中獲取實例。完成后關閉。

Finally, be sure to cleanup your Connection instance before exiting.
Connections are heavyweight objects but thread-safe so you can create one for your application and keep the instance around.

最后,確保在退出之前 清理Connection 實例。
Connection重量級 的對象,但線程安全,所以你可以為你的應用程序創(chuàng)建一個,并保持實例。

Table, Admin and RegionLocator instances are lightweight.
Create as you go and then let go as soon as you are done by closing them.
See the Client Package Javadoc Description for example usage of the new HBase 1.0 API.

Table,AdminRegionLocator 實例是 輕量級 的。
隨時創(chuàng)建,然后一旦完成就放手關閉它們。
查看客戶端軟件包Javadoc說明,了解新的HBase 1.0 API的使用范例。

To administer HBase, create and drop tables, list and alter tables, use Admin.
Once created, table access is via an instance of Table.
You add content to a table a row at a time.

要管理HBase,create和delete表,list和alter表,請使用 Admin(DDL)。
表一旦創(chuàng)建,訪問表: Table(DML) 實例。
一次將加入一行內(nèi)容添加到表。

To insert, create an instance of a Put object.
Specify value, target column and optionally a timestamp.
Commit your update using Table.put(Put).

要插入,創(chuàng)建一個Put對象的實例。指定值、目標列、可選時間戳。
使用 _Table.put(Put)_提交更新。

To fetch your inserted value, use Get.
The Get can be specified to be broad -- get all on a particular row -- or narrow; i.e. return only a single cell value.
After creating an instance of Get, invoke Table.get(Get).

要獲取您插入的值,請使用Get。
Get可以被指定為寬泛的 - 獲取一個行的所有內(nèi)容 - 或可以縮小內(nèi)容范圍;
即只返回一個單元值。創(chuàng)建一個Get的實例后,調(diào)用 Table.get(Get)

Use Scan to set up a scanner -- a Cursor- like access.
After creating and configuring your Scan instance, call Table.getScanner(Scan) and then invoke next on the returned object.
Both Table.get(Get) and Table.getScanner(Scan) return a Result.

使用Scan 設置Scanner - 類似游標的訪問。
創(chuàng)建并配置Scan實例后,調(diào)用 Table.getScanner(Scan),然后在返回的對象上調(diào)用next。
_Table.get(Get)Table.getScanner(Scan) 都返回 Result_。

Use Delete to remove content.
You can remove individual cells or entire families, etc.
Pass it to Table.delete(Delete) to execute.

使用 Delete 刪除內(nèi)容。
你可以刪除獨個單元格或整個列族.
將 Delete 傳遞給 Table.delete(Delete) 來執(zhí)行。

Puts, Gets and Deletes take out a lock on the target row for the duration of their operation.
Concurrent modifications to a single row are serialized.
Gets and scans run concurrently without interference of the row locks and are guaranteed to not to return half written rows.

Put、GetDelete,會在操作期間對目標行取到 。
并行修改某一行是序列化的(一個一個來)。
GetScan 并發(fā)運行 沒有鎖 的干擾,并保證不返回一半的數(shù)據(jù)(返回半行數(shù)據(jù)就尷尬了)。

Client code accessing a cluster finds the cluster by querying ZooKeeper.
This means that the ZooKeeper quorum to use must be on the client CLASSPATH.
Usually this means make sure the client can find your hbase-site.xml.

訪問群集的客戶端代碼通過查詢ZooKeeper來查找群集。
這意味著要使用的ZooKeeper仲裁必須位于客戶端CLASSPATH上。
通常這意味著確??蛻舳丝梢哉业侥愕膆base-site.xml。

Hbase1.0.0-官方文檔-java-api-基礎

package com.niewj.util;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Table;
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.util.Bytes;

// Put, Get ,Scan 針對一個hbase表.  API 基于HBase 1.0.
public class MyLittleHBaseClient {
    public static void main(String[] args) throws IOException {

        // 庫里已經(jīng)有,可以通過hbase shell 創(chuàng)建:create 'zoo_htable_1', 'cf1'
        String tableNameString = "zoo_htable_1"; 

        Configuration config = HBaseConfiguration.create();

        // 從zk配置加載hbase信息
        config.set("hbase.zookeeper.quorum", "master.14.niewj.spark,slave1.146.niewj.spark,slave2.207.niewj.spark");
        config.set("hbase.zookeeper.property.clientPort", "2181");
        config.set("zookeeper.znode.parent", "/hbase");

        // 1. 接下來,您需要連接到群集。 創(chuàng)建一個。 當它完成后, 關閉它。
        // 2. try / finally是確保它被關閉的一個好方法(或使用jdk7,try-with-resources)
        // 3. Connection是重量級的。創(chuàng)建一個并保持它。
        // 4. 從Connection中,可取Table實例以訪問Tables,管理集群的Admin實例以及RegionLocator,以查找集群上的區(qū)域。
        // 5. 與Connections相反,Table,Admin和RegionLocator實例是輕量級的; 根據(jù)需要創(chuàng)建,然后在完成后關閉。
        Connection connection = ConnectionFactory.createConnection(config);
        try {

            // 1. 下面實例化一個Table對象,它將您連接到“zoo_htable_1”表(TableName.valueOf將String轉換為TableName實例)。
            // 2. 當它完成后,關閉它(應該開始一個嘗試/終于在這個創(chuàng)建后,所以它被關閉的肯定
            Table table = connection.getTable(TableName.valueOf(tableNameString));
            try {

                // 1. 要添加到行中,請使用Put。 Put構造函數(shù)將要插入的行的名稱作為字節(jié)數(shù)組。
                // 在HBase中,Bytes類具有將各種java類型轉換為字節(jié)數(shù)組的實用工具。
                // 在下面,我們將字符串“myLittleRow”轉換成一個字節(jié)數(shù)組作為我們更新的rowkey。
                // 一旦你有了一個Put實例,你可以設置行上更新的column的名稱,使用的時間戳等: 如果沒有時間戳,服務器將當前時間用于編輯。
                Put p = new Put(Bytes.toBytes("rk_100001"));

                // 在“rk_100001”行設置要更新的值,請指定單元格的列族、列名和值。
                // 該列族必須已經(jīng)存在。列明可以是任何東西。 所有值必須為字節(jié)數(shù)組,因為hbase全部是關于字節(jié)數(shù)組的。
                // 讓我們假裝表myLittleHBaseTable是用 cf1 系列創(chuàng)建的
                p.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("uname"), Bytes.toBytes("喬布斯"));

                // 所有更新在Put實例中,提交它使用 HTable#put 推送到hbase
                table.put(p);

                // 現(xiàn)在,檢索剛剛寫入的數(shù)據(jù)。 返回的值是Result實例。 結果是一個hbase返回最可口形式的對象
                Get g = new Get(Bytes.toBytes("rk_100001"));
                Result r = table.get(g);
                byte[] value = r.getValue(Bytes.toBytes("cf1"), Bytes.toBytes("uname"));

                // 將字節(jié)值轉換,返回插入的實際值
                String valueStr = Bytes.toString(value);
                System.out.println("\t GET: " + valueStr);

                // 有時候,你不知道你要找的那一行. 在本例中,您使用了一個Scanner。為表內(nèi)容提供類似指針的接口。 設置 Scanner, 就像組裝 Put 和 Get一樣創(chuàng)建一個Scan. 用column names裝飾.
                Scan s = new Scan();
                s.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("uname"));
                ResultScanner scanner = table.getScanner(s);
                try {
                    // Scanners 返回 Result 實例. 迭代結果方法一:
                    for (Result result = scanner.next(); result != null; result = scanner.next()) {
                        // 打印出查得的 columns內(nèi)容
                        System.out.println("Found row: " + result);
                    }

                    // 另一種方法是使用foreach循環(huán)。scanner是iterable
                    // for (Result rr : scanner) {
                    //   System.out.println("Found row: " + rr);
                    // }
                } finally {
                    // 確保使用完后關閉 scanners! 所以放入finally:
                    scanner.close();
                }

                // 關閉table、關閉connection
            } finally {
                if (table != null) {
                    table.close();
                }
            }
        } finally {
            connection.close();
        }
    }
}

Hbase1.0.0-官方文檔-java-api-基礎

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI