我有一個 DynamoDB 流,它通過使用 Boto3 的 Lambda 函式對資料進行索引并將資料推送到 OpenSearch。流中的專案在這里看起來像這個 JSON 物件:
{
"d8346fda0c35418580c98209df378653": {
"M": {
"cloudPlatformAoi": {
"L": [
{
"S": "Google Cloud Platform"
}
]
},
"cloudPlatformStrength": {
"L": [
{
"S": "AWS"
}
]
},
"integratedDevelopmentEnvironmentAoi": {
"L": [
{
"S": "TextMate"
}
]
},
"webFrameworkStrength": {
"L": [
{
"S": "Drupal"
}
]
},
"lastEdited": {
"S": "13-Jun-2022 (16:34:09.233933)"
},
"title": {
"S": "This is my third post (edited)"
},
"body": {
"S": "asdf"
},
"programmingLanguageStrength": {
"L": [
{
"S": "Python"
}
]
},
"programmingLanguageAoi": {
"L": [
{
"S": "Elixir"
}
]
},
"dbAoi": {
"L": [
{
"S": "PostgreSQL"
}
]
},
"databaseStrength": {
"L": [
{
"S": "DynamoDB"
}
]
},
"webFrameworkAoi": {
"L": [
{
"S": "Symfony"
}
]
},
"timeCreated": {
"S": "09-Jun-2022 (13:30:29.967379)"
},
"integratedDevelopmentEnvironment": {
"L": [
{
"S": "TextMate"
}
]
},
"level": {
"S": "one"
}
}
},
"1d5c49e0fc8c458ebc2e74835831a5c8": {
"M": {
"cloudPlatformAoi": {
"L": [
{
"S": "Google Cloud Platform"
}
]
},
"cloudPlatformStrength": {
"L": [
{
"S": "Google Cloud Platform"
}
]
},
"integratedDevelopmentEnvironmentAoi": {
"L": [
{
"S": "Vim"
}
]
},
"webFrameworkStrength": {
"L": [
{
"S": "Flask"
}
]
},
"lastEdited": {
"S": "13-Jun-2022 (17:30:32.808160)"
},
"title": {
"S": "My First Post (edited) 1"
},
"body": {
"S": "test"
},
"programmingLanguageStrength": {
"L": [
{
"S": "Python"
}
]
},
"programmingLanguageAoi": {
"L": [
{
"S": "Erlang"
}
]
},
"dbAoi": {
"L": [
{
"S": "Oracle"
}
]
},
"databaseStrength": {
"L": [
{
"S": "Couchbase"
}
]
},
"webFrameworkAoi": {
"L": [
{
"S": "Spring"
}
]
},
"timeCreated": {
"S": "13-Jun-2022 (16:28:23.582059)"
},
"integratedDevelopmentEnvironment": {
"L": [
{
"S": "Vim"
}
]
},
"awsomeBuilderStage": {
"S": "2"
}
}
},
"bd9cc68521564858871a7482d77bb1a5": {
"M": {
"cloudPlatformAoi": {
"L": [
{
"S": "Google Cloud Platform"
}
]
},
"cloudPlatformStrength": {
"L": [
{
"S": "Google Cloud Platform"
}
]
},
"integratedDevelopmentEnvironmentAoi": {
"L": [
{
"S": "Vim"
}
]
},
"webFrameworkStrength": {
"L": [
{
"S": "Flask"
}
]
},
"lastEdited": {
"S": "13-Jun-2022 (16:37:50.576490)"
},
"title": {
"S": "My First Post (edited)"
},
"body": {
"S": "test"
},
"programmingLanguageStrength": {
"L": [
{
"S": "Python"
}
]
},
"programmingLanguageAoi": {
"L": [
{
"S": "Erlang"
}
]
},
"dbAoi": {
"L": [
{
"S": "Oracle"
}
]
},
"databaseStrength": {
"L": [
{
"S": "Couchbase"
}
]
},
"webFrameworkAoi": {
"L": [
{
"S": "Spring"
}
]
},
"timeCreated": {
"S": "13-Jun-2022 (16:28:23.582059)"
},
"integratedDevelopmentEnvironment": {
"L": [
{
"S": "Vim"
}
]
},
"awsomeBuilderStage": {
"S": "3"
}
}
}
}
當我索引物件并將其推送到 OpenSearch 時,它包括與每個嵌套 JSON 物件關聯的型別。例如:
"cloudPlatformStrength": {
"L": [
{
"S": "AWS"
}
]
}
代替:
"cloudPlatformStrength": [
"Google Cloud Platform"
]
我將如何修復推送到 OpenSearch 的資料?我是否必須對每個條目執行 ETL 程序?或者有更好的方法嗎?
uj5u.com熱心網友回復:
這應該做你想要的:
from boto3.dynamodb.types import TypeDeserializer
def handler(event, context):
deserializer = TypeDeserializer()
for record in event['Records']:
data = {key: deserializer.deserialize(value) for key, value in
record['dynamodb']['NewImage'].items()}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/490944.html
標籤:Python json aws-lambda 亚马逊-dynamodb 博托3
上一篇:JSON陣列和簡單資料在同一列
