CHDFS 생성
권한 그룹 생성
권한 규칙 생성
마운트 포인트 생성
CHDFS 마운트
CAM 라이선스로 액세스
Java 코드로 CHDFS 액세스
파일 시스템 삭제
<dependencies><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.8.5</version><scope>provided</scope></dependency></dependencies>
package com.qcloud.chdfs.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();// CHDFS 구성 항목은 https://www.tencentcloud.com/document/product/1105/36368?from_cn_redirect=1을 참고하십시오.// 다음 설정은 필수 항목입니다.conf.set("fs.ofs.impl", "com.qcloud.chdfs.fs.CHDFSHadoopFileSystemAdapter");conf.set("fs.AbstractFileSystem.ofs.impl", "com.qcloud.chdfs.fs.CHDFSDelegateFSAdapter");conf.set("fs.ofs.tmp.cache.dir", "/data/chdfs_tmp_cache");conf.set("fs.ofs.user.appid", "1250000000");// 기타 옵션 설정 항목은 https://www.tencentcloud.com/document/product/1105/36368?from_cn_redirect=1을 참고하십시오.String chdfsUrl = "ofs://f4maaabbb-ccdd.chdfs.ap-guangzhou.myqcloud.com/";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 {IOUtils.closeQuietly(out);}}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/ofs_demo/data/exampleobject.txt");copyFileFromLocal(fs, chdfsFilePath, localFilePath);// 파일을 로컬로 가져오기Path localDownFilePath = new Path("file:///home/hadoop/ofs_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);}}
/usr/local/service/hadoop/share/hadoop/common/ 디렉터리에, CHDFS 패키지는/usr/local/service/hadoop/share/hadoop/common/lib/ 디렉터리에 위치합니다.피드백