Skip to content

Instantly share code, notes, and snippets.

@mebubo
Created September 14, 2018 05:59
Show Gist options
  • Select an option

  • Save mebubo/867bfed32d96db6e005b04f3d4e003bb to your computer and use it in GitHub Desktop.

Select an option

Save mebubo/867bfed32d96db6e005b04f3d4e003bb to your computer and use it in GitHub Desktop.
package flat
import java.io.{BufferedReader, InputStreamReader}
import java.util.zip.{ZipEntry, ZipInputStream}
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.input.PortableDataStream
import org.apache.spark.rdd.RDD
object Main {
def main(args: Array[String]): Unit = {
val sc: SparkContext = new SparkContext(new SparkConf().setMaster("local[1]").setAppName("main"))
val strings = z(sc, "/tmp/zips")
strings.foreach(println)
}
def z(sc: SparkContext, path: String): RDD[String] = {
sc.binaryFiles(path).flatMap { case (name: String, content: PortableDataStream) =>
val zis: ZipInputStream = new ZipInputStream(content.open)
val stream: Stream[ZipEntry] = Stream.continually(zis.getNextEntry).takeWhile(_ != null)
val brs: Stream[BufferedReader] = stream.map(_ => new BufferedReader(new InputStreamReader(zis)))
val lines: Stream[String] = brs.flatMap(br => Stream.continually(br.readLine()).takeWhile(_ != null).toList)
lines
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment