简体   繁体   English

如何在 AWS Glue 作业中从 S3 中逐行读取 CSV 文件

[英]How do I read row by row of a CSV file from S3 in AWS Glue Job

Hi I am very new to AWS.嗨,我对 AWS 很陌生。

I am trying to retrieve a 5gb csv file that I have stored in a s3 bucket, do ETL on it and load it into a DynamoDB table using AWS Glue.我正在尝试检索存储在 s3 存储桶中的 5gb csv 文件,对其进行 ETL 并使用 AWS Glue 将其加载到 DynamoDB 表中。 My glue job is pure python bash shell not using spark.我的胶水工作是纯 python bash shell 不使用火花。

My problem is that when I try to retrieve the file.我的问题是当我尝试检索文件时。 I am getting File not found exception.我收到文件未找到异常。 Here is my code:这是我的代码:

import boto3
import logging
import csv
import s3fs

from boto3 import client
from boto3.dynamodb.conditions import Key
from botocore.exceptions import ClientError

csv_file_path = 's3://my_s3_bucket/mycsv_file.csv'

A few lines down within my class.......:我的 class 中的几行......:

with open(self.csv_file_path, "r") as input:
       csv_reader = csv.reader(input, delimiter='^', quoting=csv.QUOTE_NONE)

       for row in csv_reader:

within the with open function is where I get file not found.在打开的 function 中是我找不到文件的地方。 Even though it is there.即使它在那里。 I really do not want to use pandas.我真的不想使用 pandas。 Weve had problems working with pandas within glue.我们在胶水中使用 pandas 时遇到问题。 Since this a 5gb file I cant store in memory thats why im trying to open it and read it row by row.由于这是一个 5gb 文件,我无法将其存储在 memory 中,这就是为什么我试图打开它并逐行读取它的原因。

I would really appreciate the help on this.我真的很感激这方面的帮助。

Also I have the correct IAM glue permissions setup and everything.我也有正确的 IAM 胶水权限设置和一切。

I figured it out我想到了

you have to use the s3 module from boto你必须使用 boto 的 s3 模块

s3 = boto3.client('s3')

file = s3.get_object(Bucket='bucket_name', Key='file_name')

lines = file['Body'].read().decode('utf-8').splitlines(True)

csv_reader = csv.reader(lines, delimiter=',', quoting=csv.QUOTE_NONE)

and then just create a for loop for the csv reader然后为 csv 阅读器创建一个 for 循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 aws lambda 中从 aws s3 读取 csv 文件 - How do I read a csv file from aws s3 in aws lambda 如何使用 Glue 作业将 JSON 从 s3 转换为 CSV 文件并将其保存在同一个 s3 存储桶中 - How to convert JSON to CSV file from s3 and save it in same s3 bucket using Glue job 如何使用 AWS Glue 从 S3 存储桶合并 CSV 文件并将其保存回 S3 - How to merge CSV file from S3 bucket and save it back into S3 using AWS Glue AWS Glue - 从 sql server 表中读取并作为自定义 CSV 文件写入 S3 - AWS Glue - read from a sql server table and write to S3 as a custom CSV file 如何使用 AWS GLUE 对 S3 CSV 文件进行排序 - How to sort S3 CSV File using AWS GLUE AWS胶水作业在s3上的大输入csv数据失败 - AWS Glue job is failing for large input csv data on s3 从 AWS 胶上的 S3 读取 csv 和文本文件而不必将其作为动态数据帧读取的最佳方法是什么? - What is the best way to read a csv and text file from S3 on AWS glue without having to read it as a Dynamic daataframe? 从 S3 解压缩文件并将其写回 S3 的 AWS Glue 作业 - AWS Glue job to unzip a file from S3 and write it back to S3 如何从用 pyspark 编写的胶水 ETL 作业中保存 S3 中的机器学习模型(Kmeans)? - How do I save machine learning model(Kmeans) in S3 from glue ETL job in written in pyspark? 从 AWS Redshift 到 S3 的 AWS Glue ETL 作业失败 - AWS Glue ETL job from AWS Redshift to S3 fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM