查询外部数据
对文件进行查询
目前,我们支持 Parquet
、CSV
、ORC
和 NDJson
格式文件的查询。
以 Taxi Zone Lookup Table 数据为例。
curl "https://d37ci6vzurychx.cloudfront.net/misc/taxi+_zone_lookup.csv" -o /tmp/taxi+_zone_lookup.csv
创建一个外部表:
CREATE EXTERNAL TABLE taxi_zone_lookup with (location='/tmp/taxi+_zone_lookup.csv',format='csv');
检查外部表的组织和结构:
DESC TABLE taxi_zone_lookup;
+--------------------+----------------------+------+------+--------------------------+---------------+
| Column | Type | Key | Null | Default | Semantic Type |
+--------------------+----------------------+------+------+--------------------------+---------------+
| LocationID | Int64 | | YES | | FIELD |
| Borough | String | | YES | | FIELD |
| Zone | String | | YES | | FIELD |
| service_zone | String | | YES | | FIELD |
| greptime_timestamp | TimestampMillisecond | PRI | NO | 1970-01-01 00:00:00+0000 | TIMESTAMP |
+--------------------+----------------------+------+------+--------------------------+---------------+
4 rows in set (0.00 sec)
注意
在这里,你可能会注意到出现了一个 greptime_timestamp
列,这个列作为表的时间索引列,在文件中并不存在。这是因为在创建外部表时,我们没有指定时间索引列,greptime_timestamp
列被自动添加作为时间索引列,并且默认值为 1970-01-01 00:00:00+0000
。你可以在 create 文档中查找更多详情。
现在就可以查询外部表了:
SELECT `Zone`, `Borough` FROM taxi_zone_lookup LIMIT 5;
+-------------------------+---------------+
| Zone | Borough |
+-------------------------+---------------+
| Newark Airport | EWR |
| Jamaica Bay | Queens |
| Allerton/Pelham Gardens | Bronx |
| Alphabet City | Manhattan |
| Arden Heights | Staten Island |
+-------------------------+---------------+