HDFS文件系统的使用--java
一、准备jar包
lib下所有jar包和图上所示jar包
二、测试
@Test public void test() { //做一个配置 Configuration conf=new Configuration(); //服务器的地址 端口号 conf.set("fs.defaultFS","hdfs://192.168.1.63:9000"); try { //连接到服务器上去 FileSystem fileSystem = FileSystem.get(conf); //getFileStatus 获取当前某个路径的状态 FileStatus fileStatus = fileSystem.getFileStatus(new Path("/zp.txt")); System.out.println(fileStatus.isFile()); //是不是一个文件 System.out.println(fileStatus.isDirectory()); //是不是一个目录 System.out.println(fileStatus.getPath()); //文件的路径 System.out.println(fileStatus.getLen()); //文件大小 fileStatus.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
三、创建文件夹
@Test public void mkdir() { Configuration conf=new Configuration(); try { //连接 FileSystem fs = FileSystem.get(new URI("hdfs://192.168.1.63:9000"),conf); fs.mkdirs(new Path("/user")); fs.mkdirs(new Path("/hello/world/bailiban")); //关闭 fs.close(); } catch (IOException | URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
四、文件上传
@Test public void upload() { Configuration conf=new Configuration(); try { FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.63:9000"),conf); FSDataOutputStream out = fileSystem.create(new Path("/hadoop-2.7.7.tar")); FileInputStream in=new FileInputStream(new File("D:\\Hadoop\\hadoop-2.7.7.tar")); byte[]b=new byte[1024]; int len=0; while((len=in.read(b))!=-1) { out.write(b,0,len); } in.close(); out.close(); } catch (IOException | URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
五、文件下载
@Test public void download() { Configuration conf=new Configuration(); try { FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.63:9000"),conf); FSDataInputStream in = fileSystem.open(new Path("/zp.txt")); //流的概念 //输入流 输出流 FileOutputStream out=new FileOutputStream(new File("D://Hadoop//zp.txt")); byte[]b=new byte[1024]; int len=0; while((len=in.read(b))!=-1) { out.write(b, 0, len); } in.close(); out.close(); } catch (IOException | URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
六、重命名
@Test public void mv() { Configuration conf=new Configuration(); try { //连接 FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.63:9000"),conf); boolean rename = fileSystem.rename(new Path("/hadoop-2.7.7.tar.gz"),new Path("/hadoop.tar.gz")); System.out.println(rename?"修改成功":"修改失败"); //关闭 fileSystem.close(); } catch (IOException | URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
七、递归查看文件夹
@Test public void ls() { Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://192.168.1.63:9000"); try { FileSystem fs = FileSystem.get(conf); FileStatus[] fls = fs.listStatus(new Path("/")); for(FileStatus fst:fls) { judge(fs,fst); } fs.close(); } catch (IOException e) { e.printStackTrace(); } } public void list(FileSystem fs,FileStatus fst) { String name = fst.getPath().toString().split("hdfs://192.168.1.63:9000/")[1]; if(fst.isDirectory()) { System.out.println("d: "+name); try { FileStatus[] fls = fs.listStatus(new Path("/"+name)); for(FileStatus fst2:fls) { list(fs,fst2); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }else { System.out.println("f: "+name); } }