温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Hbase1.0.0-官方文档-java-api-基础

发布时间:2020-04-11 00:23:58 来源:网络 阅读:3469 作者:海德堡绝尘 栏目:大数据

@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对象,然后根据需要从TableAdminRegionLocator_中获取实例。完成后关闭。

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重量级 的对象,但线程安全,所以你可以为你的应用程序创建一个,并保持实例。

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 实例是 轻量级 的。
随时创建,然后一旦完成就放手关闭它们。
查看客户端软件包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)
表一旦创建,访问表: Table(DML) 实例。
一次将加入一行内容添加到表。

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

要插入,创建一个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可以被指定为宽泛的 - 获取一个行的所有内容 - 或可以缩小内容范围;
即只返回一个单元值。创建一个Get的实例后,调用 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 - 类似游标的访问。
创建并配置Scan实例后,调用 Table.getScanner(Scan),然后在返回的对象上调用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 删除内容。
你可以删除独个单元格或整个列族.
将 Delete 传递给 Table.delete(Delete) 来执行。

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 并发运行 没有锁 的干扰,并保证不返回一半的数据(返回半行数据就尴尬了)。

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上。
通常这意味着确保客户端可以找到你的hbase-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 {

        // 库里已经有,可以通过hbase shell 创建: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. 接下来,您需要连接到群集。 创建一个。 当它完成后, 关闭它。
        // 2. try / finally是确保它被关闭的一个好方法(或使用jdk7,try-with-resources)
        // 3. Connection是重量级的。创建一个并保持它。
        // 4. 从Connection中,可取Table实例以访问Tables,管理集群的Admin实例以及RegionLocator,以查找集群上的区域。
        // 5. 与Connections相反,Table,Admin和RegionLocator实例是轻量级的; 根据需要创建,然后在完成后关闭。
        Connection connection = ConnectionFactory.createConnection(config);
        try {

            // 1. 下面实例化一个Table对象,它将您连接到“zoo_htable_1”表(TableName.valueOf将String转换为TableName实例)。
            // 2. 当它完成后,关闭它(应该开始一个尝试/终于在这个创建后,所以它被关闭的肯定
            Table table = connection.getTable(TableName.valueOf(tableNameString));
            try {

                // 1. 要添加到行中,请使用Put。 Put构造函数将要插入的行的名称作为字节数组。
                // 在HBase中,Bytes类具有将各种java类型转换为字节数组的实用工具。
                // 在下面,我们将字符串“myLittleRow”转换成一个字节数组作为我们更新的rowkey。
                // 一旦你有了一个Put实例,你可以设置行上更新的column的名称,使用的时间戳等: 如果没有时间戳,服务器将当前时间用于编辑。
                Put p = new Put(Bytes.toBytes("rk_100001"));

                // 在“rk_100001”行设置要更新的值,请指定单元格的列族、列名和值。
                // 该列族必须已经存在。列明可以是任何东西。 所有值必须为字节数组,因为hbase全部是关于字节数组的。
                // 让我们假装表myLittleHBaseTable是用 cf1 系列创建的
                p.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("uname"), Bytes.toBytes("乔布斯"));

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

                // 现在,检索刚刚写入的数据。 返回的值是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"));

                // 将字节值转换,返回插入的实际值
                String valueStr = Bytes.toString(value);
                System.out.println("\t GET: " + valueStr);

                // 有时候,你不知道你要找的那一行. 在本例中,您使用了一个Scanner。为表内容提供类似指针的接口。 设置 Scanner, 就像组装 Put 和 Get一样创建一个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内容
                        System.out.println("Found row: " + result);
                    }

                    // 另一种方法是使用foreach循环。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问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI