Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 Python UDF 將繼續正常運作至 2026 年 6 月 30 日。如需詳細資訊,請參閱部落格文章
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
擷取 SQL 陳述式的結果
若要從執行的 SQL 陳述式擷取結果,請使用 redshift-data get-statement-result或 redshift-data get-statement-result-v2AWS CLI命令。來自 get-statement-result 的結果為 JSON 格式。來自 get-statement-result-v2 的結果為 CSV 格式。您可以提供所收到的 Id 以回應 execute-statement 或 batch-execute-statement。由 batch-execute-statement 執行的 SQL 陳述式的 Id 值可以在 describe-statement 的結果中擷取,並且會加上由冒號和序號組成的字尾 (例如 b2906c76-fa6e-4cdf-8c5f-4de1ff9b7652:2)。如果您使用 batch-execute-statement 執行多個 SQL 陳述式,則每個 SQL 陳述式都會有一個 Id 值,如 describe-statement 中所示。執行此命令的授權取決於呼叫者的 IAM 許可。
下列陳述式會傳回由 ResultFormat 預設為 JSON 的 execute-statement 所執行 SQL 陳述式的結果。若要擷取結果,請呼叫 get-statement-result 操作。
aws redshift-data get-statement-result --id d9b6c0c9-0747-4bf4-b142-e8883122f766
下列陳述式會傳回由 batch-execute-statement 執行的第二個 SQL 陳述式的結果。
aws redshift-data get-statement-result --id b2906c76-fa6e-4cdf-8c5f-4de1ff9b7652:2
以下是呼叫 get-statement-result 的回應範例,其中 SQL 結果會在回應的 Records 索引鍵中以 JSON 格式傳回。
{ "ColumnMetadata": [ { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "userid", "length": 0, "name": "userid", "nullable": 0, "precision": 10, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int4" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "query", "length": 0, "name": "query", "nullable": 0, "precision": 10, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int4" }, { "isCaseSensitive": true, "isCurrency": false, "isSigned": false, "label": "label", "length": 0, "name": "label", "nullable": 0, "precision": 320, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "bpchar" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "xid", "length": 0, "name": "xid", "nullable": 0, "precision": 19, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int8" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "pid", "length": 0, "name": "pid", "nullable": 0, "precision": 10, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int4" }, { "isCaseSensitive": true, "isCurrency": false, "isSigned": false, "label": "database", "length": 0, "name": "database", "nullable": 0, "precision": 32, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "bpchar" }, { "isCaseSensitive": true, "isCurrency": false, "isSigned": false, "label": "querytxt", "length": 0, "name": "querytxt", "nullable": 0, "precision": 4000, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "bpchar" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": false, "label": "starttime", "length": 0, "name": "starttime", "nullable": 0, "precision": 29, "scale": 6, "schemaName": "", "tableName": "stll_query", "typeName": "timestamp" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": false, "label": "endtime", "length": 0, "name": "endtime", "nullable": 0, "precision": 29, "scale": 6, "schemaName": "", "tableName": "stll_query", "type": 93, "typeName": "timestamp" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "aborted", "length": 0, "name": "aborted", "nullable": 0, "precision": 10, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int4" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "insert_pristine", "length": 0, "name": "insert_pristine", "nullable": 0, "precision": 10, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int4" }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "concurrency_scaling_status", "length": 0, "name": "concurrency_scaling_status", "nullable": 0, "precision": 10, "scale": 0, "schemaName": "", "tableName": "stll_query", "typeName": "int4" } ], "Records": [ [ { "longValue": 1 }, { "longValue": 3 }, { "stringValue": "health" }, { "longValue": 1023 }, { "longValue": 15279 }, { "stringValue": "dev" }, { "stringValue": "select system_status from stv_gui_status;" }, { "stringValue": "2020-08-21 17:33:51.88712" }, { "stringValue": "2020-08-21 17:33:52.974306" }, { "longValue": 0 }, { "longValue": 0 }, { "longValue": 6 } ] ], "TotalNumRows": 1 }
下列範例顯示 execute-statement 執行的 SQL 陳述式,且傳回的結果為 JSON 格式。資料表 testingtable 有三個整數欄 (col1、col2、col3),而且有三列的值 (1、2、3)、(4、5、6) 和 (7、8、9)。
aws redshift-data execute-statement --database dev --sql "SELECT col1, col2, col3 FROM testingtable" --cluster-id mycluster-test --result-format JSON
{ "ClusterIdentifier": "mycluster-test", "CreatedAt": "2024-04-02T16:45:25.144000+00:00", "Database": "dev", "DbUser": "IAMR:Administrator", "Id": "d468d942-6df9-4f85-8ae3-bac01a61aec3" }
以下是呼叫 get-statement-result 的回應範例,其中 SQL 結果會在回應的 Records 索引鍵中以 JSON 格式傳回。
aws redshift-data get-statement-result --id d468d942-6df9-4f85-8ae3-bac01a61aec3
{ "Records": [ [ { "longValue": 1 }, { "longValue": 2 }, { "longValue": 3 } ], [ { "longValue": 4 }, { "longValue": 5 }, { "longValue": 6 } ], [ { "longValue": 7 }, { "longValue": 8 }, { "longValue": 9 } ] ], "ColumnMetadata": [ { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "col1", "name": "col1", "nullable": 1, "precision": 10, "scale": 0, "schemaName": "public", "tableName": "testingtable", "typeName": "int4", "length": 0 }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "col2", "name": "col2", "nullable": 1, "precision": 10, "scale": 0, "schemaName": "public", "tableName": "testingtable", "typeName": "int4", "length": 0 }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "col3", "name": "col3", "nullable": 1, "precision": 10, "scale": 0, "schemaName": "public", "tableName": "testingtable", "typeName": "int4", "length": 0 } ], "TotalNumRows": 3 }
下列範例顯示 execute-statement 執行的 SQL 陳述式,且傳回的結果為 CSV 格式。資料表 testingtable 有三個整數欄 (col1、col2、col3),而且有三列的值 (1、2、3)、(4、5、6) 和 (7、8、9)。
aws redshift-data execute-statement --database dev --sql "SELECT col1, col2, col3 FROM testingtable" --cluster-id mycluster-test --result-format CSV
{ "ClusterIdentifier": "mycluster-test", "CreatedAt": "2024-04-02T16:45:25.144000+00:00", "Database": "dev", "DbUser": "IAMR:Administrator", "Id": "d468d942-6df9-4f85-8ae3-bac01a61aec3" }
以下是呼叫 get-statement-result-v2 的回應範例,其中 SQL 結果會在回應的 Records 索引鍵中以 CSV 格式傳回。資料列會以換行和新行 (\r\n) 符號分隔。Records 中傳回的第一列為欄標頭。以 CSV 格式傳回的結果會以 1 MB 為單位傳回,且每個區塊可以儲存最大 1MB 的任意列數。
aws redshift-data get-statement-result-v2 --id d468d942-6df9-4f85-8ae3-bac01a61aec3
{ "Records": [ { "CSVRecords": "col1,col2,col3\r\n1,2,3\r\n4,5,6\r\n7,8,9\r\n" } ], "ColumnMetadata": [ { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "col1", "name": "col1", "nullable": 1, "precision": 10, "scale": 0, "schemaName": "public", "tableName": "testingtable", "typeName": "int4", "length": 0 }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "col2", "name": "col2", "nullable": 1, "precision": 10, "scale": 0, "schemaName": "public", "tableName": "testingtable", "typeName": "int4", "length": 0 }, { "isCaseSensitive": false, "isCurrency": false, "isSigned": true, "label": "col3", "name": "col3", "nullable": 1, "precision": 10, "scale": 0, "schemaName": "public", "tableName": "testingtable", "typeName": "int4", "length": 0 } ], "TotalNumRows": 3, "ResultFormat": "csv" }