在 Scala 中使用 AWS Glue 在 S3 中加载 CSV 文件

Load CSV file in S3 with AWS Glue in Scala

这应该很容易... 对于我的 AWS Glue 作业,我想从 S3 上的 CSV 文件加载我的配置设置。这样,我的 lambda 函数就可以触发作业并将文件名作为参数发送。在 Python 中,我可以轻松做到这一点:

s3 = boto3.resource('s3')
bucket = s3.Bucket(<my bucket name>)
obj = s3.Object(<my bucket name>,<file location>)
data = obj.get()['Body'].read().decode('utf-8')

在 Scala 中,我找不到任何与 boto3 库等效的东西。我试过这样的 getSourceWithFormat 函数:

var datasource = glueContext.getSourceWithFormat("s3", JsonOptions(Map("paths" -> Set(<file folder name>)),
     Map("exclusions" -> <file patterns to exclude>)), 
     format = "csv", formatOptions = JsonOptions(Map("separator" -> "\t"),Map("header" -> true)))
     .getDynamicFrame()

但我只想加载一个文件并像操作字符串数组一样操作它。

谢谢!

如何将您的数据源转换为数据框,然后对其调用 collect 方法?

val  myArray = datasource.toDF().collect

应该是这样的:

  1. 在 Lambda 中编写 python 代码以读取文件。
  2. 使用 Scala 代码创建 Glue 作业。
  3. 确保您启用了触发器,它将调用带有文件名的 Glue 作业。