
- Apache Tajo 教程
- Apache Tajo - 首頁
- Apache Tajo - 簡介
- Apache Tajo - 架構
- Apache Tajo - 安裝
- Apache Tajo - 配置設定
- Apache Tajo - Shell 命令
- Apache Tajo - 資料型別
- Apache Tajo - 運算子
- Apache Tajo - SQL 函式
- Apache Tajo - 數學函式
- Apache Tajo - 字串函式
- Apache Tajo - 日期時間函式
- Apache Tajo - JSON 函式
- Apache Tajo - 資料庫建立
- Apache Tajo - 表管理
- Apache Tajo - SQL 語句
- 聚合 & 視窗函式
- Apache Tajo - SQL 查詢
- Apache Tajo - 儲存外掛
- 與 HBase 整合
- Apache Tajo - 與 Hive 整合
- OpenStack Swift 整合
- Apache Tajo - JDBC 介面
- Apache Tajo - 自定義函式
- Apache Tajo 有用資源
- Apache Tajo - 快速指南
- Apache Tajo - 有用資源
- Apache Tajo - 討論
Apache Tajo - SQL 語句
在上一章中,您瞭解瞭如何在 Tajo 中建立表。本章解釋了 Tajo 中的 SQL 語句。
建立表語句
在開始建立表之前,請在 Tajo 安裝目錄路徑下建立一個名為“students.csv”的文字檔案,如下所示:
students.csv
Id | 姓名 | 地址 | 年齡 | 分數 |
---|---|---|---|---|
1 | Adam | 23 New Street | 21 | 90 |
2 | Amit | 12 Old Street | 13 | 95 |
3 | Bob | 10 Cross Street | 12 | 80 |
4 | David | 15 Express Avenue | 12 | 85 |
5 | Esha | 20 Garden Street | 13 | 50 |
6 | Ganga | 25 North Street | 12 | 55 |
7 | Jack | 2 Park Street | 12 | 60 |
8 | Leena | 24 South Street | 12 | 70 |
9 | Mary | 5 West Street | 12 | 75 |
10 | Peter | 16 Park Avenue | 12 | 95 |
建立完檔案後,轉到終端並依次啟動 Tajo 伺服器和 Shell。
建立資料庫
使用以下命令建立一個新資料庫:
查詢
default> create database sampledb; OK
連線到現在已建立的“sampledb”資料庫。
default> \c sampledb You are now connected to database "sampledb" as user “user1”.
然後,在“sampledb”中建立一個表,如下所示:
查詢
sampledb> create external table mytable(id int,name text,address text,age int,mark int) using text with('text.delimiter' = ',') location ‘file:/Users/workspace/Tajo/students.csv’;
結果
以上查詢將生成以下結果。
OK
這裡建立了外部表。現在,您只需輸入檔案位置即可。如果您需要從 hdfs 分配表,則使用 hdfs 代替 file。
接下來,“students.csv”檔案包含逗號分隔的值。“text.delimiter”欄位賦值為‘,’。
您現在已在“sampledb”中成功建立了“mytable”。
顯示錶
要顯示 Tajo 中的表,請使用以下查詢。
查詢
sampledb> \d mytable sampledb> \d mytable
結果
以上查詢將生成以下結果。
table name: sampledb.mytable table uri: file:/Users/workspace/Tajo/students.csv store type: TEXT number of rows: unknown volume: 261 B Options: 'timezone' = 'Asia/Kolkata' 'text.null' = '\\N' 'text.delimiter' = ',' schema: id INT4 name TEXT address TEXT age INT4 mark INT4
列出表
要獲取表中的所有記錄,請鍵入以下查詢:
查詢
sampledb> select * from mytable;
結果
以上查詢將生成以下結果。

插入表語句
Tajo 使用以下語法在表中插入記錄。
語法
create table table1 (col1 int8, col2 text, col3 text); --schema should be same for target table schema Insert overwrite into table1 select * from table2; (or) Insert overwrite into LOCATION '/dir/subdir' select * from table;
Tajo 的插入語句類似於 SQL 的**INSERT INTO SELECT**語句。
查詢
讓我們建立一個表來覆蓋現有表的表資料。
sampledb> create table test(sno int,name text,addr text,age int,mark int); OK sampledb> \d
結果
以上查詢將生成以下結果。
mytable test
插入記錄
要在“test”表中插入記錄,請鍵入以下查詢。
查詢
sampledb> insert overwrite into test select * from mytable;
結果
以上查詢將生成以下結果。
Progress: 100%, response time: 0.518 sec
這裡,“mytable”記錄覆蓋了“test”表。如果您不想建立“test”表,則可以直接分配物理路徑位置,如插入查詢的替代選項中所述。
獲取記錄
使用以下查詢列出“test”表中的所有記錄:
查詢
sampledb> select * from test;
結果
以上查詢將生成以下結果。

此語句用於新增、刪除或修改現有表的列。
要重命名錶,請使用以下語法:
Alter table table1 RENAME TO table2;
查詢
sampledb> alter table test rename to students;
結果
以上查詢將生成以下結果。
OK
要檢查已更改的表名,請使用以下查詢。
sampledb> \d mytable students
現在表“test”已更改為“students”表。
新增列
要在“students”表中插入新列,請鍵入以下語法:
Alter table <table_name> ADD COLUMN <column_name> <data_type>
查詢
sampledb> alter table students add column grade text;
結果
以上查詢將生成以下結果。
OK
設定屬性
此屬性用於更改表的屬性。
查詢
sampledb> ALTER TABLE students SET PROPERTY 'compression.type' = 'RECORD', 'compression.codec' = 'org.apache.hadoop.io.compress.Snappy Codec' ; OK
這裡,分配了壓縮型別和編解碼器屬性。
要更改文字分隔符屬性,請使用以下方法:
查詢
ALTER TABLE students SET PROPERTY ‘text.delimiter'=','; OK
結果
以上查詢將生成以下結果。
sampledb> \d students table name: sampledb.students table uri: file:/tmp/tajo-user1/warehouse/sampledb/students store type: TEXT number of rows: 10 volume: 228 B Options: 'compression.type' = 'RECORD' 'timezone' = 'Asia/Kolkata' 'text.null' = '\\N' 'compression.codec' = 'org.apache.hadoop.io.compress.SnappyCodec' 'text.delimiter' = ',' schema: id INT4 name TEXT addr TEXT age INT4 mark INT4 grade TEXT
以上結果顯示,使用“SET”屬性更改了表的屬性。
選擇語句
SELECT 語句用於從資料庫中選擇資料。
Select 語句的語法如下:
SELECT [distinct [all]] * | <expression> [[AS] <alias>] [, ...] [FROM <table reference> [[AS] <table alias name>] [, ...]] [WHERE <condition>] [GROUP BY <expression> [, ...]] [HAVING <condition>] [ORDER BY <expression> [ASC|DESC] [NULLS (FIRST|LAST)] [, …]]
Where 子句
Where 子句用於從表中過濾記錄。
查詢
sampledb> select * from mytable where id > 5;
結果
以上查詢將生成以下結果。

該查詢返回 id 大於 5 的學生的記錄。
查詢
sampledb> select * from mytable where name = ‘Peter’;
結果
以上查詢將生成以下結果。
Progress: 100%, response time: 0.117 sec id, name, address, age ------------------------------- 10, Peter, 16 park avenue , 12
結果僅過濾 Peter 的記錄。
Distinct 子句
表列可能包含重複值。DISTINCT 關鍵字可用於僅返回不同的值。
語法
SELECT DISTINCT column1,column2 FROM table_name;
查詢
sampledb> select distinct age from mytable;
結果
以上查詢將生成以下結果。
Progress: 100%, response time: 0.216 sec age ------------------------------- 13 12
該查詢返回**mytable**中學生的不同年齡。
Group By 子句
GROUP BY 子句與 SELECT 語句一起使用,將相同的資料排列成組。
語法
SELECT column1, column2 FROM table_name WHERE [ conditions ] GROUP BY column1, column2;
查詢
select age,sum(mark) as sumofmarks from mytable group by age;
結果
以上查詢將生成以下結果。
age, sumofmarks ------------------------------- 13, 145 12, 610
這裡,“mytable”列有兩種年齡型別——12 和 13。現在,查詢按年齡對記錄進行分組,並生成對應學生年齡的分數總和。
Having 子句
HAVING 子句使您能夠指定條件,以過濾最終結果中顯示的組結果。WHERE 子句對選定的列施加條件,而 HAVING 子句對 GROUP BY 子句建立的組施加條件。
語法
SELECT column1, column2 FROM table1 GROUP BY column HAVING [ conditions ]
查詢
sampledb> select age from mytable group by age having sum(mark) > 200;
結果
以上查詢將生成以下結果。
age ------------------------------- 12
該查詢按年齡對記錄進行分組,並在條件結果 sum(mark) > 200 時返回年齡。
Order By 子句
ORDER BY 子句用於根據一列或多列對資料進行升序或降序排序。預設情況下,Tajo 資料庫以升序對查詢結果進行排序。
語法
SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2, .. columnN] [ASC | DESC];
查詢
sampledb> select * from mytable where mark > 60 order by name desc;
結果
以上查詢將生成以下結果。

該查詢按分數大於 60 的學生的姓名降序返回。
建立索引語句
CREATE INDEX 語句用於在表中建立索引。索引用於快速檢索資料。當前版本僅支援儲存在 HDFS 上的純文字格式的索引。
語法
CREATE INDEX [ name ] ON table_name ( { column_name | ( expression ) }
查詢
create index student_index on mytable(id);
結果
以上查詢將生成以下結果。
id ———————————————
要檢視分配給列的索引,請鍵入以下查詢。
default> \d mytable table name: default.mytable table uri: file:/Users/deiva/workspace/Tajo/students.csv store type: TEXT number of rows: unknown volume: 307 B Options: 'timezone' = 'Asia/Kolkata' 'text.null' = '\\N' 'text.delimiter' = ',' schema: id INT4 name TEXT address TEXT age INT4 mark INT4 Indexes: "student_index" TWO_LEVEL_BIN_TREE (id ASC NULLS LAST )
這裡,Tajo 預設使用 TWO_LEVEL_BIN_TREE 方法。
刪除表語句
Drop Table 語句用於從資料庫中刪除表。
語法
drop table table name;
查詢
sampledb> drop table mytable;
要檢查表是否已從表中刪除,請鍵入以下查詢。
sampledb> \d mytable;
結果
以上查詢將生成以下結果。
ERROR: relation 'mytable' does not exist
您還可以使用“\d”命令檢查查詢,以列出可用的 Tajo 表。