hdfs api java

package com.jsptpd;

import java.io.IOException;
import java.io.OutputStream;

//import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

public class HdfsUtil {
	private static final String HDFS_SIT="E:\\1\\capacity-scheduler.xml";
	private static final String HDFS_CORE="E:\\1\\hdfs-site.xml";
	private static final String MAP_SIT="E:\\1\\core-site.xml";
	private static final String YARN_SIT="E:\\1\\yarn-site.xml";
	private static final Configuration conf = new Configuration();
	
	
	public static void main(String[] args) throws IOException {
		System.setProperty("HADOOP_USER_NAME", "hdfs");
		conf.addResource(new Path(HDFS_SIT));
		conf.addResource(new Path(HDFS_CORE));
		conf.addResource(new Path(MAP_SIT));
		conf.addResource(new Path(YARN_SIT));
		// createDir();
		// rmr();
		// rename();
		//ls() ;
		//appendfile();
		// copyLocalRemote();
		//download();
		cat();
	}
	
	public static void createDir() throws IOException {
		FileSystem  fs = FileSystem.get(conf);
		fs.mkdirs(new Path("hdfs://jsptpd/wjj13413"));
		fs.close();
	}
	
	public static void rmr() throws IOException {
        
        FileSystem fs = FileSystem.get(conf);
        fs.deleteOnExit(new Path("hdfs://jsptpd/wjj13413"));
       
        fs.close();
    }
	
	public static void rename() throws IOException {
		FileSystem fs = FileSystem.get(conf);
		fs.rename(new Path("/wjj1314"), new Path("/wjj1212"));
		fs.close();
	}
	/**
	 * 列出该路径的文件信息
	 */
	public static void ls() throws IOException {
		FileSystem fs = FileSystem.get(conf);
		FileStatus[] filelist = fs.listStatus(new Path("/"));
		for (FileStatus f : filelist) {
            System.out.printf("name: %s, folder: %s, size: %d\n", f.getPath(), f.isDirectory(), f.getLen());
        }
		fs.close();
	}
	
	/***
	 * 创建文件,并写入数据
	 * @throws IOException 
	 */
	public static void createfile() throws IOException {
		FileSystem fs = FileSystem.get(conf);
		FSDataOutputStream os = fs.create(new Path("hdfs://jsptpd/wjj1212/1.txt"));
		String content="I hate this for myself 王骏骥\n";
		byte[] newcontent = content.getBytes();
		os.write(newcontent,0,newcontent.length);
		os.flush();
		os.close();
		fs.close();
	}
	
	/***
	 * hdfs 文件追加
	 */
	
	public static void appendfile() throws IOException {
		FileSystem fs = FileSystem.get(conf);
		FSDataOutputStream os = fs.append(new Path("hdfs://jsptpd/wjj1212/1.txt"));
		String content="I hate this for myself 王骏骥\n";
		byte[] newcontent = content.getBytes();
		os.write(newcontent);
		os.hsync();
		os.close();
		
	}
	
	/**
	 * 拷贝本地文件到远程服务器
	 * @throws IOException 
	 * @throws IllegalArgumentException 
	 */
	public static void copyLocalRemote() throws IllegalArgumentException, IOException {
		String local_file="D:\\workspace_scala1\\test1314\\src\\main\\java\\com\\jsptpd\\test1314\\mapreducetest.java";
		String remote_file="hdfs://jsptpd/wjj1212/mapreducetest.java";
		FileSystem fs = FileSystem.get(conf);
		fs.copyFromLocalFile(new Path(local_file), new Path(remote_file));
		fs.close();
	
	}
	
	public static void download() throws IOException {
		String local_file="D:\\mapreducetest.java";
		String remote_file="hdfs://jsptpd/wjj1212/mapreducetest.java";
		FileSystem fs = FileSystem.get(conf);
		fs.copyToLocalFile(new Path(remote_file), new Path(local_file));
		fs.close();
        
    }
	
	/***
	 * 查看hdfs文件内容
	 */
	public static void cat() throws IOException {
		
		String remote_file="hdfs://jsptpd/wjj1212/mapreducetest.java";
		FileSystem fs = FileSystem.get(conf);
		FSDataInputStream infs= fs.open(new Path(remote_file));
		
		IOUtils.copyBytes(infs, System.out,4096);
		infs.close();
		fs.close();
		
        
    }
	
	
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值