Amazon Redshift RSQL 中繼命令 - Amazon Redshift

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Amazon Redshift RSQL 中繼命令

Amazon Redshift RSQL 中繼命令會傳回有關資料庫或特定資料庫物件的資訊記錄。結果中可以包含各種資料行和中繼資料。其他命令會執行特定動作。這些命令的前面會加上反斜線。

\d[S+]

列出本機使用者建立的資料表、一般視觀表、晚期繫結視觀表及具體化視觀表。\dS 也會列出資料表和視觀表 (例如 \d),但系統物件包含在傳回的記錄中。+ 會導致所有列出的物件產生額外的中繼資料資料行 description。以下示範執行命令所傳回的範例記錄。

List of relations schema | name | type | owner --------+-----------+-------+--------- public | category | table | awsuser public | date | table | awsuser public | event | table | awsuser public | listing | table | awsuser public | sales | table | awsuser public | users | table | awsuser public | venue | table | awsuser (7 rows)

\d[S+] NAME

描述資料表、視觀表或索引。包括資料行名稱和類型。其還會提供 diststyle、備份組態、建立日期 (2018 年 10 月之後建立的資料表) 和限制。例如,\dS+ sample 會傳回物件屬性。附加 S+ 會導致所傳回的記錄中包含額外資料行。

Table "public.sample" Column | Type | Collation | Nullable | Default Value | Encoding | DistKey | SortKey --------+-----------------------------+----------------+----------+---------------+-----------+---------+--------- col1 | smallint | | NO | | none | t | 1 col2 | character(100) | case_sensitive | YES | | none | f | 2 col3 | character varying(100) | case_sensitive | YES | | text32k | f | 3 col4 | timestamp without time zone | | YES | | runlength | f | 0 col5 | super | | YES | | zstd | f | 0 col6 | bigint | | YES | | az64 | f | 0 Diststyle: KEY Backup: YES Created: 2021-07-20 19:47:27.997045 Unique Constraints: "sample_pkey" PRIMARY KEY (col1) "sample_col2_key" UNIQUE (col2) Foreign-key constraints: "sample_col2_fkey" FOREIGN KEY (col2) REFERENCES lineitem(l_orderkey)

資料表的分佈樣式 (簡稱 Diststyle) 可以是 KEY、AUTO、EVEN 或 ALL。

Backup 會指出在擷取快照時是否有備份資料表。有效值為 YESNO

Created 是建立資料表時的時間戳記。建立日期不適用於 2018 年 11 月之前建立的 Amazon Redshift 資料表。在此日期之前建立的資料表會顯示 n/a (不適用)。

Unique Constraints 會列出資料表上唯一的和主索引鍵的限制。

Foreign-key constraints 會列出資料表上的外部索引鍵限制。

\dC[+] [PATTERN]

列出轉換。包括來源類型、目標類型,以及轉換是否為隱含。

以下示範 \dC+ 的結果子集。

List of casts source type | target type | function | implicit? | description -----------------------------+-----------------------------+---------------------+---------------+------------- "char" | character | bpchar | in assignment | "char" | character varying | text | in assignment | "char" | integer | int4 | no | "char" | text | text | yes | "path" | point | point | no | "path" | polygon | polygon | in assignment | abstime | date | date | in assignment | abstime | integer | (binary coercible) | no | abstime | time without time zone | time | in assignment | abstime | timestamp with time zone | timestamptz | yes | abstime | timestamp without time zone | timestamp | yes | bigint | bit | bit | no | bigint | boolean | bool | yes | bigint | character | bpchar | in assignment | bigint | character varying | text | in assignment | bigint | double precision | float8 | yes | bigint | integer | int4 | in assignment | bigint | numeric | numeric | yes | bigint | oid | oid | yes | bigint | real | float4 | yes | bigint | regclass | oid | yes | bigint | regoper | oid | yes | bigint | regoperator | oid | yes | bigint | regproc | oid | yes | bigint | regprocedure | oid | yes | bigint | regtype | oid | yes | bigint | smallint | int2 | in assignment | bigint | super | int8_partiql | in assignment |

\dd[S] [PATTERN]

顯示其他地方未顯示的物件描述。

\de

列出外部資料表。這包括 AWS Glue 資料目錄中的資料表、蜂巢中繼存放區和來自 Amazon RDS/Aurora MySQL、Amazon RDSL/ Aurora PostgreSQL 和 Amazon Redshift 資料清理表的聯合資料表。

\de NAME

描述外部資料表。

下面的例子顯示了一個 AWS Glue 外部表。

# \de spectrum.lineitem Glue External table "spectrum.lineitem" Column | External Type | Redshift Type | Position | Partition Key | Nullable -----------------+---------------+---------------+----------+---------------+---------- l_orderkey | bigint | bigint | 1 | 0 | l_partkey | bigint | bigint | 2 | 0 | l_suppkey | int | int | 3 | 0 | l_linenumber | int | int | 4 | 0 | l_quantity | decimal(12,2) | decimal(12,2) | 5 | 0 | l_extendedprice | decimal(12,2) | decimal(12,2) | 6 | 0 | l_discount | decimal(12,2) | decimal(12,2) | 7 | 0 | l_tax | decimal(12,2) | decimal(12,2) | 8 | 0 | l_returnflag | char(1) | char(1) | 9 | 0 | l_linestatus | char(1) | char(1) | 10 | 0 | l_shipdate | date | date | 11 | 0 | l_commitdate | date | date | 12 | 0 | l_receiptdate | date | date | 13 | 0 | l_shipinstruct | char(25) | char(25) | 14 | 0 | l_shipmode | char(10) | char(10) | 15 | 0 | l_comment | varchar(44) | varchar(44) | 16 | 0 | Location: s3://redshiftbucket/kfhose2019/12/31 Input_format: org.apache.hadoop.mapred.TextInputFormat Output_format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat Serialization_lib: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe Serde_parameters: {"field.delim":"|","serialization.format":"|"} Parameters: {"EXTERNAL":"TRUE","numRows":"178196721475","transient_lastDdlTime":"1577771873"}

Hive Metastore 資料表。

# \de emr.lineitem Hive Metastore External Table "emr.lineitem" Column | External Type | Redshift Type | Position | Partition Key | Nullable -----------------+---------------+---------------+----------+---------------+---------- l_orderkey | bigint | bigint | 1 | 0 | l_partkey | bigint | bigint | 2 | 0 | l_suppkey | int | int | 3 | 0 | l_linenumber | int | int | 4 | 0 | l_quantity | decimal(12,2) | decimal(12,2) | 5 | 0 | l_extendedprice | decimal(12,2) | decimal(12,2) | 6 | 0 | l_discount | decimal(12,2) | decimal(12,2) | 7 | 0 | l_tax | decimal(12,2) | decimal(12,2) | 8 | 0 | l_returnflag | char(1) | char(1) | 9 | 0 | l_linestatus | char(1) | char(1) | 10 | 0 | l_commitdate | date | date | 11 | 0 | l_receiptdate | date | date | 12 | 0 | l_shipinstruct | char(25) | char(25) | 13 | 0 | l_shipmode | char(10) | char(10) | 14 | 0 | l_comment | varchar(44) | varchar(44) | 15 | 0 | l_shipdate | date | date | 16 | 1 | Location: s3://redshiftbucket/cetas Input_format: org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat Output_format: org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat Serialization_lib: org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe Serde_parameters: {"serialization.format":"1"} Parameters: {"EXTERNAL":"TRUE", "numRows":"4307207", "transient_lastDdlTime":"1626990007"}

PostgreSQL 外部資料表。

# \de pgrsql.alltypes Postgres Federated Table "pgrsql.alltypes" Column | External Type | Redshift Type | Position | Partition Key | Nullable --------+-----------------------------+-----------------------------+----------+---------------+---------- col1 | bigint | bigint | 1 | 0 | col2 | bigint | bigint | 2 | 0 | col5 | boolean | boolean | 3 | 0 | col6 | box | varchar(65535) | 4 | 0 | col7 | bytea | varchar(65535) | 5 | 0 | col8 | character(10) | character(10) | 6 | 0 | col9 | character varying(10) | character varying(10) | 7 | 0 | col10 | cidr | varchar(65535) | 8 | 0 | col11 | circle | varchar(65535) | 9 | 0 | col12 | date | date | 10 | 0 | col13 | double precision | double precision | 11 | 0 | col14 | inet | varchar(65535) | 12 | 0 | col15 | integer | integer | 13 | 0 | col16 | interval | varchar(65535) | 14 | 0 | col17 | json | varchar(65535) | 15 | 0 | col18 | jsonb | varchar(65535) | 16 | 0 | col19 | line | varchar(65535) | 17 | 0 | col20 | lseg | varchar(65535) | 18 | 0 | col21 | macaddr | varchar(65535) | 19 | 0 | col22 | macaddr8 | varchar(65535) | 20 | 0 | col23 | money | varchar(65535) | 21 | 0 | col24 | numeric | numeric(38,20) | 22 | 0 | col25 | path | varchar(65535) | 23 | 0 | col26 | pg_lsn | varchar(65535) | 24 | 0 | col28 | point | varchar(65535) | 25 | 0 | col29 | polygon | varchar(65535) | 26 | 0 | col30 | real | real | 27 | 0 | col31 | smallint | smallint | 28 | 0 | col32 | smallint | smallint | 29 | 0 | col33 | integer | integer | 30 | 0 | col34 | text | varchar(65535) | 31 | 0 | col35 | time without time zone | varchar(65535) | 32 | 0 | col36 | time with time zone | varchar(65535) | 33 | 0 | col37 | timestamp without time zone | timestamp without time zone | 34 | 0 | col38 | timestamp with time zone | timestamp with time zone | 35 | 0 | col39 | tsquery | varchar(65535) | 36 | 0 | col40 | tsvector | varchar(65535) | 37 | 0 | col41 | txid_snapshot | varchar(65535) | 38 | 0 | col42 | uuid | varchar(65535) | 39 | 0 | col43 | xml | varchar(65535) | 40 | 0 |

\df[anptw][S+] [PATTERN]

列出各種類型的函數。例如,命令 \df 會傳回函數清單。結果包括名稱、傳回的資料類型、存取權限和其他中繼資料等屬性。函數類型可以包括觸發條件、預存程序、視窗函數和其他類型。當您附加 S+ 至命令時 (例如 \dfantS+),其中會包括其他中繼資料資料行,例如 ownersecurityaccess privileges

\dL[S+] [PATTERN]

列出與資料庫相關聯之程序語言的相關資料。資訊中會包括名稱 (例如 plpgsql) 和其他中繼資料,其中包括其是否受信任、存取權限和描述。例如,範例呼叫是會列出語言及其屬性的 \dLS+。當您附加 S+ 至命令時,其中會包括其他中繼資料資料行,例如 call handleraccess privileges

範例結果:

List of languages name | trusted | internal language | call handler | validator | access privileges | description -----------+---------+-------------------+-------------------------+------------------------------------------------------------+-------------------+-------------------------------- c | f | t | - | fmgr_c_validator(oid) | | Dynamically-loaded C functions exfunc | f | f | exfunc_call_handler() | - | rdsdb=U/rdsdb | internal | f | t | - | fmgr_internal_validator(oid) | | Built-in functions mlfunc | f | f | mlfunc_call_handler() | - | rdsdb=U/rdsdb | plpgsql | t | f | plpgsql_call_handler() | plpgsql_validator(oid) | | plpythonu | f | f | plpython_call_handler() | plpython_compiler(cstring,cstring,cstring,cstring,cstring) | rdsdb=U/rdsdb | sql | t | t | - | fmgr_sql_validator(oid) | =U/rdsdb | SQL-language functions

\dm[S+] [PATTERN]

列出具體化視觀表。例如,\dmS+ 會列出具體化視觀表及其屬性。當您附加 S+ 至命令時,其中會包括其他中繼資料資料行。

\dn[S+] [PATTERN]

列出結構描述。當您附加 S+ 至命令時 (例如 \dnS+),其中會包括其他中繼資料資料行,例如 descriptionaccess privileges

\dp [PATTERN]

列出資料表、視觀表和序列存取權限。

\dt[S+] [PATTERN]

列出資料表。當您附加 S+ 至命令時 (例如 \dtS+),其中會包括其他中繼資料資料行,例如本案例中的 description

\du

列出資料庫的使用者。包括其名稱及其角色 (例如超級使用者) 和屬性。

\dv[S+] [PATTERN]

列出視觀表。包括結構描述、類型和擁有者資料。當您附加 S+ 至命令時 (例如 \dvS+),其中會包括其他中繼資料資料行。

\H

開啟 HTML 輸出。這對於快速傳回格式化結果非常有用。例如,select * from sales; \H 會以 HTML 格式傳回銷售資料表的結果。若要切換回表格式結果,請使用 \q 或靜音。

\i

從檔案執行命令。例如,假設工作目錄中有 rsql_steps.sql,以下內容會執行檔案中的命令:\i rsql_steps.sql

\l[+] [PATTERN]

列出資料庫。包括擁有者、編碼和其他資訊。

\q

結束或 \q 命令會登出資料庫工作階段並結束 RSQL。

\sv[+] VIEWNAME

顯示視觀表的定義。

\timing

例如,顯示查詢的執行時間。

\z [PATTERN]

與 \dp 相同的輸出。

\?

顯示說明資訊。選用參數會指定要解釋的項目。

\EXIT

登出所有資料庫工作階段並結束 Amazon Redshift RSQL。此外,您也可以指定選用的結束代碼。例如,\EXIT 15 會結束 Amazon Redshift RSQL 終端並傳回結束代碼 15。

以下範例示範從連線輸出以及從 RSQL 結束。

% rsql -D testuser DSN Connected DBMS Name: Amazon Redshift Driver Name: Amazon Redshift ODBC Driver Driver Version: 1.4.34.1000 Rsql Version: 1.0.1 Redshift Version: 1.0.29306 Type "help" for help. (testcluster) user1@dev=# \exit 15 % echo $? 15

\EXPORT

指定 RSQL 用來儲存後續 SQL SELECT 陳述式所傳回之資料庫資訊的匯出檔案名稱。

export_01.sql

\export report file='E:\\accounts.out' \rset rformat off \rset width 1500 \rset heading "General Title" \rset titledashes on select * from td_dwh.accounts; \export reset

主控台輸出

Rformat is off. Target width is 1500. Heading is set to: General Title Titledashes is on. (exported 40 rows)

\LOGON

連線至資料庫。您可以使用位置語法或連線字串形式指定連線參數。

命令語法如下:\logon {[DBNAME|- USERNAME|- HOST|- PORT|- [PASSWORD]] | conninfo}

DBNAME 是所連線的資料庫名稱。USERNAME 是連線時所用身分的使用者名稱。預設的 HOSTlocalhost。預設的 PORT5439

\LOGON 命令中指定主機名稱時,其會變成其他 \LOGON 命令的預設主機名稱。若要變更預設主機名稱,請在另外的 \LOGON 命令中指定新的 HOST

來自 user1\LOGON 命令的範例輸出如下。

(testcluster) user1@redshiftdb=# \logon dev DBMS Name: Amazon Redshift Driver Name: Amazon Redshift ODBC Driver Driver Version: 1.4.27.1000 Rsql Version: 1.0.1 You are now connected to database "dev" as user "user1". (testcluster) user1@dev=#

user2 的範例輸出。

(testcluster) user1@dev=# \logon dev user2 testcluster2.example.com Password for user user2: DBMS Name: Amazon Redshift Driver Name: Amazon Redshift ODBC Driver Driver Version: 1.4.27.1000 Rsql Version: 1.0.1 You are now connected to database "dev" as user "user2" on host "testcluster2.example.com" at port "5439". (testcluster2) user2@dev=#

\REMARK

\echo 命令的延伸。\REMARK 會將指定字串列印至輸出串流。\REMARK 會透過新增將輸出換行到不同行的能力來延伸 \echo

以下範例示範命令的輸出。

(testcluster) user1@dev=# \remark 'hello//world' hello world

\RSET

命令 \rset 會設定命令參數和變數。\rset 同時具有互動式模式和批次模式。其不支援將選項作為 bash 選項,如 -x 或引數,例如 --<arg>

其會設定變數,如下所示:

  • ERRORLEVEL

  • HEADING 和 RTITLE

  • RFORMAT

  • MAXERROR

  • TITLEDASHES

  • WIDTH

以下範例會指定標題。

\rset heading "Winter Sales Report"

如需更多如何使用 \rset 的範例,您可以在 Amazon Redshift RSQL 變數 主題中找到幾個。

\RUN

執行指定檔案中包含的 Amazon Redshift RSQL 指令碼。\RUN 會透過新增選項來擴展 \i 命令,以略過檔案中的標題行。

如果檔案名稱包含逗號、分號或空格,請以單引號將其括住。此外,如果檔案名稱後面有文字,請以引號將其括住。在 UNIX 中,檔案名稱會區分大小寫。在 Windows 中,檔案名稱不區分大小寫。

以下範例示範命令的輸出。

(testcluster) user1@dev=# \! cat test.sql select count(*) as lineitem_cnt from lineitem; select count(*) as customer_cnt from customer; select count(*) as orders_cnt from orders; (testcluster) user1@dev=# \run file=test.sql lineitem_cnt -------------- 4307207 (1 row) customer_cnt -------------- 37796166 (1 row) orders_cnt ------------ 0 (1 row) (testcluster) user1@dev=# \run file=test.sql skip=2 2 records skipped in RUN file. orders_cnt ------------ 0 (1 row)

\OS

\! 命令的別名。\OS 會執行以參數的形式傳遞的作業系統命令。執行命令後,控制項會回到 Amazon Redshift RSQL。例如,您可以執行以下命令來列印目前的系統日期時間,並返回 RSQL 終端:\os date

(testcluster) user1@dev=# \os date Tue Sep 7 20:47:54 UTC 2021

\GOTO

Amazon Redshift RSQL 的新命令。\GOTO 會略過所有中間命令,並在指定的 \LABEL 繼續處理。\LABEL 必須是前向參照。你不能跳到詞法上在 \GOTO 之前的 \LABEL

下面顯示了範例輸出。

(testcluster) user1@dev=# \! cat test.sql select count(*) as cnt from lineitem \gset select :cnt as cnt; \if :cnt > 100 \goto LABELB \endif \label LABELA \remark 'this is label LABELA' \label LABELB \remark 'this is label LABELB' (testcluster) user1@dev=# \i test.sql cnt --------- 4307207 (1 row) \label LABELA ignored \label LABELB processed this is label LABELB

\LABEL

Amazon Redshift RSQL 的新命令。\LABEL 會建立用於執行程式的進入點,以作為 \GOTO 命令的目標。

以下示範命令的範例輸出。

(testcluster) user1@dev=# \! cat test.sql select count(*) from lineitem limit 5; \goto LABELB \remark "this step was skipped by goto label"; \label LABELA \remark 'this is label LABELA' \label LABELB \remark 'this is label LABELB' (testcluster) user1@dev=# \i testgoto.sql count 4307193 (1 row) \label LABELA ignored \label LABELB processed this is label LABELB

\IF (\ELSEIF, \ELSE, \ENDIF)

\IF 和相關命令會有條件地執行輸入指令碼的某些部分。PSQL \if (\elif\else\endif) 命令的延伸。\IF\ELSEIF 支援布林運算式,包括 ANDORNOT 條件。

以下示範命令的範例輸出。

(testcluster) user1@dev=# \! cat test.sql SELECT query FROM stv_inflight LIMIT 1 \gset select :query as query; \if :query > 1000000 \remark 'Query id is greater than 1000000' \elseif :query = 1000000 \remark 'Query id is equal than 1000000' \else \remark 'Query id is less than 1000000' \endif (testcluster) user1@dev=# \i test.sql query -------- 994803 (1 row) Query id is less than 1000000

在您的分支邏輯中使用 ERRORCODE

\if :'ERRORCODE' = '00000' \remark 'The statement was executed without error' \else \remark :LAST_ERROR_MESSAGE \endif

\IF 區塊內使用 \GOTO 來控制程式碼的執行方式。