【问题标题】:Read and parse CSV file in S3 without downloading the entire file using Python在 S3 中读取和解析 CSV 文件,无需使用 Python 下载整个文件
【发布时间】:2019-07-09 18:43:00
【问题描述】:

所以,我想从 S3 存储桶中读取一个大的 CSV 文件,但我不希望该文件完全下载到内存中,我想做的是以某种方式将文件分块流式传输,然后进行处理。

到目前为止,这就是我所做的,但我认为这不能解决问题。

import logging
import boto3
import codecs
import os
import csv

LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)

s3 = boto3.client('s3')


def lambda_handler(event, context):
    # retrieve bucket name and file_key from the S3 event
    bucket_name = event['Records'][0]['s3']['bucket']['name']
    file_key = event['Records'][0]['s3']['object']['key']
    chunk, chunksize = [], 1000
    if file_key.endswith('.csv'):
        LOGGER.info('Reading {} from {}'.format(file_key, bucket_name))

        # get the object
        obj = s3.get_object(Bucket=bucket_name, Key=file_key)
        file_object = obj['Body']
        count = 0
        for i, line in enumerate(file_object):
            count += 1
            if (i % chunksize == 0 and i > 0):
                process_chunk(chunk)
                del chunk[:]
            chunk.append(line)





def process_chunk(chuck):
    print(len(chuck))

【问题讨论】:

标签: amazon-s3 python-3.6


【解决方案1】:

这将完成您想要实现的目标。它不会将整个文件下载到内存中,而是分块下载,处理并继续:

  from smart_open import smart_open
  import csv

  def get_s3_file_stream(s3_path):
      """
      This function will return a stream of the s3 file.
      The s3_path should be of the format: '<bucket_name>/<file_path_inside_the_bucket>'
      """
      #This is the full path with credentials:
      complete_s3_path = 's3://' + aws_access_key_id + ':' + aws_secret_access_key + '@' + s3_path
      return smart_open(complete_s3_path, encoding='utf8')

  def download_and_process_csv:
      datareader = csv.DictReader(get_s3_file_stream(s3_path))
      for row in datareader:
          yield process_csv(row) # write a function to do whatever you want to do with the CSV

【讨论】:

    【解决方案2】:

    您尝试过 AWS Athena https://aws.amazon.com/athena/ 吗? 它非常出色的无服务器,即付即用。无需下载文件,它就可以完成您想要的一切。 BlazingSql 是开源的,在大数据问题的情况下也很有用。

    【讨论】:

      猜你喜欢
      • 2017-02-13
      • 2021-05-22
      • 2020-05-25
      • 2021-11-22
      • 1970-01-01
      • 2019-09-14
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多