HDFS JAVA API简单测试
HDFS JAVA API简单测试
Endless2010 发表于4个月前
HDFS JAVA API简单测试
  • 发表于 4个月前
  • 阅读 7
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

创建目录

mkdirs相当于 mkdir -p, mkdirs在dir存在的情况下也是返回true

public static void mkdirs(FileSystem fs, String path) {
		try {
			Path dirPath = new Path(path);
			if (!fs.exists(dirPath)) {
				fs.mkdirs(dirPath);
				System.out.println("created dir:" + path);
			} else {
				System.out.println("dir:" + path + "already exists");
			}
		} catch (IOException e) {
			System.out.println("mkdir:" + path + " failed, msg="+ e.getMessage());
			e.printStackTrace();
		}
	}

创建新文件

文件的父目录会自动创建

	public static void createFile(FileSystem fs, String filePath, String content) {
		try {
			Path path = new Path(filePath);
			FSDataOutputStream out = fs.create(path);
			out.write(content.getBytes());
			System.out.println("create File:" + filePath + " successed");
			out.close();
		} catch (IOException e) {
			System.out.println("create File:" + filePath + " failed, msg="+ e.getMessage());
			e.printStackTrace();
		}
	}

文件重命名

to目录必须存在,否则rename会失败,文件不会移动到目的路径

	public static void mv(FileSystem fs, String from, String to) {
		try {
			fs.rename(new Path(from), new Path(to));
			System.out.println("mv from:" + from + " to " + to+ " successed");
		} catch (IOException e) {
			System.out.println("mv from:" + from + " to " + to+ " failed, msg="+ e.getMessage());
			e.printStackTrace();
		}
	}

删除文件或目录

	public static void delete(FileSystem fs, String filePath) {
		try {
			fs.delete(new Path(filePath), true);// true表示是否递归删除目录,如果是文件则无效
			System.out.println("delete file:" + filePath + " successed");
		} catch (IOException e) {
			System.out.println("delete file:" + filePath + " delete failed, msg="+ e.getMessage());
			e.printStackTrace();
		}
	}

上传本地文件

hdfsPath的目录会自动创建

public static void uploadFile(FileSystem fs, String localPath, String hdfsPath,boolean deleteLocal){
		if(!new File(localPath).exists()){
			System.out.println("localfile: "+localPath+" does not exist");
			return;
		}
		try {
			//deleteLocal为false表示不删除源文件,true为删除
			fs.copyFromLocalFile(deleteLocal, new Path(localPath), new Path(hdfsPath));
			System.out.println("uploadFile File:" + localPath + " to: "+hdfsPath+" successed");
		} catch (IOException e) {
			System.out.println("uploadFile File:" + localPath + " to: "+hdfsPath+" failed,msg="+e.getMessage());
			e.printStackTrace();
		}
	}

下载HDFS文件到本地

本地目录会自动创建


public static void downloadFiles(FileSystem fs, String hdfsPath, String localPath,boolean deleteSrc){
		try {
			if(!fs.exists(new Path(hdfsPath))){
				System.out.println("hdfsPath: "+hdfsPath+" does not exist");
				return;
			}
			fs.copyToLocalFile(deleteSrc, new Path(hdfsPath), new Path(localPath));
			System.out.println("downloadFiles File from:" + hdfsPath + " to: "+localPath+" successed");
		} catch (IOException e) {
			System.out.println("downloadFiles File from:" + hdfsPath + " to: "+localPath+" failed,msg="+e.getMessage());
			e.printStackTrace();
		}
	}

读取文件的内容

public static void printFile(FileSystem fs, String filePath) throws IOException{
		Path srcPath = new Path(filePath);
		InputStream in = null;
		try {
			in = fs.open(srcPath);
			IOUtils.copyBytes(in, System.out, 4096, false);
		} finally {
			IOUtils.closeStream(in);
		}

	}

读取目录信息


	public static void printStatus(FileSystem fs, String hdfsPath){
		try {
              FileStatus[] files = fs.listStatus(new Path(hdfsPath));
              for(FileStatus file: files)
            	  System.out.println(file.getPath().toString());
		} catch (IOException e) {
			System.out.println("printStatus :" + hdfsPath + " failed,msg="+e.getMessage());
			e.printStackTrace();
		}
		
	}

测试

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://localhost:9000");
		FileSystem fs = FileSystem.get(conf);
		mkdirs(fs, "/hadoop/test");
		createFile(fs, "/hadoop/test1/files/helloworld.txt", "helloscala");
		mkdirs(fs, "/hadoop/test3/files/");
		mv(fs, "/hadoop/test1/files/helloworld.txt","/hadoop/test3/files/hellohdfs.txt");
		delete(fs, "/helloworld");
		uploadFile(fs,"D:/hadoop-2.7.3/LICENSE.txt","/hadoop/files/license.txt",false);
		downloadFiles(fs,"/files","D:/hdfs/files",false);
		printStatus(fs,"/files");
		printFile(fs,"/hadoop/files/license.txt");
	}
共有 人打赏支持
粉丝 2
博文 25
码字总数 23027
×
Endless2010
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: