AWS 数据管道:将 CSV 文件从 S3 上传到 DynamoDB
创始人
2024-11-18 07:00:31
0

以下是一个使用AWS数据管道将CSV文件从S3上传到DynamoDB的解决方案,包含代码示例:

  1. 创建一个Lambda函数来处理数据管道的转换和加载任务:
import boto3

def lambda_handler(event, context):
    input_bucket = event['inputBucket']
    input_key = event['inputKey']
    output_table = event['outputTable']

    # 从S3下载CSV文件
    s3 = boto3.client('s3')
    s3.download_file(input_bucket, input_key, '/tmp/input.csv')
    
    # 处理CSV文件,转换为DynamoDB条目
    items = []
    with open('/tmp/input.csv', 'r') as file:
        lines = file.readlines()
        headers = lines[0].strip().split(',')
        for line in lines[1:]:
            values = line.strip().split(',')
            item = {}
            for i in range(len(headers)):
                item[headers[i]] = values[i]
            items.append(item)
    
    # 将转换后的条目加载到DynamoDB表中
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(output_table)
    with table.batch_writer() as batch:
        for item in items:
            batch.put_item(Item=item)
    
    return 'Data pipeline completed successfully.'
  1. 创建一个数据管道定义文件(pipeline-definition.json),指定输入和输出:
{
  "objects": [
    {
      "id": "S3Input",
      "name": "S3Input",
      "schedule": {
        "ref": "DefaultSchedule"
      },
      "type": "S3DataNode",
      "dataFormat": {
        "ref": "CSVDataFormat"
      },
      "scheduleType": "ONDEMAND",
      "directoryPath": "#{myInputS3Path}",
      "errorThreshold": 0,
      "errorHandlingConfig": {
        "failOnError": true
      }
    },
    {
      "id": "DynamoDBOutput",
      "name": "DynamoDBOutput",
      "schedule": {
        "ref": "DefaultSchedule"
      },
      "type": "DynamoDBDataNode",
      "scheduleType": "ONDEMAND",
      "tableName": "#{myOutputDynamoDBTable}",
      "errorThreshold": 0,
      "errorHandlingConfig": {
        "failOnError": true
      }
    }
  ],
  "parameters": [
    {
      "id": "myInputS3Path",
      "description": "S3 input path",
      "type": "String"
    },
    {
      "id": "myOutputDynamoDBTable",
      "description": "DynamoDB output table",
      "type": "String"
    }
  ]
}
  1. 创建一个数据管道任务,指定Lambda函数和数据管道定义文件:
import boto3

def create_data_pipeline(input_bucket, input_key, output_table):
    client = boto3.client('datapipeline')
    
    # 创建数据管道定义
    with open('pipeline-definition.json', 'r') as file:
        pipeline_definition = file.read()
    
    # 创建数据管道
    response = client.create_pipeline(
        name='csv-to-dynamodb-pipeline',
        uniqueId='csv-to-dynamodb-pipeline',
        description='Pipeline to upload CSV file to DynamoDB',
        pipelineTags=[
            {
                'key': 'environment',
                'value': 'development'
            },
        ],
        pipelineObjects=[
            {
                'id': 'S3Input',
                'name': 'S3Input',
                'fields': [
                    {
                        'key': 'myInputS3Path',
                        'stringValue': 's3://' + input_bucket + '/' + input_key
                    }
                ]
            },
            {
                'id': 'DynamoDBOutput',
                'name': 'DynamoDBOutput',
                'fields': [
                    {
                        'key': 'myOutputDynamoDBTable',
                        'stringValue': output_table
                    }
                ]
            }
        ],
        parameterObjects=[
            {
                'id': 'myInputS3Path',
                'attributes': []
            },
            {
                'id': 'myOutputDynamoDBTable',
                'attributes': []
            }
        ],
        parameterValues=[
            {
                'id': 'myInputS3Path',
                'stringValue': 's3://' + input_bucket + '/' + input_key
            },
            {
                'id': 'myOutputDynamoDBTable',
                'stringValue': output_table
            }
        ],
        pipelineDefinition=pipeline_definition
    )
    

相关内容

热门资讯

透视免费!poker mast... 透视免费!poker master安卓版外挂(透视)竟然真的有挂(详细教程)(有挂教学)-哔哩哔哩,...
揭秘关于!云扑克辅助软件(透视... 揭秘关于!云扑克辅助软件(透视)其实是真的有挂(详细教程)(有挂了解)-哔哩哔哩1、许多玩家不知道云...
我来教大家!鱼扑克有挂(辅助挂... 我来教大家!鱼扑克有挂(辅助挂)的确真的有挂(详细教程)(有挂介绍)-哔哩哔哩1、玩家可以在鱼扑克有...
重要通知!德州wpk辅助真的(... 重要通知!德州wpk辅助真的(透视)果真是真的有挂(详细教程)(有挂攻略)-哔哩哔哩是一款可以让一直...
终于懂了!pokerx智能软件... 终于懂了!pokerx智能软件(透视)果真真的有挂(详细教程)(有挂了解)-哔哩哔哩;(需添加指定薇...
玩家亲测!fishpoker俱... 玩家亲测!fishpoker俱乐部有挂(透视)果真真的有挂(详细教程)(有挂详情)-哔哩哔哩是一款可...
一分钟了解!德州ai辅助神器w... 一分钟了解!德州ai辅助神器wpk(辅助挂)确实真的有挂(详细教程)(有挂方法)-哔哩哔哩;德州ai...
终于懂了!德州全自动辅助(辅助... 相信很多朋友都在电脑上玩过德州全自动辅助吧,但是很多朋友都在抱怨用电脑玩起来不方便。为此小编给大家带...
透明讲解!德州全自动辅助(透视... 透明讲解!德州全自动辅助(透视)原来是真的有挂(详细教程)(有挂技巧)-哔哩哔哩相信很多朋友都在电脑...
玩家必备攻略!红龙扑克模拟器(... 玩家必备攻略!红龙扑克模拟器(辅助挂)的确是真的有挂(详细教程)(有挂总结)-哔哩哔哩1、让任何用户...