HDFS API 사용 정리(개인 인증)

5352 단어 apihdfs
HDFS API 사용 코드 정리
자바 추상 클래스 org.apache.hadop.fs.FileSystem 은 hadop 의 파일 시스템 인 터 페 이 스 를 정의 합 니 다.이 종 류 는 추상 적 인 유형 으로 다음 과 같은 두 가지 정적 공장 방법 을 통 해 FileSystem 인 스 턴 스 를 얻 을 수 있 습 니 다.
public static FileSystem.get(Configuration conf) throws IOException public static FileSystem.get(URI uri, Configuration conf) throws IOException
eg:
String filePath = "hdfs://ip:port/recycle/word.pdf”; //ip namenode  ,port hdfs   
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(filePath), conf);

String filePath ="hdfs://ip:port/recycle/word.pdf”; //ip 는 namenode 주소 이 고 port 는 hdfs 포트 번호 입 니 다.
Configurationconf = new Configuration();
FileSystem fs =FileSystem.get(URI.create(filePath), conf);
다음은 파일 시스템 fs 인 스 턴 스 에 대한 접근 및 작업 입 니 다.
1.새 파일 디 렉 터 리:public boolean mkdirs(Path f)throws IOException
eg:
String path = "hdfs://ip:port/”;
Configurationconf = new Configuration();
FileSystem fs =FileSystem.get(URI.create(path), conf);
boolean flag=fs.mkdirs(new Path (path+”   ”));

String path = "hdfs://ip:port/”;
Configurationconf = new Configuration();
FileSystem fs =FileSystem.get(URI.create(path), conf);
boolean flag=fs.mkdirs(new Path(path+"디 렉 터 리 이름");
2.데이터 쓰기(HDFS 에 파일 업로드):public FSOutputStream create(Path f)throws IOException
eg:
File file = new File("d:\\    \\ss.txt");
String path = "hdfs://ip:port/ss.txt”;
InputStream in = newBufferedInputStream(new FileInputStream(file)); //    
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(path),conf);//URI.create(hdfs_path)
OutputStream out = fs.create(new Path(path), new Progressable() {
@Override
publicvoid progress() {
                                System.out.print("*");
                    }
});
IOUtils.copyBytes(in, out,4096, true);

파일 파일=new File("d:\\\파일 업로드\\ss.txt");
String path = "hdfs://ip:port/ss.txt”;
InputStream in = newBufferedInputStream(new FileInputStream(file)); //캐 시 파일
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(path),conf);//URI.create(hdfs_path)
OutputStream out = fs.create(new Path(path), new Progressable() {
@Override
publicvoid progress() {
                               System.out.print("*");
                   }
});
IOUtils.copyBytes(in, out,4096, true);
3.데이터 읽 기(HDFS 에서 파일 다운로드):
public FSDataInputStreamopen(Path f) throws IOException
eg:
String down_path = “hdfs://ip:port/ss.txt”;
Configuration conf = newConfiguration();
URI uri = URI.create(down_path);
FileSystem fs = FileSystem.get(uri,conf);
FSDataInputStream hdfsInStream =fs.open(new Path(down_path));
  HDFS   hdfsInStream

String down_path = “hdfs://ip:port/ss.txt”;
Configuration conf = newConfiguration();
URI uri = URI.create(down_path);
FileSystem fs = FileSystem.get(uri,conf);
FSDataInputStream hdfsInStream =fs.open(new Path(down_path));
HDFS 파일 흐름 hdfsInStream 가 져 오기
4.파일/디 렉 터 리 존재 여부 확인:public boolean exists(Path f)throws IOException
5.파일 삭제:public boolean delete(Path f,Boolean recursive)
지정 한 파일 이나 디 렉 터 리 를 영구적 으로 삭제 합 니 다.f 가 빈 디 렉 터 리 나 파일 이 라면 recursive 의 값 은 무 시 됩 니 다.recursive=true 일 때 만 비 어 있 는 디 렉 터 리 와 내용 이 삭 제 됩 니 다.
주:대응 하 는 셸 명령 의 rm 은 임시 삭 제 됩 니 다.hadop 휴지통 Trash 가 설정 되 어 있 으 면 rm 이 삭제 한 파일 은/user/root/.Trash/current/디 렉 터 리 에서 설정 한 시간 내 에 완전히 삭제 되 고 복 구 를 선택 할 수 있 습 니 다.
6.로 컬 시스템 에서 HDFS 파일 시스템 으로 복사:Public boolean copy FromLocal(Path src,Path dst)throws IOException
7.파일 상태 정보 조회:
FileStatus 클래스 는 파일 시스템 의 파일 과 디 렉 터 리 의 메타 데 이 터 를 봉 인 했 습 니 다.파일 길이,블록 크기,백업,수정 시간,소유자 와 권한 정 보 를 포함 합 니 다.
"FileStatus.getPath()"를 통 해 지정 한 HDFS 의 한 디 렉 터 리 에 있 는 모든 파일 을 볼 수 있 습 니 다.
eg:
String dst = "hdfs://ip:port/"+"user";//user        
Configuration conf = new Configuration(); 
FileSystem fs = FileSystem.get(URI.create(dst), conf);
FileStatus fileList[] = null;
fileList = fs.listStatus(new Path(dst)); //    
int size = fileList.length;//         
for(int i = 0; i < size; i++){ 
System.out.println(fileList[i].getPath().getName());//   
System.out.println(fileList[i].getLen());//    
System.out.println(fileList[i].getModificationTime());//            
}

String dst = "hdfs://ip:port/"+"user";//user 디 렉 터 리 의 모든 파일
Configuration conf = new Configuration();  
FileSystem fs = FileSystem.get(URI.create(dst), conf);
FileStatus fileList[] = null;
fileList = fs.listStatus(new Path(dst)); //파일 경로
int size = fileList.length;//디 렉 터 리 아래 모든 파일 수
for(int i = 0; i < size; i++){  
System.out.println(fileList[i].getPath().getName());//파일 이름
System.out.println(fileList[i].getLen());//파일 크기
System.out.println(fileList[i].getModificationTime());//파일 내용 마지막 수정 시간
}  
8.파일 추가:public FSDataOutputStream append(Path f)throws IOException
9.파일 이름 바 꾸 기:public boolean 이름 바 꾸 기(Patharg 0,Patharg 1)throws IOException
HDFS  API 에 파일 을 이동 하 는 인터페이스 가 없습니다.이름 을 바 꾸 면 이 기능 을 수행 할 수 있 습 니 다.
셸 명령 에 대응 하 는:mv
이 글 은"carrie 천"블 로그 에서 나 왔 습 니 다.전 재 를 사절 합 니 다!

좋은 웹페이지 즐겨찾기