zoukankan      html  css  js  c++  java
  • 与HDFS交互- By java API编程

    环境(ubuntu下)

      jdk

      eclipse

      jar(很烦,整了很久才清楚)

        - 导包方法

        查看:https://www.cnblogs.com/floakss/p/9739030.html

    1)”/usr/local/hadoop/share/hadoop/common”目录下的hadoop-common-2.9.1.jar和haoop-nfs-2.9.1.jar;
    (2)“/usr/local/hadoop/share/hadoop/common/lib”目录下的所有JAR包;
    (3)“/usr/local/hadoop/share/hadoop/hdfs”目录下的haoop-hdfs-2.9.1.jar和haoop-hdfs-nfs-2.9.1.jar;
    (4)“/usr/local/hadoop/share/hadoop/hdfs/lib”目录下的所有JAR包。

    操作

      文件的创建,读入,写入,删除,上传,下载

      目录的创建,删除等

    例子 - 文件的创建

    //工具类

    import java.io.BufferedOutputStream; import java.io.IOException; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path;
    public class Temp { public void createFileOnHDFS() { String rootPath="hdfs://Kouri:9000/"; Configuration conf=new Configuration(); conf.set("fs.defaultFS", "hdfs://Kouri:9000"); conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem"); try { FileSystem fs=FileSystem.get(URI.create(rootPath),conf); Path hdfsPath=new Path(rootPath+"/user/hadoop/demo1.txt"); System.out.println(""+fs.getHomeDirectory()); String con="hello world"; FSDataOutputStream fout=fs.create(hdfsPath); BufferedOutputStream bout=new BufferedOutputStream(fout); bout.write(con.getBytes(),0,con.getBytes().length); bout.close(); fout.close(); System.out.println(hdfsPath+"创建"); } catch (IOException e) { e.printStackTrace(); } } }

    //测试类

    public class Test {
        public static void main(String []args) {
            Temp temp=new Temp();
            temp.createFileOnHDFS();
        }
    }

    结果截图:

    参考:http://dblab.xmu.edu.cn/blog/290-2/

    ...................................................
  • 相关阅读:
    virtual
    微软MBS intern笔试
    Ubuntu Linux Green hand
    Coding style
    abstract
    Jquery Ajax请求标准格式
    Hashtable的简单实用
    C#中GET和POST的简单区别
    WIN7 64位机与32位机有什么区别
    一个加密解密类
  • 原文地址:https://www.cnblogs.com/floakss/p/9738969.html
Copyright © 2011-2022 走看看