要从 JSON 文件中创建 Hive 表,您需要专门为您的 JSON 结构编写 CREATE TABLE statement based on HiveQL DDL standards。
如果您使用嵌套的 JSON 文件可能会非常复杂,因此我建议您使用这个快速简单的生成器:https://hivetablegenerator.com/
使用 HiveQL 分析 JSON 文件需要 org.openx.data.jsonserde.JsonSerDe 或 org.apache.hive.hcatalog.data.JsonSerDe 才能正常工作。
org.apache.hive.hcatalog.data.JsonSerDe
这是默认的JSON SerDe from Apache。这通常用于处理事件等 JSON 数据。这些事件表示为由新行分隔的 JSON 编码文本块。 Hive JSON SerDe 不允许映射或结构键名中的重复键。
org.openx.data.jsonserde.JsonSerDe
OpenX JSON SerDe类似于原生Apache;但是,它提供了多个可选属性,例如“ignore.malformed.json”、“case.insensitive”等等。在我看来,它通常在处理嵌套的 JSON 文件时效果更好。
以这个复杂的 JSON 文件为例:
{
"schemaVersion": "1.0",
"id": "07c1687a0fd34ebf8a42e8a8627321dc",
"accountId": "123456677",
"partition": "aws",
"region": "us-west-2",
"severity": {
"score": "0",
"description": "Informational"
},
"createdAt": "2021-02-27T18:57:07Z",
"resourcesAffected": {
"s3Bucket": {
"arn": "arn:aws:s3:::bucket-sample",
"name": "bucket-sample",
"createdAt": "2020-08-09T07:24:55Z",
"owner": {
"displayName": "account-name",
"id": "919a30c2f56c0b220c32e9234jnkj435n6jk4nk"
},
"tags": [],
"defaultServerSideEncryption": {
"encryptionType": "AES256"
},
"publicAccess": {
"permissionConfiguration": {
"bucketLevelPermissions": {
"accessControlList": {
"allowsPublicReadAccess": false,
"allowsPublicWriteAccess": false
},
"bucketPolicy": {
"allowsPublicReadAccess": true,
"allowsPublicWriteAccess": false
},
"blockPublicAccess": {
"ignorePublicAcls": false,
"restrictPublicBuckets": false,
"blockPublicAcls": false,
"blockPublicPolicy": false
}
},
"accountLevelPermissions": {
"blockPublicAccess": {
"ignorePublicAcls": false,
"restrictPublicBuckets": false,
"blockPublicAcls": false,
"blockPublicPolicy": false
}
}
},
"effectivePermission": "PUBLIC"
}
},
"s3Object": {
"bucketArn": "arn:aws:s3:::bucket-sample",
"key": "2021/01/17191133/Camping-Checklist-Google-Docs.pdf",
"path": "bucket-sample/2021/01/17191133/Camping-Checklist-Google-Docs.pdf",
"extension": "pdf",
"lastModified": "2021-01-17T22:11:34Z",
"eTag": "e8d990704042d2e1b7bb504fb5868095",
"versionId": "isqHLkSsQUMbbULNT2nMDneMG0zqitbD",
"serverSideEncryption": {
"encryptionType": "AES256"
},
"size": "150532",
"storageClass": "STANDARD",
"tags": [],
"publicAccess": true
}
},
"category": "CLASSIFICATION",
"classificationDetails": {
"jobArn": "arn:aws:macie2:us-west-2:123412341341:classification-job/d6cf41ccc7ea8daf3bd53ddcb86a2da5",
"result": {
"status": {
"code": "COMPLETE"
},
"sizeClassified": "150532",
"mimeType": "application/pdf",
"sensitiveData": []
},
"detailedResultsLocation": "s3://bucket-macie/AWSLogs/123412341341/Macie/us-west-2/d6cf41ccc7ea8daf3bd53ddcb86a2da5/123412341341/50de3137-9806-3e43-9b6e-a6158fdb0e3b.jsonl.gz",
"jobId": "d6cf41ccc7ea8daf3bd53ddcb86a2da5"
}
}
需要以下创建表语句:
CREATE EXTERNAL TABLE IF NOT EXISTS `macie`.`macie_bucket` (
`schemaVersion` STRING,
`id` STRING,
`accountId` STRING,
`partition` STRING,
`region` STRING,
`severity` STRUCT<
`score`:STRING,
`description`:STRING>,
`createdAt` STRING,
`resourcesAffected` STRUCT<
`s3Bucket`:STRUCT<
`arn`:STRING,
`name`:STRING,
`createdAt`:STRING,
`owner`:STRUCT<
`displayName`:STRING,
`id`:STRING>,
`defaultServerSideEncryption`:STRUCT<
`encryptionType`:STRING>,
`publicAccess`:STRUCT<
`permissionConfiguration`:STRUCT<
`bucketLevelPermissions`:STRUCT<
`accessControlList`:STRUCT<
`allowsPublicReadAccess`:BOOLEAN,
`allowsPublicWriteAccess`:BOOLEAN>,
`bucketPolicy`:STRUCT<
`allowsPublicReadAccess`:BOOLEAN,
`allowsPublicWriteAccess`:BOOLEAN>,
`blockPublicAccess`:STRUCT<
`ignorePublicAcls`:BOOLEAN,
`restrictPublicBuckets`:BOOLEAN,
`blockPublicAcls`:BOOLEAN,
`blockPublicPolicy`:BOOLEAN>>,
`accountLevelPermissions`:STRUCT<
`blockPublicAccess`:STRUCT<
`ignorePublicAcls`:BOOLEAN,
`restrictPublicBuckets`:BOOLEAN,
`blockPublicAcls`:BOOLEAN,
`blockPublicPolicy`:BOOLEAN>>>,
`effectivePermission`:STRING>>,
`s3Object`:STRUCT<
`bucketArn`:STRING,
`key`:STRING,
`path`:STRING,
`extension`:STRING,
`lastModified`:STRING,
`eTag`:STRING,
`versionId`:STRING,
`serverSideEncryption`:STRUCT<
`encryptionType`:STRING>,
`size`:STRING,
`storageClass`:STRING,
`publicAccess`:BOOLEAN>>,
`category` STRING,
`classificationDetails` STRUCT<
`jobArn`:STRING,
`result`:STRUCT<
`status`:STRUCT<
`code`:STRING>,
`sizeClassified`:STRING,
`mimeType`:STRING>,
`detailedResultsLocation`:STRING,
`jobId`:STRING>)
ROW FORMAT SERDE
'org.openx.data.jsonserde.JsonSerDe'
LOCATION
's3://awsexamplebucket1-logs/AWSLogs/'
如果您需要亚马逊提供有关如何为 AWS Athena 使用嵌套 JSON 文件创建表的更多信息,请查看此链接:https://aws.amazon.com/blogs/big-data/create-tables-in-amazon-athena-from-nested-json-and-mappings-using-jsonserde/