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 천"블 로그 에서 나 왔 습 니 다.전 재 를 사절 합 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기상청 API를 사용한 비오는 날만 알려주는 LINE Notify 작성지금까지 기상청의 기상 데이터는 스크래핑을 하는 것으로 밖에 얻을 수 없었습니다만, 1개월 정도 전에 기상청 HP가 API화했다(엄밀한 API가 아닌 것 같다)라고 하는 것으로 조속히 사용해 가려고 생각합니다. 이번...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.