美文网首页
Eclipse-HadoopAPI开发测试

Eclipse-HadoopAPI开发测试

作者: 胖胖的大罗 | 来源:发表于2016-07-12 00:41 被阅读0次

    Eclipse-HadoopAPI开发测试#

    1. 如何获取文件系统##


    public static FileSystem getFileSystem() throws Exception { //读取配置文件, read core-*.xml; hdfs-*.xml Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(configuration); System.out.println(fileSystem); return fileSystem; } public static void main(String[] args) throws Exception { //create configuration, read core-*.xml; hdfs-*.xml Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(configuration); System.out.println(fileSystem); }

    读取配置文件

    public static void read(String filename)throws Exception { FileSystem fileSystem = getFileSystem(); //read Path Path readPath = new Path(filename); //input stream FSDataInputStream inStream = fileSystem.open(readPath); try{ IOUtils.copyBytes(inStream, System.out, 4096, false); }catch(Exception e){ e.printStackTrace(); } finally{ } }

    写入文件

    `public static void main(String[] args) throws Exception {

    /* String filename = "/user/beifeng/core-site.xml";
    read(filename); */
    String filename = "/user/beifeng/wc.input";
    FileSystem fileSystem = getFileSystem();
    //read Path
    Path readPath = new Path(filename); //write file
    FSDataOutputStream outStream = fileSystem.create(readPath);
    //get input stream
    FileInputStream inStream = new FileInputStream( new File("/opt/datas/wc.input") );
    try{
    IOUtils.copyBytes(inStream, outStream, 4096, false);
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    IOUtils.closeStream(inStream); IOUtils.closeStream(outStream);
    }
    }`

    相关文章

      网友评论

          本文标题:Eclipse-HadoopAPI开发测试

          本文链接:https://www.haomeiwen.com/subject/raujjttx.html