简体   繁体   中英

HBase scan api from Java

I am writing some very basic stuff in Java based Hbase client for doing scan operation on existing table which is enabled. The program is based on: 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();
     }
  }
}

Compilation and execution is fine...session is getting established. But I am not getting any results from the scan operation, why? Eclipse console output:

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

What I am doing wrong? I am using Hbase - 1.1.2 version on Ubuntu Linux and running JDK1.8.x.

Pheonix is altogether different approach of data retrieval... I hope at that time of testing, test data is available!!! This below code should work.

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()));
    }
}

I used the Apache Phoenix API and finally able to go beyond the connectivity to HBase and perform all the CRUD operations to HBase from Java Client.

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));
        }

    }
}

And followed these steps: copy all the server JARS into Hbase lib

then from: nix-4.5.2-HBase-0.98-bin/bin execute

./psql.py localhost /home/cloudera/phoenix-4.5.2-HBase-0.98-src/examples/WEB_STAT.sql /home/cloudera/phoenix-4.5.2-HBase-0.98-src/examples/WEB_STAT.csv /home/cloudera/phoenix-4.5.2-HBase-0.98-src/examples/WEB_STAT_QUERIES.sql

then on Hbase shell... scan 'WEB_STAT'

to verify that the table is created or not?

also do check it from apache phoenix shell

then just put the /home/cloudera/phoenix-4.5.2-HBase-0.98-bin/phoenix-4.5.2-HBase-0.98-client.jar

into your Eclipse project and make use of this source code: and it's working all fine Apache Phoenix is very easy to get started and get working with Apahce HBase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM