使用Scala的HDFS读取数据

问题描述 投票:5回答:1

我是新来斯卡拉。我怎样才能读取使用斯卡拉(不使用星火)从HDFS文件?当我GOOGLE了它,我只发现写选项HDFS。

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.PrintWriter;

/**
* @author ${user.name}
*/
object App {

//def foo(x : Array[String]) = x.foldLeft("")((a,b) => a + b)

def main(args : Array[String]) {
println( "Trying to write to HDFS..." )
val conf = new Configuration()
//conf.set("fs.defaultFS", "hdfs://quickstart.cloudera:8020")
conf.set("fs.defaultFS", "hdfs://192.168.30.147:8020")
val fs= FileSystem.get(conf)
val output = fs.create(new Path("/tmp/mySample.txt"))
val writer = new PrintWriter(output)
try {
    writer.write("this is a test") 
    writer.write("\n")
}
finally {
    writer.close()
    println("Closed!")
}
println("Done!")
}

}

请帮助me.How可以使用Scala的从HDFS读取文件或加载文件。

scala hdfs
1个回答
17
投票

其中一个方法(还挺函数式)可能是这样的:

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))

你也可以看看this articleherehere,这些问题看起来关系到你的,包含工作(但更多的Java类)的代码示例,如果你有兴趣。

© www.soinside.com 2019 - 2024. All rights reserved.