Hbase-8.phoenix Introduction

Source: Internet
Author: User
Tags sql client psql table definition port number stringbuffer
1. What is Phoenix? Apache Phoenix is a high-performance relational database running on HBase, and through Phoenix you can access hbase as you would access a relational database using JDBC. Phoenix, the table of operations and the data stored on hbase. Phoenix only needs to be associated with the HBase table. Then use the tool to perform some read and write operations. Phoenix can only be seen as a substitute for the hbase syntax tool. Although Java can use JDBC to connect to Phoenix and then manipulate HBase, it is not possible to use OLTP in a production environment. Phoenix in Query HBase, although some optimizations, but the delay is not small. So it's still used in Olat, where the results are stored back.

Description: Today's data processing can be broadly divided into two broad categories: online transaction processing OLTP (on-line transaction processing), online analytical processing OLAP (On-line Analytical Processing). OLTP is the main application of the traditional relational database, mainly basic, daily transaction processing, such as bank transaction. OLAP is the main application of Data Warehouse system, supports complex analysis operations, focuses on decision support, and provides intuitive and understandable query results.
Which companies are using Phoenix.
2, Phoenix Installation and use the following gives Phoenix's follow-up and use, the official website address please refer to: http://phoenix.apache.org/Phoenix-in-15-minutes-or-less.html
installation(1) Download the corresponding HBase version of Phoenix (phoenix-4.6.0-hbase-0.98-bin.tar.gz) HBase and Phoenix compatibility Phoenix 2.x-hbase 0.94.x Phoenix 3. X-hbase 0.94.x Phoenix 4.x-hbase 0.98.1+
(2) After extracting, copy the Phoenix server jar into the Lib directory of each regionserver TAR-ZXVF phoenix-4.6.0-hbase-0.98-bin.tar.gz
(3) Restart region servers
(4) Add the Phoenix client jar to the classpath of your HBase client
(5) Download and install Squirrel as SQL client and then SQL interaction with HBase cluster
StartIn the bin directory of Phoenix, execute sqlline.py zk_ip:2181
Operation(1) Output Help view assistance and List A common command!help Print command instructions!history Show History command!sql Execute SQL command execute a S QL command!tables Displays all tables in the database
(2) Input!tables Show All tables operation of the table(1) Table creation and query first, create the Us_population.sql file, which mainly contains the table definition:
CREATE TABLE IF not EXISTS us_population (
      the state CHAR (2) is not NULL, the city VARCHAR is not
      null,
      population bigint
  constraint my_pk PRIMARY KEY (state, city));

Then, create the Us_population.csv file that contains the table data
Ny,new york,8143197
ca,los angeles,3844829
il,chicago,2842518
tx,houston,2016582
PA,Philadelphia , 1463281
az,phoenix,1461575
tx,san antonio,1256509
ca,san diego,1255540
tx,dallas,1213825
Ca,san jose,912332

Finally, create a us_population_queries.sql file that is used primarily for querying data for SQL statements
Select state as ' state ', Count (city) as ' City Count ', sum (population) as "population sum" from
us_population
GROUP by state
ORDER by sum (population) DESC;

Executed at the terminal by the following command
./psql.py  192.168.2.20:2181 us_population.sql us_population.csv us_population_queries.sql

Execution Result:
st                             city Cou nt                           population Sum------- ---------------------------------------------------------------------------ny                                        1    &N Bsp                             8143197 ca                             ,         &NB Sp 3                                  60 12701 tx                                &N Bsp     &NBSP 3                                  44 86916 il                                &N Bsp       1                            &N Bsp     2842518 pa                                        1                      &N Bsp           1463281 az                                        1                &N Bsp                 1461575
With the psql.py command we can find and then store the script in the shell file and then execute it by changing the command. Example: Execute a command individually
./psql.py 192.168.2.20:2181 Us_population_queries.sql

(2) inserting data upsert into us_population (state,city,population) VALUES (' NY ', ' DN ', 200); 3, Phoenix's client Phoenix How to use it. We can think of Phoenix as MySQL. Phoenix can operate under the CLI, or it can be accessed using one of Phoenix's client tools, squirrel. Squirrel is a client tool that connects to a database. Databases that generally support JDBC can be used to connect.
(1) Installation of the Phoenix client is performed Java-jar Squirrel-sql-3.7-standard.jar via the command line and then installed via a graphical interface
(2) After the installation is complete, start the interface



(3) You need to click "Drivers" to add the Phoenix driver.


In the order above, fill in in turn. The first step name: Write a name, mark the connection; the second step example URL: equivalent to the MySQL JDBC connection string, here alias write zookeeper host name, port number, can write, can not write, I generally do not write; third step selection Phoenix-4.6.0-hbase-0.98-client.jar jar package; The fourth step is to manually enter Org.apache.phoenix.jdbc.PhoenixDriver.
then click OK.
Note: The Phoenix-4.6.0-hbase-0.98-client.jar driver package can be obtained in the phoenix-4.6.0-hbase-0.98-bin.tar.gz installation package.

(4) Adding aliases
(5) After successful connection, double alias can view the result

(6) Manipulating SQL statements with SQL view


4. Use the Phoenix Java API to manipulate HBase Note: You need to import Phoenix driver corresponding jar package (ie: Phoenix-4.6.0-hbase-0.98-client.jar) (1) Write Java program
try {
    class.forname ("Org.apache.phoenix.jdbc.PhoenixDriver");
    Connection conn = drivermanager.getconnection ("jdbc:phoenix:192.168.2.20:2181");
    StringBuffer sb = new StringBuffer ();

    Sb.append ("Select State,count (city) as Citycount,sum (POPULATION) as populatitionsum from Us_population");
    Sb.append ("GROUP by State");
    Sb.append ("ORDER by SUM (POPULATION) DESC");
    Statement pstm = conn.preparestatement (sb.tostring ());
    ResultSet rs = pstm.executequery (sb.tostring ());
    while (Rs.next ()) {
        String state = rs.getstring ("state");
        int citycount = Rs.getint ("Citycount");
        int populatitionsum = Rs.getint ("Populatitionsum");
        SYSTEM.OUT.PRINTLN (state + "\ T" + Citycount + "\ T" + populatitionsum);}
} catch (Exception e) {
    System.err.println ("Connection Exception:");
    E.printstacktrace ();
}

(2) Operating result NY 2 8143397 CA 3 6012701 TX 3 4486916 IL 1 2842518 PA 1 1463281 AZ 1 1461575

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.