[Hadoop] hdfs file read - java main 실행.

ITWeb/Hadoop일반 2014. 7. 21. 12:14

hdfs 에 저장된 파일을 읽어서 처리 하는 java main 프로그램 작성 시 hadoop 관련 라이브러리에 대한 classpath 지정을 하지 않게 되면 아래와 같은 에러가 발생 할 수 있으니 참고하세요.


[Error]

 java.io.IOException: No FileSystem for scheme: hdfs


[예제코드]

Configuration conf = new Configuration();

// conf.set("fs.default.name", hdfsServer); // deprecated

conf.set("fs.defaultFS", hdfsServer);


FileSystem hdfs = FileSystem.get(conf);

BufferedReader br = new BufferedReader(new InputStreamReader(hdfs.open(new Path(schemaFile))));


while ((line = br.readLine()) != null) {

....

}


br.close();


eclipse 에서 Runnable JAR file 로 export 하셔서 실행 하시면 됩니다.

여기서 Library handling 은 

"Copy required libraries into a sub-folder next to the generated JAR"

를 선택 하세요.


이렇게 해서 생성된 파일을 실행 하실 때 -classpath  옵션으로 추가해 주시고 실행 하시면 됩니다.


[실행 예제]

java -classpath /Users/hwjeong/Documents/workspace/eclipse-j2ee/PROJECT/target/standalone_lib -jar /Users/hwjeong/Documents/workspace/eclipse-j2ee/PROJECT/target/standalone.jar StandaloneMain hdfs://localhost:9000


※ 단순히 hdfs 에 있는 파일을 읽을 떄는 eclipse 에서 hadoo-hdfs 를 추가해서 작성해 주시면 됩니다.

<dependency>

    <groupId>org.apache.hadoop</groupId>

    <artifactId>hadoop-hdfs</artifactId>

    <version>2.3.0</version>

</dependency>


: