/***
* 对hdfs文件系统的上传,下载,删除,创建文件夹的操作演示
*/
package itcast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
public class HDFSDemo {
FileSystem fs = null;
@Before
public void init() throws IOException, URISyntaxException, InterruptedException{
//首先创建FileSystem的实现类(工具类)
fs = FileSystem.get(new URI("hdfs://itcast01:9000"), new Configuration(), "root");
}
/**
*
* info:上传本地文到hdfs上
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void testUpload() throws IllegalArgumentException, IOException{
//读取本地文件系统的文件,返回输入流
InputStream in = new FileInputStream("E://Test.jar");
//在Hdfs上创建一个文件,返回输出流
OutputStream out = fs.create(new Path("/test.jar"));
//输入-》输出
IOUtils.copyBytes(in, out, 4096, true);
}
/**
* info:删除hdfs上文件
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void testDel() throws IllegalArgumentException, IOException{
fs.delete(new Path("/Demo"), false);
}
/***
* info:在hdfs上创建文件夹
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void testMkdir() throws IllegalArgumentException, IOException{
fs.mkdirs(new Path("/itcast0626/"));
}
public static void main(String[] args) throws IOException, URISyntaxException {
// TODO Auto-generated method stub
FileSystem fs = FileSystem.get(new URI("hdfs://itcast01:9000"), new Configuration());
InputStream in = fs.open(new Path("/Demo"));
OutputStream out = new FileOutputStream("E://Demo");
IOUtils.copyBytes(in, out, 4096, true);
}
}