用 Hadoop Java Api 针对 HDFS 进行文件上传、创建、重命名、删除等操作

一、项目要加入的一些包:

二、下面是代码相关的:

package org.apache.hadoop.examples.yao;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class FileDemo {

	public static void main(String[] args)  throws IOException{
		mkdir("test");
		putFile("/home/yaokj/temp.txt","test/test.txt");
		createFile("test/create.txt");
		rename("test/test.txt","test/test2.txt");
		System.out.println(deleteFile("test/test2.txt"));
		System.out.println(getLastModifyTime("test/create.txt"));
		System.out.println(isExists("test/create.txt"));
		System.out.println(isExists("test/test2.txt"));
	}
	
	public static boolean isExists(String path) throws IOException{
		FileSystem fs = getFileSystem() ; 
		return fs.exists(new Path(path));
	}
	public static long getLastModifyTime(String path) throws IOException{
		FileSystem fs = getFileSystem() ; 
		FileStatus file = fs.getFileStatus(new Path(path));
		return file.getModificationTime();
	}
	public static boolean deleteFile(String path) throws IOException{
		FileSystem fs = getFileSystem() ; 
		return fs.delete(new Path(path),false);//false 为是否递归删除
	}
	
	public static void rename( String fromFile,String toFile) throws IOException{
		FileSystem fs = getFileSystem() ; 
		fs.rename(new Path(fromFile), new Path(toFile));
		fs.close();
	}
	
	public static void createFile(String file)  throws IOException{
		FileSystem fs = getFileSystem() ;
		FSDataOutputStream fsd = fs.create(new Path(file));
		byte[] witeByte = "Hello world , you know".getBytes();
		fsd.write(witeByte, 0, witeByte.length);
		
		fsd.close();
		fs.close();
	}
	
	public static void putFile(String srcPath ,String dstPath) throws IOException{
		FileSystem fs = getFileSystem() ;
		
		Path src = new Path( srcPath);
		Path dst = new Path(dstPath);
		
		fs.copyFromLocalFile(src, dst);
		
		FileStatus[] fileStatus = fs.listStatus(dst);
		
		for(FileStatus status : fileStatus){
			System.out.println(status.getPath());
		}
		
		fs.close();
	}
	
	public static void mkdir(String dst) throws IOException{
		if(dst != null && !"".equals(dst)){
			FileSystem fs = getFileSystem();
			fs.mkdirs(new Path(dst));
			fs.close();
		}
		
	}
	
	public static FileSystem getFileSystem() throws IOException{
		Configuration cfg = new Configuration();
	    cfg.addResource(new Path("/home/yaokj/hadoop-0.20.203.0/conf/hdfs-site.xml"));
		cfg.addResource(new Path("/home/yaokj/hadoop-0.20.203.0/conf/core-site.xml"));
		FileSystem fs = FileSystem.get(cfg);
		return fs;
	}

}



转载于:https://my.oschina.net/jamaly/blog/323279

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值