<dependencies><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.8.5</version><scope>provided</scope></dependency><dependency><groupId>com.qcloud.cos</groupId><artifactId>hadoop-cos</artifactId><version>xxx</version></dependency><dependency><groupId>com.qcloud</groupId><artifactId>cos_api-bundle</artifactId><version>xxx</version></dependency></dependencies>
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/436/6884?from_cn_redirect=1#.E4.B8.8B.E8.BD.BD.E4.B8.8E.E5.AE.89.E8.A3.85// 以下配置是必填项conf.set("fs.cosn.impl", "org.apache.hadoop.fs.CosFileSystem");conf.set("fs.AbstractFileSystem.cosn.impl", "org.apache.hadoop.fs.CosN");conf.set("fs.cosn.userinfo.secretId", "xxxxxx");conf.set("fs.cosn.userinfo.secretKey", "xxxxxx");conf.set("fs.cosn.bucket.region", "xxxxxx");conf.set("fs.cosn.tmp.dir", "/data/chdfs_tmp_cache");// 配置项可参考 https://www.tencentcloud.com/document/product/436/71550?from_cn_redirect=1// 通过 POSIX 访问方式必填配置项(推荐方式)conf.set("fs.cosn.trsf.fs.AbstractFileSystem.ofs.impl", "com.qcloud.chdfs.fs.CHDFSDelegateFSAdapter");conf.set("fs.cosn.trsf.fs.ofs.impl", "com.qcloud.chdfs.fs.CHDFSHadoopFileSystemAdapter");conf.set("fs.cosn.trsf.fs.ofs.tmp.cache.dir", "com.qcloud.chdfs.fs.CHDFSHadoopFileSystemAdapter");conf.set("fs.cosn.trsf.fs.ofs.impl", "com.qcloud.chdfs.fs.CHDFSHadoopFileSystemAdapter");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/436/6884?from_cn_redirect=1#.E4.B8.8B.E8.BD.BD.E4.B8.8E.E5.AE.89.E8.A3.85// 是否开启 CRC64 校验,默认不开启,此时无法使用 hadoop fs -checksum 命令获取文件的 CRC64 校验值conf.set("fs.cosn.crc64.checksum.enabled", "true");String cosHadoopFSUrl = "cosn://examplebucket-12500000000/";return FileSystem.get(URI.create(cosHadoopFSUrl), 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-CRC32Cprivate 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);}}
/usr/local/service/hadoop/share/hadoop/common/ 目录下,元数据加速桶依赖的 Jar 包通常在 /usr/local/service/hadoop/share/hadoop/common/lib/ 目录下。文档反馈