如何在scala中使用并行文件处理?

如何在scala中使用并行文件处理?,scala,file,parallel-processing,Scala,File,Parallel Processing,嗨~我有200个文件,我想在scala中处理并行文件(读写) 所以,我尝试了几种方法 首先,我使用par方法来处理并行文件 这是我的密码 val path = new Path("[file directory path]") //Here is 200 text files. val fileSystem = FileSystem.get(new Configuration()) val status = fileSystem.listStatus(path) status.par.fo

嗨~我有200个文件,我想在scala中处理并行文件(读写)

所以,我尝试了几种方法

首先,我使用
par
方法来处理并行文件

这是我的密码

val path = new Path("[file directory path]")    //Here is 200 text files.
val fileSystem = FileSystem.get(new Configuration())
val status = fileSystem.listStatus(path)
status.par.foreach{x =>
val stream = fileSystem.open(x.getPath) //Each text file open.
def readLines = Stream.cons(stream.readLine, Stream.continually(stream.readLine))
readLines.takeWhile(_ != null).foreach{ line => 

        //I want to write several file in here using PrintWriter.

        val f = new File(outputName)
        if(f.exists() && !f.isDirectory()){
        out = new PrintWriter(new FileOutputStream(new File(outputName), true))
        out.append(line+"\n")
        out.close
        }else{
        out = new PrintWriter(outputName)
        out.println(line)                  
        out.close
        }
}
}
当我运行此代码时,出现了一个错误“打开的文件太多”

所以,我把ulimit-n1024改为65536。但它不起作用

其次,我使用了
Thread
方法

val path = new Path("[file directory path]")    //Here is 200 text files.
val fileSystem = FileSystem.get(new Configuration())
val status = fileSystem.listStatus(path)
    status.foreach{x =>
      val thread = new Thread{
        override def run{
            val stream = fileSystem.open(x.getPath) //Each text file open.
            def readLines = Stream.cons(stream.readLine, Stream.continually(stream.readLine))
            readLines.takeWhile(_ != null).foreach{ line => 

            //I want to write several file in here using PrintWriter.

            val f = new File(outputName)
            if(f.exists() && !f.isDirectory()){
            out = new PrintWriter(new FileOutputStream(new File(outputName), true))
            out.append(line+"\n")
            out.close
            }else{
            out = new PrintWriter(outputName)
            out.println(line)                  
            out.close
            }
         }
      }
      thread.start
      Thread.sleep(50)
    }
}
它还给了我一个错误
“$$anonfun$main$2$$anonfun$apply$mcVI$sp$3$$anonfun$apply$mcV$sp$2$$anon$1$$anonfun$run$2$$anonfun$apply$4.apply”

我的代码中有什么问题

事实上,我不知道该拿什么。我尝试了我所知道的一切


有什么想法吗?

您正在为200个文件中的每一行创建一个
printWriter
?它在没有
par
的情况下工作吗?最初它在没有
par
的情况下工作,但速度很慢。因此,我尝试使用
par
Thread
进行并行处理。我不确定。但我同意你的第一个答复。