package com.wwei.had241.demo.hdfs;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class HdfsDemo {
private static FileSystem system = null;
private static int buffSize = 4096;
private static String loginUser = "root";
static{
Configuration config = new Configuration();
URI uri;
try {
uri = new URI("hdfs://hadoop07:9000");
system = FileSystem.get(uri, config,loginUser);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public long startPro(){
System.out.println("start process ......");
return System.currentTimeMillis();
}
public void endPro(long startTime){
System.out.println("end process!spend:" + (System.currentTimeMillis()-startTime)+"ms");
}
/**
* 创建文件目录
* @throws IOException
**/
public void mkdir() throws IOException{
long startTime = startPro();
Path path = new Path("/d4");
boolean bool = system.mkdirs(path);
System.out.println("mkdir result " + bool);
endPro(startTime);
}
/**
* 上传
* */
public void upload() throws IOException{
long startTime = startPro();
Path path = new Path("/word.txt");
FSDataOutputStream out = system.create(path);
InputStream in = new FileInputStream("D:\\word.txt");
IOUtils.copyBytes(in, out, buffSize);
endPro(startTime);
}
/**
* 获取文件
* */
public void getFile() throws IOException{
long startTime = startPro();
Path path = new Path("/test1.txt");
InputStream in = system.open(path);
// OutputStream out = new FileOutputStream("d:\\hello.txt");
OutputStream out = System.out;
IOUtils.copyBytes(in, out, buffSize);
endPro(startTime);
}
public void delect() throws IOException{
long startTime = startPro();
Path path = new Path("/d4");
boolean bool = system.deleteOnExit(path);
System.out.println("delete file result:"+ bool);
endPro(startTime);
}
public static void main(String[] args) {
HdfsDemo demo = new HdfsDemo();
try {
// demo.getFile();
demo.upload();
// demo.delect();
// demo.mkdir();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}