HBase 5、Phoenix使用


1、建表

執行建表語句
$ ./psql.py localhost:2181 ../examples/stock_symbol.sql

其中../examples/stock_symbol.sql是建表的sql語句

CREATE TABLE IF NOT EXISTS WEB_STAT (
  HOST CHAR(2) NOT NULL,
  DOMAIN VARCHAR NOT NULL,
  FEATURE VARCHAR NOT NULL,
  DATE DATE NOT NULL,
  USAGE.CORE BIGINT,--usage指定列族名
  USAGE.DB BIGINT,--usage指定列族名
  STATS.ACTIVE_VISITOR INTEGER
  CONSTRAINT PK PRIMARY KEY (HOST, DOMAIN, FEATURE, DATE)--指定主鍵
);

2、導入數據

命令:./psql.py -t WEB_STAT localhost:2181 ../examples/web_stat.csv

PS:其中 -t 后面是表名, ../examples/web_stat.csv 是csv數據(注意數據的分隔符需要是逗號)。

3、查詢數據

  首先使用sqlline查看(截圖為部分列的數據),查詢表名不區分大小寫。

  查詢1、查詢全部記錄

    語句:select * from web_stat;

  查詢2、查詢記錄總條數

    語句:select count(1) from web_stat;

  查詢3、查詢結果分組排序

    語句:select domain,count(1) as num from web_stat group by domain order by num desc;

  查詢4、求平均值

    語句:select avg(core) from web_stat;

  查詢5、多字段分組,排序,別名。

    語句:select domain,count(1) as num,avg(core) as core,avg(db) as db from web_stat group by domain order by num desc;

  查詢6、查詢日期類型字段

    語句:select host,domain,date from web_stat where TO_CHAR(date)='2013-01-15 07:09:01.000';

  查詢7、字符串,日期類型轉換

    語句:select TO_DATE('20131125','yyyyMMdd') from web_stat;

    Ps:輸入的日期字符串會被轉換為hbasedate的日期類型。

  總結:Phoenix還支持了很多函數和sql語法,在這里不再一一列舉。更多請參考Phoenix支持部分

4Phoenix基本shell命令

  PS:以下,可能有部分命令在Phoenix更高版本中已失效,改為其他命令代替,請注意。 

0: jdbc:phoenix:localhost> help
!all                Execute the specified SQL against all the current connections
!autocommit         Set autocommit mode on or off
!batch              Start or execute a batch of statements
!brief              Set verbose mode off
!call               Execute a callable statement
!close              Close the current connection to the database
!closeall           Close all current open connections
!columns            List all the columns for the specified table
!commit             Commit the current transaction (if autocommit is off)
!connect            Open a new connection to the database.
!dbinfo             Give metadata information about the database
!describe           Describe a table
!dropall            Drop all tables in the current database
!exportedkeys       List all the exported keys for the specified table
!go                 Select the current connection
!help               Print a summary of command usage
!history            Display the command history
!importedkeys       List all the imported keys for the specified table
!indexes            List all the indexes for the specified table
!isolation          Set the transaction isolation for this connection
!list               List the current connections
!manual             Display the SQLLine manual
!metadata           Obtain metadata information
!nativesql          Show the native SQL for the specified statement
!outputformat       Set the output format for displaying results
                    (table,vertical,csv,tsv,xmlattrs,xmlelements)
!primarykeys        List all the primary keys for the specified table
!procedures         List all the procedures
!properties         Connect to the database specified in the properties file(s)
!quit               Exits the program  此命令在Phoenix4.3版本已改為!exit
!reconnect          Reconnect to the database
!record             Record all output to the specified file
!rehash             Fetch table and column names for command completion
!rollback           Roll back the current transaction (if autocommit is off)
!run                Run a script from the specified file
!save               Save the current variabes and aliases
!scan               Scan for installed JDBC drivers
!script             Start saving a script to a file
!set                Set a sqlline variable

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM