LoginSignup
1
1

More than 5 years have passed since last update.

Spark on EMRでS3のファイルを扱う

Posted at

概要

Sparkで、sc.textFile, rdd.saveAsTextFileあたりは色々なサンプルに書いてありますが、他の例はあまり見なかったのでメモしておきます。

やりかた

Path#getFileSystem(sc.hadoopConfiguration)を使う

ファイルの存在確認

    val path = "s3://hoge"
    val fsPath = new Path(path)
    val fsOut = fsPath.getFileSystem(sc.hadoopConfiguration)
    if (fsOut.exists(fsPath)) {
      println(s"input path found. path: $path")
    }else{
      println(s"input path not found. path: $path")
    }

削除。

    val path = "s3://hoge"
    val fsPath = new Path(path)
    val fsOut = fsPath.getFileSystem(sc.hadoopConfiguration)
    if (fsOut.exists(fsPath)) {
      fsOut.delete(fsPath, true)
      println(s"deleted. path: $path")
    }

注意

sc.textFile などもそうだが、s3を使おうと思うとHadoopクラスタに以下の設定がされていて、クラスパスも通ってる必要がある様子。(s3, s3nスキーマを使う場合)

core-site.xml
<property>
  <name>fs.s3.impl</name>
  <value>org.apache.hadoop.fs.s3.S3FileSystem</value>
</property>

<property>
  <name>fs.s3n.impl</name>
  <value>org.apache.hadoop.fs.s3native.NativeS3FileSystem</value>
</property>
core-site.xml
<property>
  <name>fs.s3.awsAccessKeyId</name>
  <value>some id</value>
</property>

<property>
  <name>fs.s3n.awsAccessKeyId</name>
  <value>some id</value>
</property>

<property>
  <name>fs.s3.awsSecretAccessKey</name>
  <value>some key</value>
</property>

<property>
  <name>fs.s3n.awsSecretAccessKey</name>
  <value>some key</value>
</property>

TODO

  • .crcファイルはどういうときに作られるんだろう。
  • localのspark-submitでs3に繋ごうとするとClassNotFoundExceptionで落ちる。
    • Sparkデフォルトでは org.apache.hadoop.fs.s3native.NativeS3FileSystem が入ってない。
    • Jarを追加すれば普通に動きそう。
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1