zoukankan      html  css  js  c++  java
  • java操作hdfs实例

    环境:window7+eclipse+vmware虚拟机+搭建好的hadoop环境(master、slave01、slave02)

    内容:主要是在windows环境下,利用eclipse如何来操作hdfs,如上传文件、删除文件、创建文件夹、查看节点信息等。

    eclipse开发环境的搭建,请参考:http://www.cnblogs.com/bookwed/p/4816521.html

    1、新建maven项目,(主要是因为要引入一些jar包,除非是特别清楚要引入哪些jar包可以不用建maven项目)

      创建web项目的细节不作说明了,下面把关键的pom依赖信息贴出来,这里主要是hadoop的基础包和hdfs包  

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.6.2</version>
        </dependency>
          <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>    

    2、创建普通java类,编写代码,贴出部分代码,如下:

     1 public class OperaHDFS {
     2     public static void main(String args[]) throws IOException {
     3         //测试 创建新文件
     4         //byte[] contents = "hello world 世界你好
    --created by eclipse
    ".getBytes();
     5         //createFile("/eclipse/first.txt", contents);    //或 createFile("hdfs://192.168.137.56:9000/eclipse/first.txt", contents); 
     6         
     7         //测试 上传本地文件
     8         //uploadFile("D:\c.txt", "/eclipse/");
     9         
    10         //测试重命名
    11         //rename("/eclipse/c.txt", "/eclipse/cc.txt");
    12         
    13         //测试删除文件
    14         //delete("/eclipse/cc.txt"); //使用相对路径
    15         //delete("/eclipse2");    //删除目录
    16         
    17         //测试新建目录
    18         //mkdir("/eclipse2/");
    19         
    20         //测试读取文件
    21         //readFile("/eclipse/first.txt");
    22         
    23         //测试文件是否存在
    24         //fileIsExists("/eclipse/first.txt");
    25         
    26         getNodeMsgHdfs();
    27         
    28     }
    29 
    30     //1、创建新文件(直接生成指定路径下的first.txt,即:/eclipse/first.txt)
    31     public static void createFile(String dst, byte[] contents) throws IOException {
    32         Configuration conf = new Configuration();
    33         System.out.println("-----------:"+conf);
    34         conf.set("fs.defaultFS", "hdfs://192.168.137.56:9000");    //master
    35         FileSystem fs = FileSystem.get(conf);
    36         Path dstPath = new Path(dst); // 目标路径
    37         // 打开一个输出流
    38         FSDataOutputStream outputStream = fs.create(dstPath);
    39         outputStream.write(contents);
    40         outputStream.close();
    41         fs.close();
    42         System.out.println("文件创建成功!");
    43     }
    44 
    45     //2、上传本地文件
    46     public static void uploadFile(String src, String dst) throws IOException {
    47         Configuration conf = new Configuration();
    48         conf.set("fs.defaultFS", "hdfs://192.168.137.56:9000");    //master
    49         FileSystem fs = FileSystem.get(conf);
    50         Path srcPath = new Path(src); // 源路径
    51         Path dstPath = new Path(dst); // 目标路径
    52         // 调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为false
    53         fs.copyFromLocalFile(false, srcPath, dstPath);
    54 
    55         // 打印文件路径
    56         System.out.println("Upload to " + conf.get("fs.default.name"));
    57         //列出指定路径下的所有文件
    58         System.out.println("------------list files------------" + "
    ");
    59         FileStatus[] fileStatus = fs.listStatus(dstPath);
    60         for (FileStatus file : fileStatus) {
    61             System.out.println(file.getPath()+"--"+file.getGroup()+"--"+file.getBlockSize()+"--"+file.getLen()+"--");
    62         }
    63         fs.close();
    64     }
      }

    3、完整代码,请参考: http://pan.baidu.com/s/1eRsXp6M 密码: 9tg9,里面还有一些关于压缩文件的例子。

      

      

  • 相关阅读:
    BZOJ 3506 机械排序臂 splay
    BZOJ 2843 LCT
    BZOJ 3669 魔法森林
    BZOJ 2049 LCT
    BZOJ 3223 文艺平衡树 splay
    BZOJ 1433 假期的宿舍 二分图匹配
    BZOJ 1051 受欢迎的牛 强连通块
    BZOJ 1503 郁闷的出纳员 treap
    BZOJ 1096 ZJOI2007 仓库设计 斜率优化dp
    BZOJ 1396: 识别子串( 后缀数组 + 线段树 )
  • 原文地址:https://www.cnblogs.com/bookwed/p/5280332.html
Copyright © 2011-2022 走看看