如何解決查詢 Amazon Athena 資料表時的「失敗: NullPointerException 名稱為空」錯誤?

3 分的閱讀內容
0

查詢 Amazon Athena 資料表時,我收到「失敗: NullPointerException 名稱為空」的錯誤。

簡短描述

在 AWS Glue Data Catalog 中未為查詢的資料表定義屬性 TableType 時,您會收到此錯誤。TableType 屬性用於定義資料表是否為外部資料表或視圖。此屬性可以使用值定義,例如 EXTERNAL_TABLE 和 VIRTUAL_VIEW。如果您要執行 DDL 查詢,例如 SHOW CREATE TABLEMSCK REPAIR TABLE,則必須定義 TableType 屬性。

如果您使用 AWS CloudFormation 範本或 AWS Glue API 定義資料表,但未指定 TableType 為其中一個屬性,則可能會收到此錯誤。

解決方法

若要解決此錯誤,請根據您的使用情況執行下列一或多項動作:

在資料表建立期間新增屬性

在建立資料表時新增 TableType 屬性。

**注意:**如果資料表是使用 DDL 陳述式或 AWS Glue 爬蟲程式建立的,就會自動定義 TableType 屬性。

更新 CloudFormation 範本或 AWS Glue API 呼叫

如果使用 CloudFormation 範本或 AWS Glue API 定義資料表,但未指定 TableType,則更新 CloudFormation 範本或 AWS Glue API 呼叫以新增 TableType 屬性。

使用 AWS Command Line Interface (AWS CLI) 更新資料表

若要更新資料表的 TableType 屬性,請使用 AWS CLI 命令 aws glue update-table。若要執行此命令,您必須擁有用於定義整個資料表架構的 TableInput 物件。

為取得資料表的 TableInput 物件,請執行 aws glue get-table 命令。然後,按照下列步驟的定義,更新此命令的輸出。

**注意:**如果您在執行 AWS CLI 命令時收到錯誤訊息,請確認您使用的是最新版本的 AWS CLI

1.    在資料表上執行類似下列的命令。

aws glue get-table --catalog-id 1111222233334444 --database doc_example_database --name doc_example_table

2.    您會看到類似下列內容的輸出:

{
    "Table": {
            "StorageDescriptor": {
            "OutputFormat": "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat",
            "SortColumns": [],
            "InputFormat": "org.apache.hadoop.mapred.TextInputFormat",
            "SerdeInfo": {
                    "SerializationLibrary": "org.apache.hadoop.hive.serde2.OpenCSVSerde",
                    "Parameters": {
                    "serialization.format": "1"
                        }
            },
            "Parameters": {
                "separatorChar": ","
            },
            "Location": "s3://doc_example_bucket/doc_example_prefix/",
            "NumberOfBuckets": 0,
            "StoredAsSubDirectories": false,
            "Columns": [
                {
                    "Type": "int",
                    "Name": "id"
                },
                {
                    "Type": "string",
                    "Name": "name"
                }
            ],
            "Compressed": false
        },
        "UpdateTime": 1620508098.0,
        "IsRegisteredWithLakeFormation": false,
        "Name": "doc_example_table",
        "CreatedBy": "arn:aws:iam::1111222233334444:user/Administrator",
        "DatabaseName": "doc_example_database",
        "Owner": "1111222233334444",
        "Retention": 0,
        "CreateTime": 1619909955.0,
        "Description": "tb description"
    }
}

3.    從以上輸出中移除參數,例如 UpdateTime、IsRegisteredWithLakeFormation、CreatedBy、DatabaseName 和 CreateTime。AWS Glue 不支援這些參數。如果您在執行 update-table 命令時於 TableInput 屬性中包含這些參數,可能會看到下列錯誤:

Parameter validation failed:
Unknown parameter in TableInput: "UpdateTime", must be one of: Name, Description, Owner, LastAccessTime, LastAnalyzedTime, Retention, StorageDescriptor, PartitionKeys, ViewOriginalText, ViewExpandedText, TableType, Parameters
Unknown parameter in TableInput: "IsRegisteredWithLakeFormation", must be one of: Name, Description, Owner, LastAccessTime, LastAnalyzedTime, Retention, StorageDescriptor, PartitionKeys, ViewOriginalText, ViewExpandedText, TableType, Parameters
Unknown parameter in TableInput: "CreatedBy", must be one of: Name, Description, Owner, LastAccessTime, LastAnalyzedTime, Retention, StorageDescriptor, PartitionKeys, ViewOriginalText, ViewExpandedText, TableType, Parameters
Unknown parameter in TableInput: "DatabaseName", must be one of: Name, Description, Owner, LastAccessTime, LastAnalyzedTime, Retention, StorageDescriptor, PartitionKeys, ViewOriginalText, ViewExpandedText, TableType, Parameters
Unknown parameter in TableInput: "CreateTime", must be one of: Name, Description, Owner, LastAccessTime, LastAnalyzedTime, Retention, StorageDescriptor, PartitionKeys, ViewOriginalText, ViewExpandedText, TableType, Parameters

4.    新增參數 "TableType": "EXTERNAL_TABLE" 到輸出。

5.    使用輸出作為 TableInput 參數以執行 update-table 命令。

aws glue update-table --catalog-id 1111222233334444 --database-name doc_example_database --table-input'{
        "StorageDescriptor": {
            "OutputFormat": "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat",
            "SortColumns": [],
            "InputFormat": "org.apache.hadoop.mapred.TextInputFormat",
            "SerdeInfo": {
                "SerializationLibrary": "org.apache.hadoop.hive.serde2.OpenCSVSerde",
                "Parameters": {
                    "serialization.format":"1"
                }
            },
            "Parameters": {
                "separatorChar":","
            },
            "Location": "s3://doc_example_bucket/doc_example_prefix/",
            "NumberOfBuckets": 0,
            "StoredAsSubDirectories": false,
            "Columns": [
                {
                    "Type": "int",
                    "Name": "id"
                },
                {
                    "Type": "string",
                    "Name": "name"
                }
            ],
            "Compressed": false
        },
        "Name": "doc_example_table",
        "TableType": "EXTERNAL_TABLE",
        "Owner": "1111222233334444",
        "Retention": 0,
        "Description": "tb description"
    }

請確保替換上述命令的下列內容:

  • 使用您的資料庫名稱替換 doc_example_database
  • 使用您的資料表名稱替換 doc_example_table
  • 使用您的帳戶 ID 替換 1111222233334444
  • 使用儲存資料表的 Amazon Simple Storage Service (Amazon S3) 位置替換 s3://doc_example_bucket/doc_example_prefix/

執行上述命令後,TableType 參數會更新,而 SHOW CREATE TABLE 或 MSCK REPAIR TABLE 等 DDL 查詢會成功。


相關資訊

Athena 疑難排解

AWS 官方
AWS 官方已更新 3 年前