HBase 6, using the Phoenix Java API to manipulate hbase

Source: Internet
Author: User

Preparation of the development environment:eclipse3.5,jdk1.7,window8,hadoop2.2.0 , hbase0.98.0.2 , phoenix4.3.0

1. Copy the following files from the cluster:core-site.xml,hbase-site.xml,hdfs-site.xml files are put into the project src under

2, the Phoenix-4.3.0-client.jar and phoenix-core-4.3.0.jar of Phoenix Add to Project Classpath

3. Configure the hosts file for each node in the cluster to add the client's hostname:IP

4. Join the hostname and IP of the cluster in the client host file

5. Engineering

   

 Example 1:

Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.Statement;  public class Phoenix_test {/** * uses the APIs provided by Phoenix to manipulate hbase to read data */public static void Main (string[] args) throws Throwable {try {//The drive below is used for the old version of Phoenix using 2.11, corresponding to hbase0.94+//class.forname ("Com.salesforce.phoenix.jdbc.PhoenixDriver");// phoenix4.3 with the following driver corresponding to Hbase0.98+class.forname ("Org.apache.phoenix.jdbc.PhoenixDriver");} catch (Exception e) {e.printstacktrace ();} This configures the address of the zookeeper, which can be single or multiple. Can be a domain name or ipstring url = "Jdbc:phoenix:node5,node6,node7";//String URL =//"jdbc:phoenix:41.byzoro.com,42.byzoro.com, 43.byzoro.com:2181 "; Connection conn = drivermanager.getconnection (URL); Statement Statement = Conn.createstatement (); String sql = "SELECT COUNT (1) as Num from Web_stat"; long time = System.currenttimemillis (); ResultSet rs = statement.executequery (sql), while (Rs.next ()) {int count = rs.getint ("num"); System.out.println ("Row count is" + count);} Long timeused = System.currenttimemillis ()-TimE System.out.println ("Time" + timeused + "mm");//close connection rs.close (); Statement.close (); Conn.close ();}}

Execution Result:

  

  Example 2:

Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.Statement; public class Phoenix_test2 {/** * uses the API operation provided by Phoenix to read data in HBase */public static void Main (string[] args) throws Throwable {T ry {///the drive below is used for the old version of Phoenix 2.11, corresponding to Hbase0.94+//class.forname ("Com.salesforce.phoenix.jdbc.PhoenixDriver");// phoenix4.3 with the following driver corresponding to Hbase0.98+class.forname ("Org.apache.phoenix.jdbc.PhoenixDriver");} catch (Exception e) {e.printstacktrace ();} Here the address of ZK is configured, can be single, or multiple. Can be a domain name or ipstring url = "Jdbc:phoenix:node5,node6,node7"; Connection conn = drivermanager.getconnection (URL); Statement Statement = Conn.createstatement (); String sql = "SELECT * from Web_stat where core = 1"; Long time = System.currenttimemillis (); ResultSet rs = statement.executequery (sql); while (Rs.next ()) {//Get core field value int core = Rs.getint ("core");// Gets the core field value string host = Rs.getstring ("host");//Gets the Domain field value string domain = rs.getstring ("domain");//Gets the feature field value string Feature = rs.getstring ("feature");//Gets the Date field value, in the databasefield is the date type, where the code is automatically converted to a string of type string date = rs.getstring ("date");//Gets the DB fields value string db = Rs.getstring ("db"); System.out.println ("Host:" +host+ "\tdomain:" +domain+ "\tfeature:" +feature+ "\tdate:" +date+ "\tcore:" + core+ "\tdb:" + db);} Long timeused = System.currenttimemillis ()-time; System.out.println ("Time" + timeused + "mm");//close connection rs.close (); Statement.close (); Conn.close ();}}

  Execution Result:

  

HBase 6, using the Phoenix Java API to manipulate hbase

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.