hdfs javaapi

来源:互联网 发布:mysql如何使用 编辑:程序博客网 时间:2024/05/21 22:53
package hdfsapi;import java.io.FileInputStream;import java.io.FileOutputStream;import java.net.URI;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FSDataOutputStream;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 TestHdfsFile {FileSystem fs = null;@Beforepublic void getFs() throws Exception{//1.获取一个FileSystem对象fs = FileSystem.get(new URI("hdfs://nameservice1:8020"),new Configuration(),"hdfs");}//测试下载@Testpublic void testDownload() throws Exception{//2.通过文件系统下载文件FSDataInputStream in = fs.open(new Path("/test/test.xtx"));//3.构造一个输出流FileOutputStream out = new FileOutputStream("bendi.txt");/* * 4.copy文件 * 使用true:代表使用完之后自动关闭 */IOUtils.copyBytes(in, out, 4096, true);}//测试上传@Testpublic void testUpload() throws Exception{FSDataOutputStream out = fs.create(new Path("/shell/test"), true);FileInputStream in = new FileInputStream("d://rainbow");IOUtils.copyBytes(in, out, 4096, true);}//测试创建文件夹@Testpublic void testMkdir() throws Exception{boolean mkdirs = fs.mkdirs(new Path("/testMkdir"));System.out.println(mkdirs ? "创建成功":"创建失败");}//测试删除@Testpublic void testDel() throws Exception{boolean delete = fs.delete(new Path("/testMkdir"), true);System.out.println(delete?"删除成功":"删除失败");}}

0 0
原创粉丝点击