zoukankan      html  css  js  c++  java
  • 在eclipse下操作HDFS

    //方法一通过一个类IOUtils来实现

    写文件 create 读取文件 open 删除文件delete

    
    

    创建目录 mkdirs 删除文件或目录 delete 列出目录的内容 listStatus 显示文件系统的目录和文件的元数据信息 getFileStatus


    package
    hdfs; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import org.apache.hadoop.fs.FsUrlStreamHandlerFactory; import org.apache.hadoop.io.IOUtils; public class App1 { /** * @param args */ //首先定义一个连接hadoop的URI(标识符和URL做区别) public static final String HDFS_PATH="hdfs://hadoop:9000/hello"; public static void main(String[] args) throws Exception { // TODO Auto-generated method stub //url不认识hdfs://所以需要解析器 URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory()); //用静态类IOUtils.copyBytes() URL url = new URL(HDFS_PATH); InputStream in = url.openStream(); IOUtils.copyBytes(in, System.out , 1024, true ); } }
    //方法二用过类FileSystem中方法.mkdir   .create    .open   get
    package hdfs;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.net.URL;
    
    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.fs.FileSystemTestHelper.fileType;
    import org.apache.hadoop.io.IOUtils;
    
    
    public class App2 {
        
        public static final String HDFS_PATH="hdfs://hadoop:9000";
        public static final String DIR_PATH="/d1000";
        public static final String FILE_PATH="/d1000/f1000";
        public static void main(String[] args) throws Exception, URISyntaxException {
    /*
            final  FileSystem fileSystem = FileSystem.get(new URI(HDFS_PATH),new Configuration() );
            
            //创建文件mkdir
            fileSystem.mkdirs(new Path(DIR_PATH));
            //上传文件create
            FSDataOutputStream out = fileSystem.create(new Path(FILE_PATH));
            FileInputStream in= new FileInputStream("C:/Users/Administrator/Desktop/01.txt");
            IOUtils.copyBytes(in,out,1024,true);
            //下载文件open
            final FSDataInputStream in2= fileSystem.open(new Path(FILE_PATH));
            IOUtils.copyBytes(in2, System.out, 1024,true );
            //删除文件delete
            fileSystem.delete(new Path(FILE_PATH), true);*/
            FileSystem fileSystem = FileSystem.get(new URI(HDFS_PATH),new Configuration());
            //mkdir
            fileSystem.mkdirs(new Path(DIR_PATH));
            //上传create创造目标通道
            FSDataOutputStream out = fileSystem.create(new Path(FILE_PATH));
            FileInputStream in = new FileInputStream("C:/Users/Administrator/Desktop/01.txt");
            IOUtils .copyBytes(in,out , 1024, true);
            //下载文件open一个通道
            FSDataInputStream in2 = fileSystem.open(new Path(FILE_PATH));
            IOUtils.copyBytes(in2, System.out ,1024,true);
            //删除delete
            fileSystem.delete(new Path(DIR_PATH),true );
        }
    
    }
  • 相关阅读:
    MongoDB快速入门教程 (4.1)
    MongoDB快速入门教程 (3.3)
    MongoDB快速入门教程 (3.2)
    MongoDB快速入门教程(3.1)
    MongoDB快速入门教程 (2)
    MongoDB快速入门教程 (1)
    express高效入门教程(5)
    刷过的题 分类汇总
    Binary Tree & Divide Conquer 完整笔记------part 2
    binary search完整笔记
  • 原文地址:https://www.cnblogs.com/dandandeyoushangnan/p/4703057.html
Copyright © 2011-2022 走看看