2017-01-11 9 views
2

저는 scala에 익숙하지 않습니다. 어떻게 spark를 사용하지 않고 scala를 사용하여 HDFS에서 파일을 읽을 수 있습니까? 내가봤을 때 나는 HDFS에 쓰기 옵션 만 쓰고있다.scala를 사용하여 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!") 
} 

} 

도와주세요. 파일을 읽거나 scala를 사용하여 HDFS에서 파일을로드하는 방법은 무엇입니까?

(좀 기능 스타일) 방식의
+0

지금까지 무엇을 시도 했습니까? https://hadoop.apache.org/docs/current/api/org/apache/hadoop/fs/FileSystem.html? – Reactormonk

+0

여기서는 문서를 따르기가 어렵습니다. – thebluephantom

+0

우리는 작은 파일을 HDFS에서 로컬 파일 시스템으로 복사하고 거기에서 순차적으로 처리하도록 선택했습니다. – thebluephantom

답변

7

하나는 다음과 같이 수 :

또한
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 article 또는 herehere이,이 질문은 당신에 관한 봐 살펴보고 작업 포함 (하지만 더 할 수 Java와 같은) 코드 예제를 사용할 수 있습니다.

관련 문제