Help us understand the problem. What is going on with this article?

PySparkでS3上のファイルマージ

More than 1 year has passed since last update.

マージ処理

いろいろ調べた結果以下のように

を元にScalaコードをPythonへ移しました。

  • s3as3でもいいのだろうか?未検証
  • copyMergeの第5引数はTrueでマージ元ディレクトリを消す
  • マージしたファイルをマージ元ディレクトリに入れていると一緒に削除されてしまう

ScalaのAPI, copyMerge

public static boolean copyMerge(FileSystem srcFS,
                Path srcDir,
                FileSystem dstFS,
                Path dstFile,
                boolean deleteSource,
                Configuration conf,
                String addString)
                         throws IOException
Copy all files in a directory to one output file (merge).

書いたコード

    def merge(bucket, suffix, file_name):
        uri = spark_context._gateway.jvm.java.net.URI
        path = spark_context._gateway.jvm.org.apache.hadoop.fs.Path
        file_sys = spark_context._gateway.jvm.org.apache.hadoop.fs.s3.S3FileSystem
        file_util = spark_context._gateway.jvm.org.apache.hadoop.fs.FileUtil
        conf = spark_context._jsc.hadoopConfiguration()

        # ex) bucket = 'hangedman.s3.com'
        # ex) suffix = '/output/dir'
        # ex) file_name = 'output.csv'
        src_path = "s3a://{}{}".format(bucket, suffix)
        fs = file_sys.get(uri.create(src_path), conf)
        dst_file = "s3a://{}{}/{}".format(bucket, suffix, file_name)
        file_util.copyMerge(fs, path(src_path), fs, path(dst_file), False, conf, None)
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away