1. 程式人生 > >Phoenix(2):Phoenix常用操作

Phoenix(2):Phoenix常用操作

一、基本原則

1.phoenix中的表名和欄位名不加雙引號,都為大寫,加上雙引號,則區分大寫;
2.phoenix和RDBMS中一樣,有資料型別
3.插入資料時,values中數值型別不用引號,字串只能是單引號

二、常用語句

1.建立表

create table us_population(
state varchar(2),
city varchar(15),
population bigint,
constraint pk primary key(state,city)
);

2.插入/更新資料

upsert into us_population(state,city,population) values('NY','New York',8143197);
upsert into us_population(state,city,population) values('NY','New York',8143197);  
upsert into us_population(state,city,population) values('CA','Los Angeles',3844829); 
upsert into us_population(state,city,population) values('IL','Chicago',2842518);
upsert into us_population(state,city,population) values('TX','Houston',2016582);
upsert into us_population(state,city,population) values('PA','Philadelphia',1463281);  
upsert into us_population(state,city,population) values('AZ','Phoenix',1461575);
upsert into us_population(state,city,population) values('TX','San Antonio',1256509);  
upsert into us_population(state,city,population) values('CA','San Diego',1255540);
upsert into us_population(state,city,population) values('TX','Dallas',1213825);
upsert into us_population(state,city,population) values('CA','San Jose',912332);

其中:字串是單引號

3.查詢語句

select * from us_population;

+-------+-----------------+------------------------------------------+
| STATE |      CITY       |                POPULATION                |
+-------+-----------------+------------------------------------------+
| AZ    | Phoenix         | 1461575                                  |
| CA    | Los Angeles     | 3844829                                  |
| CA    | San Diego       | 1255540                                  |
| CA    | San Jose        | 912332                                   |
| IL      | Chicago         | 2842518                                  |
| NY    | New York        | 8143197                                  |
| PA    | Philadelphia    | 1463281                                  |
| TX    | Dallas          | 1213825                                  |
| TX    | Houston         | 2016582                                  |
| TX    | San Antonio     | 1256509                                  |
+-------+-----------------+------------------------------------------+

4.修改資料

upsert into us_population(state,city,population) values('AZ','Phoenix',1500000);

結果:

0: jdbc:phoenix:hadoop:2181> select * from us_population;
+--------+---------------+-------------+
| STATE  |     CITY      | POPULATION  |
+--------+---------------+-------------+
| AZ     | Phoenix       | 1500000     |
| CA     | Los Angeles   | 3844829     |
| CA     | San Diego     | 1255540     |
| CA     | San Jose      | 912332      |
| IL     | Chicago       | 2842518     |
| NY     | New York      | 8143197     |
| PA     | Philadelphia  | 1463281     |
| TX     | Dallas        | 1213825     |
| TX     | Houston       | 2016582     |
| TX     | San Antonio   | 1256509     |
+--------+---------------+-------------+

5.分組聚合查詢

0: jdbc:phoenix:hadoop:2181> select state as "State",count(city) "City",sum(population) "Population" from us_population group by state order by sum(population) desc;
+--------+-------+-------------+
| State  | City  | Population  |
+--------+-------+-------------+
| NY     | 1     | 8143197     |
| CA     | 3     | 6012701     |
| TX     | 3     | 4486916     |
| IL     | 1     | 2842518     |
| AZ     | 1     | 1500000     |
| PA     | 1     | 1463281     |
+--------+-------+-------------+
6 rows selected (0.034 seconds)

三、小寫表以及新增列簇

1.建立能夠正常在HBase中帶有列簇,且都為表名和列簇都為小寫的表

create table "us_population"(
"state" varchar(2),
"city" varchar(15),
"info"."population" bigint,
constraint pk primary key("state","city")
);

備註:

   (1) state和city是一起用了做聯合主鍵,就是hbase的行鍵!所以不需要列簇
    (2)指明列簇info

2.插入資料

upsert into "us_population"("state","city","population") values('NY','New York',8143197);  
upsert into "us_population"("state","city","population") values('CA','Los Angeles',3844829); 
upsert into "us_population"("state","city","population") values('IL','Chicago',2842518);
upsert into "us_population"("state","city","population") values('TX','Houston',2016582);
upsert into "us_population"("state","city","info"."population") values('PA','Philadelphia',1463281);  
upsert into "us_population"("state","city","info"."population") values('AZ','Phoenix',1461575);
upsert into "us_population"("state","city","info"."population") values('TX','San Antonio',1256509);  
upsert into "us_population"("state","city","info"."population") values('CA','San Diego',1255540);
upsert into "us_population"("state","city","info"."population") values('TX','Dallas',1213825);
upsert into "us_population"("state","city","info"."population") values('CA','San Jose',912332);

注意:

    (1)此時插入資料需要注意在表名和列名對應地方加雙引號
    (2)插入資料加不所在列簇都是沒有關係的

3.刪除資料

delete from "us_population" where "state" = 'IL Henan';