반응형

1. Java 에서 HDFS 이용하기

 1) 다음을 import 해야 한다. 

import org.apache.hadoop.fs.Path

import org.apache.hadoop.fs.FileSystem

import org.apache.hadoop.fs.FSDataInputStream;

import org.apache.hadoop.fs.FSDataOutputStream;

 2) FileSystem 객체가 기본적으로 파일 처리를 위한 객체로 다음과 같이 참조를 얻는다.

 

FileSystem hdfs=FileSystem.get(Configuration객체);

 

 - Configuration객체는 Configuration conf=new Configuration(); 으로 생성하거나 MapReduce에서는 context.getConfiguration() 으로 가져올 수 있다. MapReduce에서는 다음과 같이 FileSystem을 세팅한다.

 

FileSystem hdfs=FileSystem.get(context.getConfiguration());

 

 3) 경로는 다음과 같이 생성한다.

 

Path path_name = new Path(경로명);

 

예를 들어 Path testPath = new Path("/test/test.txt"); 와 같이 이용 한다.

 

4) 경로의 파일이 존재하는지 확인, 삭제

 2)에서 생성한 FIleSystem 객체인 hdfs와 3)에서 생성한 경로를 이용하여 다음과 같이 확인 및 삭제가 가능하다.

 

if(hdfs.exists(path_name)) hdfs.delete(path_name,true);

 

5) Input/Output Stream 생성

 다음과 같이 FSDataInputStream과 FSDataOutputStream을 생성 가능하다.

 

FSDataOutputStream stream_out__name = hdfs.create(Path객체)

FSDataInputStream stream_in_name =hdfs.open(Path객체)

 

6) read/write

 FSDataOutputStream의 write 메소드, FSDataInputStream의 Read 메소드로 HDFS에 데이터를 Read/Write 할 수 있다.

 

https://hadoop.apache.org/docs/stable/api/index.html 의 API를 참조..


다음은 Streaming을 조작하는 예이다.


http://yaseminavcular.blogspot.kr/2011/06/ways-to-write-read-hdfs-files.html

 

 

 

 

반응형

+ Recent posts