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
    )
    

相关内容

热门资讯

透视揭露!wepoker辅助脚... 透视揭露!wepoker辅助脚本,wepoker私人局透视-确实是真的有辅助神器(哔哩哔哩)1、下载...
透视科普!wpk透视是真的假的... 透视科普!wpk透视是真的假的,wpk软件是正规的吗-真是存在有辅助软件(哔哩哔哩)1、金币登录送、...
透视解密!wepoker辅助真... 透视解密!wepoker辅助真的假的,We poker辅助器下载-真是真的有辅助神器(哔哩哔哩)亲,...
透视推荐!hhpoker辅助软... 透视推荐!hhpoker辅助软件,hhpoker德州有挂吗-果然是有辅助神器(哔哩哔哩)1、模拟器是...
透视科普!wpk透视是真的假的... 透视科普!wpk透视是真的假的,wpk辅助器是真的吗-真是是真的有辅助攻略(哔哩哔哩)1、有没有辅助...
透视曝光!wepoker可以透... 透视曝光!wepoker可以透视码,wejoker内置辅助-本来有辅助教程(哔哩哔哩)1、该软件可以...
透视揭露!wepoker破解工... 透视揭露!wepoker破解工具,wepoker怎么设置盖牌-本来一直总是有辅助方法(哔哩哔哩)1、...
透视有挂!有哪些免费的wpk作... 透视有挂!有哪些免费的wpk作弊码,wpk辅助器是真的吗-果然一直总是有辅助脚本(哔哩哔哩)1、公共...
透视关于!德扑圈透视挂,德普之... 透视关于!德扑圈透视挂,德普之星透视辅助-好像是真的有辅助软件(哔哩哔哩)脚本下载中分为三种模型:挂...
透视解密!德普辅助器怎么用,德... 透视解密!德普辅助器怎么用,德普之星透视-好像是有辅助app(哔哩哔哩)1、完成辅助器v3.3的残局...