簡體   English   中英

如何在不使用 spark 的情況下從 AWS EMR 內部讀取 S3 存儲桶中的文本文件

[英]How to read a text file in S3 bucket from inside an AWS EMR without using spark

我需要從 EMR 集群打開位於 S3 存儲桶中的常規文本文件(不是鑲木地板或 CSV 文件)。 我可以使用spark.read.parquet("s3://mybucket/some_parq_file")直接打開 CSV 或 parquet 文件

But I need to read just a regular text file from EMR cluster using java.io.File or scala.io.Source . 當我嘗試時得到一個 java.io.FileNotFoundException

import scala.io.Source
val hdr = "s3://mybucket/txtfile.txt"
for (line <- Source.fromFile(hdr).getLines) {
    println(line)
}
  1. 您可以提供引導腳本,當 EMR 出現並且引導腳本(.sh 文件)可以訪問 s3 文件時(我已經多次使用過)
  2. 您可以提交 EMR 步驟,執行 jar 文件,jar 可以訪問 s3

我猜大多數 AWS 設置已經使用默認憑證鏈和默認區域提供商鏈在您的 EMR 集群中配置了憑證。 這也應該適用於 AWS Lambda。 因此,要從 EMR 集群訪問我的 S3 存儲桶,我只需要使用 AWSS3ClientBuilder

import com.amazonaws.services.s3.AmazonS3ClientBuilder
import java.io.File
import java.nio.file.{Files, StandardCopyOption}

val bucket ="s3_bucket"
val file_in_s3 = "somefile.txt"
val dest = "/tmp/local_file.txt"
val s3 = AmazonS3ClientBuilder.defaultClient()
val stream = s3.getObject(bucket, file_in_s3).getObjectContent

Files.copy(stream, new File(dest).toPath, StandardCopyOption.REPLACE_EXISTING)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM