来自java的hbase扫描api

nimxete2  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(361)

我正在用基于java的hbase客户端编写一些非常基本的东西,用于对已启用的现有表执行扫描操作。该计划基于:https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/package-summary.html

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;
import org.apache.hadoop.hbase.protobuf.generated.*;

public class FirstHBaseClient {
  public static void main(String[] args) throws IOException {

    Configuration config = HBaseConfiguration.create();

    Connection connection = ConnectionFactory.createConnection(config);
    try {

      Table table = connection.getTable(TableName.valueOf("test"));
      try {

        Scan s = new Scan();
        ResultScanner scanner = table.getScanner(s);
        try {

           for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
             // print out the row we found and the columns we were looking for
             System.out.println("Found row: " + rr);
           }

         } finally {

           scanner.close();
         }

       } finally {
         if (table != null) table.close();
       }
     } finally {
       connection.close();
     }
  }
}

编译和执行很好…会话正在建立。但是我没有从扫描中得到任何结果,为什么?eclipse控制台输出:

15/09/17 19:37:18 INFO zookeeper.ZooKeeper: Client environment:user.dir=/root/workspace_hbase/HBaseIntro
15/09/17 19:37:18 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=90000 watcher=hconnection-0xea4a92b0x0, quorum=localhost:2181, baseZNode=/hbase
15/09/17 19:37:18 INFO zookeeper.ClientCnxn: Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
15/09/17 19:37:18 INFO zookeeper.ClientCnxn: Socket connection established to localhost/127.0.0.1:2181, initiating session
15/09/17 19:37:18 INFO zookeeper.ClientCnxn: Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x14fde0f7576000e, negotiated timeout = 40000

我做错了什么?我在ubuntulinux上使用hbase-1.1.2版本,运行jdk1.8.x。

sycxhyv7

sycxhyv71#

我使用了ApachePhoenix api,最终能够超越到hbase的连接,从java客户端执行到hbase的所有crud操作。

import java.sql.*;
import java.util.*;

public class phoenixTest
{
    public static void main(String args[]) throws Exception
    {
        Connection conn;
        Properties prop = new Properties();
        Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
        System.out.println("Driver class loaded successfully");

        conn = DriverManager.getConnection("jdbc:phoenix:localhost");
        System.out.println("got connection");

        //WEB_STAT
        ResultSet rst = conn.createStatement().executeQuery("select * from WEB_STAT");
        while (rst.next())
        {
            System.out.println(rst.getString(1) + " " + rst.getString(2));
        }

    }
}

并遵循以下步骤:将所有服务器jar复制到hbase lib中
然后从:nix-4.5.2-hbase-0.98-bin/bin执行
./psql.py localhost/home/cloudera/phoenix-4.5.2-hbase-0.98-src/examples/web\u stat.sql/home/cloudera/phoenix-4.5.2-hbase-0.98-src/examples/web\u stat.csv/home/cloudera/phoenix-4.5.2-hbase-0.98-src/examples/web\u stat\u querys.sql
然后在hbase shell上。。。扫描“web\u stat”
验证是否已创建表?
也要从apachephoenix shell检查它
然后把/home/cloudera/phoenix-4.5.2-hbase-0.98-bin/phoenix-4.5.2-hbase-0.98-client.jar
在您的eclipse项目中,使用以下源代码:而且一切正常apachephoenix非常容易开始使用apahce hbase。

nnvyjq4y

nnvyjq4y2#

pheonix是完全不同的数据检索方法。。。我希望在那个时候的测试,测试数据是可用的!!!下面的代码应该可以工作。

for (Result result = scanner.next(); (result != null); result = scanner.next()) {
    for(KeyValue keyValue : result.list()) {
        System.out.println("Qualifier : " + keyValue.getKeyString() + " : Value : " + Bytes.toString(keyValue.getValue()));
    }
}

相关问题