hadoop2 客户端Java调用写法

package hdfs;

import java.net.URI;

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

public class App1 {

	public static void main(String[] args) throws Exception {
		//http://www.baidu.com
		final Configuration conf = new Configuration();
		final FileSystem fileSystem = FileSystem.get(new URI("hdfs://h2single511-115:9000/"), conf);
		System.out.println("**************"+fileSystem.toString());
		System.out.println("**************"+fileSystem.getClass());

		// ===================== 创建目录 1 目录绝对路径相对路径区别  2 创建目录带权限和不带权限写法 ====================
		/* 绝对路径下 在单用户下使用
		 * Path path = new Path("/mydir"); // 递归创建   /mydir1
		boolean result = fileSystem.mkdirs(path);
		System.out.println(result);*/
		
		/**
		 * 不带路径下 创建文件夹会在 /user/操作用户名/文件夹名称
		 * 在多用户环境下 建议使用 相对路径来写
		 * 
		
		Path path1 = new Path("mydir1"); // 递归创建 /user/root/mydir1
		boolean result1 = fileSystem.mkdirs(path1);
		System.out.println(result1);
		 */
		// d--x--x--x   - root supergroup          0 2015-01-10 03:50 /mydir2
		//fileSystem.mkdirs(new Path("/mydir2"), new FsPermission("111"));  // 指定创建此目录的权限
		 
		
		// ===================== 上传文件到hdfs 分为这两种写法  1 普通上传   2 上传过程中不断展示上传进度  ====================
		// 1024000表示数据流向hadoop集群写入缓冲 单位都是字节
		/*FSDataOutputStream out = fileSystem.create(new Path("mydir1/input"), true, 1024000, (short)2, 1048576);
		FileInputStream in = new FileInputStream("/etc/profile");
		IOUtils.copyBytes(in, out, 1024,true); // 1024表示 linux文件写到hdfs接收流的缓冲
*/		// 结果-rw-r--r--   2 root supergroup       1629 2015-01-10 04:13 /user/root/mydir1/input

//		final AtomicInteger writeBytes = new AtomicInteger(0);
//		final FSDataOutputStream out = fileSystem.create(new Path("/dir1/file2"), new Progressable() {
//			@Override
//			public void progress() {
//				System.out.println("WriteBytes = "+writeBytes.get());
//			}
//		});
//		
//		final FileInputStream in = new FileInputStream("/root/Downloads/hello");
//		byte[] buffer = new byte[4];
//		int readBytes = in.read(buffer);
//		while(readBytes!=-1) {
//			out.write(buffer);
//			out.flush();
//			out.hsync();
//			writeBytes.addAndGet(readBytes);
//			readBytes = in.read(buffer);
//		}
		
		// ===================== 下载文件  ====================
		/*FSDataInputStream  input = fileSystem.open(new Path("mydir1/input"));
		IOUtils.copyBytes(input, System.out, 1024);*/
		
		// ===================== 遍历文件 文件夹不会存在datanode上 仅仅是一个命名空间而已  只有文件才会存放起来  这里查看文件存放位置  ====================
		/*FileStatus[]  liststatus = fileSystem.listStatus(new Path("/mydir/input"));
		for(FileStatus fileStatus : liststatus){
			System.out.println(fileStatus); 
			if(!fileStatus.isDirectory()){// 只有是非文件夹 才能保存到数据节点中 也才能打印出保存的datanode节点名称等
				BlockLocation[] locations = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
				for(BlockLocation location : locations){
					String[] hostNamess = location.getNames();
					for(String hostname : hostNamess){
						System.out.print("hostname: " + hostname); // hostname: 192.168.1.115:50010
					}
				}
			}
			
		}*/
		
		
		//遍历
//		final FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
//		for (FileStatus fileStatus : listStatus) {
//			System.out.println(fileStatus);
//		}
		
		// ===================== 删除  ====================
		fileSystem.delete(new Path("/user/root/"),true);
		
		
		//获取工作目录
		//fileSystem.getWorkingDirectory().toString();
	}
}

猜你喜欢

转载自chengjianxiaoxue.iteye.com/blog/2174695