HBase 从 Java 扫描 api
HBase scan api from Java
我正在基于 Java 的 Hbase 客户端中编写一些非常基本的东西,以便对已启用的现有 table 执行扫描操作。该计划基于:
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
我做错了什么?
我在 Ubuntu Linux 和 运行 JDK1.8.x.
上使用 Hbase - 1.1.2 版本
我使用了 Apache Phoenix 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));
}
}
}
并遵循以下步骤:
将所有服务器 JARS 复制到 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_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
然后在 Hbase 上 shell...
扫描 'WEB_STAT'
验证是否创建了 table?
也可以从 apache phoenix 上查看 shell
然后把 /home/cloudera/phoenix-4.5.2-HBase-0.98-bin/phoenix-4.5.2-HBase-0.98-client.jar
进入您的 Eclipse 项目并使用此源代码:
一切正常 Apache Phoenix 非常容易上手并开始使用 Apahce HBase。
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()));
}
}
我正在基于 Java 的 Hbase 客户端中编写一些非常基本的东西,以便对已启用的现有 table 执行扫描操作。该计划基于: 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
我做错了什么? 我在 Ubuntu Linux 和 运行 JDK1.8.x.
上使用 Hbase - 1.1.2 版本我使用了 Apache Phoenix 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));
}
}
}
并遵循以下步骤: 将所有服务器 JARS 复制到 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_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
然后在 Hbase 上 shell... 扫描 'WEB_STAT'
验证是否创建了 table?
也可以从 apache phoenix 上查看 shell
然后把 /home/cloudera/phoenix-4.5.2-HBase-0.98-bin/phoenix-4.5.2-HBase-0.98-client.jar
进入您的 Eclipse 项目并使用此源代码: 一切正常 Apache Phoenix 非常容易上手并开始使用 Apahce HBase。
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()));
}
}