Read the data from HDFS using Scala

前端 未结 1 1062
逝去的感伤
逝去的感伤 2020-12-30 10:07

I am new to Scala. How can I read a file from HDFS using Scala (not using Spark)? When I googled it I only found writing option to HDFS.

import org.apache.ha         


        
相关标签:
1条回答
  • 2020-12-30 11:02

    One of the ways (kinda in functional style) could be like this:

    val hdfs = FileSystem.get(new URI("hdfs://yourUrl:port/"), new Configuration()) 
    val path = new Path("/path/to/file/")
    val stream = hdfs.open(path)
    def readLines = Stream.cons(stream.readLine, Stream.continually( stream.readLine))
    
    //This example checks line for null and prints every existing line consequentally
    readLines.takeWhile(_ != null).foreach(line => println(line))
    

    Also you could take a look this article or here and here, these questions look related to yours and contain working (but more Java-like) code examples if you're interested.

    0 讨论(0)
提交回复
热议问题