[Hadoop]HDFS文件的相关操作(上传、新建、删除和重命名)

package com.hadooplearn.test;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
//学习HDFS的使用
public class HdfsLearn {
	//复制文件,从本地到HDFS
	public void copyFile(String src,String dst){
		Configuration conf = new Configuration();
		conf.set("mapred.job.tracker", "localhost:9001");  
        conf.set("fs.default.name", "hdfs://localhost:9000");
		FileSystem hdfs = null;
		try {
			hdfs = FileSystem.get(conf);
			Path srcPath = new Path(src);
			Path dstPath = new Path(dst);
			hdfs.copyFromLocalFile(srcPath, dstPath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if(hdfs != null){
				try {
					hdfs.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	//创建新文件
	public void createFile(String dst){
		Configuration conf = new Configuration();
		conf.set("mapred.job.tracker","localhost:9001");
		conf.set("fs.default.name","hdfs://localhost:9000");
		FileSystem fs = null;
		FSDataOutputStream out = null;
		try{
			String content = "hello hadoop";
			Path path = new Path(dst);
			fs = FileSystem.get(conf);
			out = fs.create(path);
			out.write(content.getBytes());
			out.flush();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			if(out != null){
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(fs != null){
				try {
					fs.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public void renameFile(String str,String dst){
		Configuration conf = new Configuration();
		conf.set("mapred.job.tracker","localhost:9001");
		conf.set("fs.default.name","hdfs://localhost:9000");
		FileSystem fs = null;
		try{
			fs = FileSystem.get(conf);
			Path srcPath = new Path(str);
			Path dstPath = new Path(dst);
			fs.rename(srcPath, dstPath);
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			if(fs != null){
				try {
					fs.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public void deleteFile(String src){
		Configuration conf = new Configuration();
		conf.set("mapred.job.tracker","localhost:9001");
		conf.set("fs.default.name","hdfs://localhost:9000");
		FileSystem fs = null;
		try{
			fs = FileSystem.get(conf);
			fs.delete(new Path(src));
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			if(fs != null){
				try {
					fs.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void main(String[] args){
		System.setProperty("HADOOP_USER_NAME", "pijing");
		String src = "/home/pijing/input/test1";
		String dst = "/";
		HdfsLearn learn = new HdfsLearn();
//		learn.copyFile(src, dst);
//		System.out.println("copy file from local file system to HDFS");
//		learn.createFile("/test2");
//		System.out.println("create file test2");
//		String org = "/test2";
//		String lasted = "/test3";
//		learn.renameFile(org, lasted);
//		System.out.println("rename file /test2 to /test3");
		learn.deleteFile("/test3");
		System.out.println("delete file /test3");
	}
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值