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();
}
}