tencent cloud

Cloud Object Storage

동향 및 공지
릴리스 노트
제품 공지
제품 소개
제품 개요
기능 개요
적용 시나리오
제품 장점
기본 개념
리전 및 액세스 도메인
규격 및 제한
제품 요금
과금 개요
과금 방식
과금 항목
프리 티어
과금 예시
청구서 보기 및 다운로드
연체 안내
FAQ
빠른 시작
콘솔 시작하기
COSBrowser 시작하기
사용자 가이드
요청 생성
버킷
객체
데이터 관리
일괄 프로세스
글로벌 가속
모니터링 및 알람
운영 센터
데이터 처리
스마트 툴 박스 사용 가이드
데이터 워크플로
애플리케이션 통합
툴 가이드
툴 개요
환경 설치 및 설정
COSBrowser 툴
COSCLI 툴
COSCMD 툴
COS Migration 툴
FTP Server 툴
Hadoop 툴
COSDistCp 툴
HDFS TO COS 툴
온라인 도구 (Onrain Dogu)
자가 진단 도구
실습 튜토리얼
개요
액세스 제어 및 권한 관리
성능 최적화
AWS S3 SDK를 사용하여 COS에 액세스하기
데이터 재해 복구 백업
도메인 관리 사례
이미지 처리 사례
COS 오디오/비디오 플레이어 사례
데이터 다이렉트 업로드
데이터 보안
데이터 검증
빅 데이터 사례
COS 비용 최적화 솔루션
3rd party 애플리케이션에서 COS 사용
마이그레이션 가이드
로컬 데이터 COS로 마이그레이션
타사 클라우드 스토리지 데이터를 COS로 마이그레이션
URL이 소스 주소인 데이터를 COS로 마이그레이션
COS 간 데이터 마이그레이션
Hadoop 파일 시스템과 COS 간 데이터 마이그레이션
데이터 레이크 스토리지
클라우드 네이티브 데이터 레이크
메타데이터 가속
데이터 레이크 가속기 GooseFS
데이터 처리
데이터 처리 개요
이미지 처리
미디어 처리
콘텐츠 조정
파일 처리
문서 미리보기
장애 처리
RequestId 가져오기
공용 네트워크로 COS에 파일 업로드 시 속도가 느린 문제
COS 액세스 시 403 에러 코드 반환
리소스 액세스 오류
POST Object 자주 발생하는 오류
보안 및 컴플라이언스
데이터 재해 복구
데이터 보안
액세스 관리
자주 묻는 질문
인기 질문
일반 문제
과금
도메인 규정 준수 문제
버킷 설정 문제
도메인 및 CDN 문제
파일 작업 문제
로그 모니터링 문제
권한 관리
데이터 처리 문제
데이터 보안 문제
사전 서명 URL 관련 문제
SDK FAQ
툴 관련 문제
API 관련 문제
Agreements
Service Level Agreement
개인 정보 보호 정책
데이터 처리 및 보안 계약
연락처
용어집
문서Cloud Object Storage데이터 레이크 스토리지메타데이터 가속데이터 액세스Hadoop FileSystem API 코드를 사용하여 COS 메타데이터 가속 버킷에 액세스하기

Hadoop FileSystem API 코드를 사용하여 COS 메타데이터 가속 버킷에 액세스하기

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 2024-12-26 21:36:29

작업 시나리오

COS(Cloud Object Storage) 버킷에 대해 메타데이터 가속화가 활성화된 경우 Java 코드를 사용하여 Hadoop 명령 라인 및 빅 데이터 컴포넌트를 사용하는 것 외에도 Hadoop Filesystem API를 통해 버킷에 액세스할 수 있습니다. 본문에서는 그 방법을 설명합니다.

전제 조건

메타데이터 가속이 활성화되었고 환경 배포 및 HDFS 프로토콜 구성이 올바르게 수행되었는지 확인하십시오. 자세한 내용은 HDFS 프로토콜을 사용하여 메타데이터 가속이 활성화된 버킷에 액세스를 참고하십시오.
Hadoop 환경이 있는 경우 Hadoop 명령 라인에 올바르게 액세스할 수 있는지 확인할 수 있습니다.

작업 단계

1. maven 프로젝트를 생성하고 maven의 pom.xml에 다음 종속 항목(실제 Hadoop 버전 및 환경에 따라 hadoop-common 패키지 버전 설정)을 추가합니다.
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.8.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
2. 다음 hadoop 코드를 참고하여 변경합니다. 구성 항목은 CHDFS 마운트에 설명된 대로 수정할 수 있습니다. 데이터 지속성 및 가시성 관련 지침에 특히 주의하십시오. 일부 자주 사용되는 파일 시스템 작업 인터페이스는 다음과 같습니다. 다른 API는 Hadoop FileSystem API를 참고하십시오.
package com.qcloud.cos.demo;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileChecksum;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;

public class Demo {
private static FileSystem initFS() throws IOException {
Configuration conf = new Configuration();
// 구성 항목 참고: https://www.tencentcloud.com/document/product/1106/41965
// 다음 구성은 필수 항목입니다

conf.set("fs.cosn.trsf.fs.ofs.impl", "com.qcloud.chdfs.fs.CHDFSHadoopFileSystemAdapter");
conf.set("fs.cosn.trsf.fs.AbstractFileSystem.ofs.impl", "com.qcloud.chdfs.fs.CHDFSDelegateFSAdapter");
conf.set("fs.cosn.trsf.fs.ofs.tmp.cache.dir", "/data/chdfs_tmp_cache");
// appid를 실제 appid로 교체
conf.set("fs.cosn.trsf.fs.ofs.user.appid", "1250000000");
// region을 실제 리전으로 교체
conf.set("fs.cosn.trsf.fs.ofs.bucket.region", "ap-beijing")
// 기타 옵션 설정 항목 참고: https://www.tencentcloud.com/document/product/1106/41965

String chdfsUrl = "cosn://examplebucket-12500000000/";
return FileSystem.get(URI.create(chdfsUrl), conf);
}

private static void mkdir(FileSystem fs, Path filePath) throws IOException {
fs.mkdirs(filePath);
}

private static void createFile(FileSystem fs, Path filePath) throws IOException {
// 파일 생성 (이미 있는 경우 덮어쓰기)
// if the parent dir does not exist, fs will create it!
FSDataOutputStream out = fs.create(filePath, true);
try {
// 파일 입력
String content = "test write file";
out.write(content.getBytes());
} finally {
// close가 성공적으로 반환되면 데이터 쓰기가 성공한 것이며, 예외가 발생하면 데이터 쓰기가 실패한 것입니다
out.close();
}
}

private static void readFile(FileSystem fs, Path filePath) throws IOException {
FSDataInputStream in = fs.open(filePath);
try {
byte[] buf = new byte[4096];
int readLen = -1;
do {
readLen = in.read(buf);
} while (readLen >= 0);
} finally {
IOUtils.closeQuietly(in);
}
}


private static void queryFileOrDirStatus(FileSystem fs, Path path) throws IOException {
FileStatus fileStatus = fs.getFileStatus(path);
if (fileStatus.isDirectory()) {
System.out.printf("path %s is dir\\n", path);
return;
}

long fileLen = fileStatus.getLen();
long accessTime = fileStatus.getAccessTime();
long modifyTime = fileStatus.getModificationTime();
String owner = fileStatus.getOwner();
String group = fileStatus.getGroup();


System.out.printf("path %s is file, fileLen: %d, accessTime: %d, modifyTime: %d, owner: %s, group: %s\\n",
path, fileLen, accessTime, modifyTime, owner, group);
}


// 기본 인증 유형은 COMPOSITE-CRC32C
private static void getFileCheckSum(FileSystem fs, Path path) throws IOException {
FileChecksum checksum = fs.getFileChecksum(path);
System.out.printf("path %s, checkSumType: %s, checkSumCrcVal: %d\\n",
path, checksum.getAlgorithmName(), ByteBuffer.wrap(checksum.getBytes()).getInt());
}


private static void copyFileFromLocal(FileSystem fs, Path chdfsPath, Path localPath) throws IOException {
fs.copyFromLocalFile(localPath, chdfsPath);
}


private static void copyFileToLocal(FileSystem fs, Path chdfsPath, Path localPath) throws IOException {
fs.copyToLocalFile(chdfsPath, localPath);
}


private static void renamePath(FileSystem fs, Path oldPath, Path newPath) throws IOException {
fs.rename(oldPath, newPath);
}


private static void listDirPath(FileSystem fs, Path dirPath) throws IOException {
FileStatus[] dirMemberArray = fs.listStatus(dirPath);


for (FileStatus dirMember : dirMemberArray) {
System.out.printf("dirMember path %s, fileLen: %d\\n", dirMember.getPath(), dirMember.getLen());
}
}


// 재귀 삭제 마크는 디렉터리 삭제에 사용됩니다
// 재귀가 false 이고 dir이 비어있지 않으면 작업이 수행되지 않습니다
private static void deleteFileOrDir(FileSystem fs, Path path, boolean recursive) throws IOException {
fs.delete(path, recursive);
}


private static void closeFileSystem(FileSystem fs) throws IOException {
fs.close();
}


public static void main(String[] args) throws IOException {
// 파일 시스템 초기화
FileSystem fs = initFS();


// 파일 생성
Path chdfsFilePath = new Path("/folder/exampleobject.txt");
createFile(fs, chdfsFilePath);


// 파일 불러오기
readFile(fs, chdfsFilePath);


// 파일 또는 디렉터리 조회
queryFileOrDirStatus(fs, chdfsFilePath);


// 파일 검사합 가져오기
getFileCheckSum(fs, chdfsFilePath);


// 로컬에서 파일 복사
Path localFilePath = new Path("file:///home/hadoop/cosn_demo/data/exampleobject.txt");
copyFileFromLocal(fs, chdfsFilePath, localFilePath);


// 파일을 로컬로 가져오기
Path localDownFilePath = new Path("file:///home/hadoop/cosn_demo/data/exampleobject.txt");
copyFileToLocal(fs, chdfsFilePath, localDownFilePath);


// 이름 변경
Path newPath = new Path("/doc/example.txt");
renamePath(fs, chdfsFilePath, newPath);


// 파일 삭제
deleteFileOrDir(fs, newPath, false);


// 디렉터리 생성
Path dirPath = new Path("/folder");
mkdir(fs, dirPath);


// 디렉터리에 파일 생성
Path subFilePath = new Path("/folder/exampleobject.txt");
createFile(fs, subFilePath);


// 디렉터리 나열
listDirPath(fs, dirPath);


// 디렉터리 삭제
deleteFileOrDir(fs, dirPath, true);


// 파일 시스템 비활성화
closeFileSystem(fs);
}
}
3. 프로젝트를 컴파일하고 실행합니다.
설명:
코드를 실행하기 전에 메타데이터 가속 버킷에 종속된 Hadoop common 패키지 및 Jar 패키지의 경로를 포함해야 하는 classpath를 올바르게 설정해야 합니다.
EMR 환경의 경우 HDFS 프로토콜을 사용하여 메타데이터 가속이 활성화된 버킷에 액세스에 설명된 단계를 따르면 Hadoop common 패키지는 일반적으로 /usr/local/service/hadoop/share/hadoop/common/ 디렉터리에 있으며, 메타데이터 가속 버킷에 의존하는 Jar 패키지는 일반적으로 /usr/local/service/hadoop/share/hadoop/common/lib/ 디렉터리에 있습니다.

도움말 및 지원

문제 해결에 도움이 되었나요?

피드백