zoukankan      html  css  js  c++  java
  • HDFS常规操作-Hadoop(工作太忙,仅仅作为记录)

      1 package hdfs;
      2 
      3 import java.net.URI;
      4 
      5 import org.apache.hadoop.conf.Configuration;
      6 import org.apache.hadoop.fs.BlockLocation;
      7 import org.apache.hadoop.fs.FSDataInputStream;
      8 import org.apache.hadoop.fs.FSDataOutputStream;
      9 import org.apache.hadoop.fs.FileStatus;
     10 import org.apache.hadoop.fs.FileSystem;
     11 import org.apache.hadoop.fs.Path;
     12 import org.apache.hadoop.hdfs.DistributedFileSystem;
     13 import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
     14 import org.apache.hadoop.io.IOUtils;
     15 
     16 /** hadoop Hdfs 常规操作 */
     17 public class Hdfs {
     18 
     19     // 入口 main
     20     public static void main(String[] args) throws Exception {
     21         // 将本地文件上传至HDFS中
     22         // uploadFile();
     23 
     24         // 在HDFS上创建文件
     25         // createFile();
     26 
     27         // 在HDFS上创建文件夹目录
     28         // createDir();
     29 
     30         // 给HDFS上文件重新命名
     31         // fileRename();
     32 
     33         // 删除文件
     34         // deleteFile();
     35 
     36         // 读取文件内容
     37         // readFile();
     38 
     39         // 判断文件是否存在
     40         // isFileExists();
     41 
     42         // 查看文件状态和最后修改时间
     43         // fileLastModify();
     44 
     45         // 查看文件位置,查看文件块,在哪台机器上(datanode)
     46         // fileLocation();
     47 
     48         // DataNode列表
     49         // nodeList();
     50 
     51         // 上传单词文本文档到HDFS上
     52         // uploadWords();
     53 
     54         // 其他test
     55         // createDir2();
     56         // deleteFile2();
     57         // createEmptyFile();
     58         // deleteEmptyFile();
     59     }
     60 
     61     static FileSystem getFileSystem() throws Exception {
     62         // HDFS URI地址及端口号
     63         URI uri = new URI("hdfs://localhost:9000/");
     64         // 使用HDFS文件系统并提供服务器路径,端口号在core-site.xml中配置
     65         FileSystem fileSystem = FileSystem.get(uri, new Configuration());
     66         return fileSystem;
     67     }
     68 
     69     public static void uploadFile() throws Exception {
     70 
     71         FileSystem hdfs = getFileSystem();
     72         // 本地文件地址
     73         Path src = new Path("D:/data/SBSNTEST111.txt");
     74         // 文件在HDFS上存放的地址
     75         Path dst = new Path("/");
     76         FileStatus files[] = hdfs.listStatus(dst);
     77         for (FileStatus file : files) {
     78             System.out.println(file.getPath());
     79         }
     80         System.out.println("------------after upload--------------------");
     81         hdfs.copyFromLocalFile(src, dst);
     82         files = hdfs.listStatus(dst);
     83         for (FileStatus file : files) {
     84             System.out.println(file.getPath());
     85         }
     86         System.out.println("------------upload ok--------------------");
     87 
     88         // 将路径指定文件的内容输出到stdout
     89         // hadoop fs -cat hdfs://localhost:9000/SBSNTEST111.txt
     90         // 返回结果,文本文件内容
     91 
     92         // 查看指定路径文件
     93         // hadoop fs -ls hdfs://localhost:9000/
     94         // 返回结果,如下:
     95         // Found 1 items
     96         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
     97     }
     98 
     99     public static void createFile() throws Exception {
    100 
    101         byte[] buff = "Hello Hadoop 888@Chinasofti
    ".getBytes();
    102         FileSystem hdfs = getFileSystem();
    103         Path dfs = new Path("/testcreate");
    104         FSDataOutputStream outputStream = hdfs.create(dfs);
    105         outputStream.write(buff, 0, buff.length);
    106         outputStream.close();
    107         System.out.println("------------createFile ok--------------------");
    108 
    109         // 查看指定路径文件
    110         // hadoop fs -ls hdfs://localhost:9000/
    111         // 返回结果,如下:
    112         // Found 2 items
    113         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    114         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    115 
    116         // 将路径指定文件的内容输出到stdout
    117         // hadoop fs -cat hdfs://localhost:9000/testcreate
    118         // 返回结果,文本文件内容
    119         // Hello Hadoop 888@Chinasofti
    120     }
    121 
    122     public static void createDir() throws Exception {
    123         FileSystem hdfs = getFileSystem();
    124         Path dfs = new Path("/TestDir");
    125         hdfs.mkdirs(dfs);
    126         System.out.println("------------createDir ok--------------------");
    127 
    128         // 查看指定路径文件
    129         // hadoop fs -ls hdfs://localhost:9000/
    130         // 返回结果,如下:
    131         //Found 3 items
    132         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    133         //drwxr-xr-x   - jiangshan supergroup          0 2021-09-08 16:06 hdfs://localhost:9000/TestDir
    134         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    135 
    136         // 查看指定路径文件
    137         // hadoop fs -ls hdfs://localhost:9000/TestDir
    138         // 返回结果为空
    139     }
    140 
    141     public static void fileRename() throws Exception {
    142         FileSystem hdfs = getFileSystem();
    143         Path frpaht = new Path("/testcreate");//原文件名称
    144         Path topath = new Path("/testcreate2");//新文件名称
    145         boolean isRename = hdfs.rename(frpaht, topath);
    146         String result = isRename ? "成功" : "失败";
    147         System.out.println("文件重命名结果为:" + result);
    148         System.out.println("------------fileRename ok--------------------");
    149 
    150         // 查看指定路径文件
    151         // hadoop fs -ls hdfs://localhost:9000/
    152         // 返回结果,如下:
    153         //Found 3 items
    154         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    155         //drwxr-xr-x   - jiangshan supergroup          0 2021-09-08 16:06 hdfs://localhost:9000/TestDir
    156         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate2
    157 
    158         // 将路径指定文件的内容输出到stdout
    159         // hadoop fs -cat hdfs://localhost:9000/testcreate2
    160         // 返回结果,文本文件内容
    161         // Hello Hadoop 888@Chinasofti
    162     }
    163 
    164     public static void deleteFile() throws Exception {
    165         FileSystem hdfs = getFileSystem();
    166         Path delef = new Path("/TestDir");
    167         boolean isDeleted = hdfs.delete(delef, false);
    168         // 递归删除
    169         // boolean isDeleted=hdfs.delete(delef, true);
    170         System.out.println("Delete " + isDeleted);
    171         System.out.println("------------deleteFile ok--------------------");
    172 
    173         // 查看指定路径文件
    174         // hadoop fs -ls hdfs://localhost:9000/
    175         // 返回结果,如下:
    176         //Found 3 items
    177         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    178         //drwxr-xr-x   - jiangshan supergroup          0 2021-09-08 16:06 hdfs://localhost:9000/TestDir
    179         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    180 
    181         // 查看指定路径文件
    182         // hadoop fs -ls hdfs://localhost:9000/
    183         // 返回结果,如下:
    184         //Found 2 items
    185         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    186         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    187 
    188     }
    189 
    190     public static void readFile() throws Exception {
    191         FileSystem fileSystem = getFileSystem();
    192         FSDataInputStream openStream = fileSystem.open(new Path("/testcreate"));
    193         IOUtils.copyBytes(openStream, System.out, 1024, false);
    194         IOUtils.closeStream(openStream);
    195         System.out.println("------------readFile ok--------------------");
    196 
    197         // 查看指定路径文件
    198         // hadoop fs -ls hdfs://localhost:9000/
    199         // 返回结果,如下:
    200         //Found 2 items
    201         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    202         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    203 
    204         // 将路径指定文件的内容输出到stdout
    205         // hadoop fs -cat hdfs://localhost:9000/testcreate
    206         // 返回结果,文本文件内容: Hello Hadoop 888@Chinasofti
    207     }
    208 
    209     public static void isFileExists() throws Exception {
    210         FileSystem hdfs = getFileSystem();
    211         Path findf = new Path("/test1");
    212         boolean isExists = hdfs.exists(findf);
    213         System.out.println("Exist " + isExists);
    214         System.out.println("------------isFileExists ok--------------------");
    215         // 查看指定路径文件
    216         // hadoop fs -ls hdfs://localhost:9000/
    217         // 返回结果,如下:
    218         //Found 2 items
    219         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    220         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    221 
    222     }
    223 
    224     public static void fileLastModify() throws Exception {
    225         FileSystem hdfs = getFileSystem();
    226         Path fpath = new Path("/testcreate");
    227         FileStatus fileStatus = hdfs.getFileStatus(fpath);
    228         long modiTime = fileStatus.getModificationTime();
    229         System.out.println("testcreate的修改时间是" + modiTime);
    230         System.out.println("------------fileLastModify ok--------------------");
    231         // 查看指定路径文件
    232         // hadoop fs -ls hdfs://localhost:9000/
    233         // 返回结果,如下:
    234         //Found 2 items
    235         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    236         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    237     }
    238 
    239     public static void fileLocation() throws Exception {
    240         FileSystem hdfs = getFileSystem();
    241         Path fpath = new Path("/testcreate");
    242         FileStatus filestatus = hdfs.getFileStatus(fpath);
    243         BlockLocation[] blkLocations = hdfs.getFileBlockLocations(filestatus,
    244                 0, filestatus.getLen());
    245         int blockLen = blkLocations.length;
    246         for (int i = 0; i < blockLen; i++) {
    247             String[] hosts = blkLocations[i].getHosts();
    248             System.out.println("block_" + i + "_location:" + hosts[0]);
    249         }
    250         System.out.println("------------fileLocation ok--------------------");
    251         // 查看指定路径文件
    252         // hadoop fs -ls hdfs://localhost:9000/
    253         // 返回结果,如下:
    254         //Found 2 items
    255         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    256         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    257     }
    258 
    259     public static void nodeList() throws Exception {
    260         FileSystem fs = getFileSystem();
    261         DistributedFileSystem hdfs = (DistributedFileSystem) fs;
    262 
    263         DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
    264         for (int i = 0; i < dataNodeStats.length; i++) {
    265             System.out.println("DataNode_" + i + "_Name:"
    266                     + dataNodeStats[i].getHostName());
    267         }
    268         System.out.println("------------nodeList ok--------------------");
    269 
    270         // 查看指定路径文件
    271         // hadoop fs -ls hdfs://localhost:9000/
    272         // 返回结果,如下:
    273         //Found 2 items
    274         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    275         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    276 
    277     }
    278 
    279     public static void uploadWords() throws Exception {
    280 
    281         FileSystem hdfs = getFileSystem();
    282         // 本地文件地址
    283         Path src = new Path("D:/data/words");
    284         // 文件在HDFS上存放的地址
    285         Path dst = new Path("/");
    286         FileStatus files[] = hdfs.listStatus(dst);
    287         for (FileStatus file : files) {
    288             System.out.println(file.getPath());
    289         }
    290         System.out.println("------------after upload--------------------");
    291         hdfs.copyFromLocalFile(src, dst);
    292         files = hdfs.listStatus(dst);
    293         for (FileStatus file : files) {
    294             System.out.println(file.getPath());
    295         }
    296         System.out.println("------------uploadWords ok--------------------");
    297 
    298         // 将路径指定文件的内容输出到stdout
    299         // hadoop fs -cat hdfs://localhost:9000/words
    300         // 返回结果,文本文件内容
    301 
    302         // 查看指定路径文件
    303         // hadoop fs -ls hdfs://localhost:9000/
    304         // 返回结果,如下:
    305         // Found 3 items
    306         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    307         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    308         //-rw-r--r--   3 jiangshan supergroup  163254798 2021-09-08 16:56 hdfs://localhost:9000/words
    309     }
    310 
    311     public static void createDir2() throws Exception {
    312         FileSystem hdfs = getFileSystem();
    313         Path dfs1 = new Path("/wordcount1");
    314         hdfs.mkdirs(dfs1);
    315         Path dfs2 = new Path("/wordcount2");
    316         hdfs.mkdirs(dfs2);
    317         System.out.println("------------createDir ok--------------------");
    318 
    319         // 将路径指定文件的内容输出到stdout
    320         // hadoop fs -cat hdfs://localhost:9000/words
    321         // 返回结果,文本文件内容
    322 
    323         // 查看指定路径文件
    324         // hadoop fs -ls hdfs://localhost:9000/
    325         // 返回结果,如下:
    326         // Found 5 items
    327         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    328         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    329         //drwxr-xr-x   - jiangshan supergroup          0 2021-09-08 19:21 hdfs://localhost:9000/wordcount1
    330         //drwxr-xr-x   - jiangshan supergroup          0 2021-09-08 19:21 hdfs://localhost:9000/wordcount2
    331         //-rw-r--r--   3 jiangshan supergroup  163254798 2021-09-08 16:56 hdfs://localhost:9000/words
    332     }
    333 
    334     public static void deleteFile2() throws Exception {
    335         FileSystem hdfs = getFileSystem();
    336         Path delef1 = new Path("/wordcount1");
    337         // boolean isDeleted1 = hdfs.delete(delef1, false);
    338         // 递归删除
    339          boolean isDeleted1 = hdfs.delete(delef1, true);
    340         System.out.println("Delete " + isDeleted1);
    341 
    342         Path delef2 = new Path("/wordcount2");
    343         // boolean isDeleted2 = hdfs.delete(delef2, false);
    344         // 递归删除
    345          boolean isDeleted2 = hdfs.delete(delef2, true);
    346         System.out.println("Delete " + isDeleted2);
    347         System.out.println("------------deleteFile ok--------------------");
    348 
    349         // 查看指定路径文件
    350         // hadoop fs -ls hdfs://localhost:9000/
    351         // 返回结果,如下:
    352         //
    353 
    354     }
    355 
    356     public static void createEmptyFile() throws Exception {
    357 
    358         byte[] buff = "".getBytes();
    359         FileSystem hdfs = getFileSystem();
    360         Path dfs = new Path("/wordcountout");
    361         FSDataOutputStream outputStream = hdfs.create(dfs);
    362         outputStream.write(buff, 0, buff.length);
    363         outputStream.close();
    364         System.out.println("------------createEmptyFile ok--------------------");
    365 
    366         // 查看指定路径文件
    367         // hadoop fs -ls hdfs://localhost:9000/
    368         // 返回结果,如下:
    369         //Found 4 items
    370         //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    371         //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    372         //-rw-r--r--   3 jiangshan supergroup          0 2021-09-08 20:17 hdfs://localhost:9000/wordcountout
    373         //-rw-r--r--   3 jiangshan supergroup  163254798 2021-09-08 16:56 hdfs://localhost:9000/words
    374 
    375         // 将路径指定文件的内容输出到stdout
    376         // hadoop fs -cat hdfs://localhost:9000/wordcountout
    377         // 返回结果,文本文件内容
    378         //
    379     }
    380 
    381     public static void deleteEmptyFile() throws Exception {
    382         FileSystem hdfs = getFileSystem();
    383         Path delef1 = new Path("/wordcountout");
    384          boolean isDeleted1 = hdfs.delete(delef1, false);
    385         // 递归删除
    386 //        boolean isDeleted1 = hdfs.delete(delef1, true);
    387         System.out.println("Delete " + isDeleted1);
    388         System.out.println("------------deleteEmptyFile ok--------------------");
    389 
    390         // 查看指定路径文件
    391         // hadoop fs -ls hdfs://localhost:9000/
    392         // 返回结果,如下:
    393         //
    394 
    395     }
    396 
    397 }
    398 
    399 // 查看指定路径文件
    400 // hadoop fs -ls hdfs://localhost:9000/
    401 // 返回结果,如下:
    402 //Found 3 items
    403 //-rw-r--r--   3 jiangshan supergroup  573545760 2021-09-08 15:48 hdfs://localhost:9000/SBSNTEST111.txt
    404 //drwxr-xr-x   - jiangshan supergroup          0 2021-09-08 16:06 hdfs://localhost:9000/TestDir
    405 //-rw-r--r--   3 jiangshan supergroup         28 2021-09-08 16:01 hdfs://localhost:9000/testcreate
    406 
    407 // 将路径指定文件的内容输出到stdout
    408 // hadoop fs -cat hdfs://localhost:9000/testcreate
    409 // 返回结果,文本文件内容
    410 // Hello Hadoop 888@Chinasofti
    个人学习记录
  • 相关阅读:
    apache .htaccess文件详解和配置技巧总结
    看懂UML类图和时序图
    你必须了解的Session的本质
    php中fopen不能创建中文文件名文件的问题
    RabbitMQ消费服务关掉时会删除exchange,导致生成服务发布内容失败
    fastjson 调用JSONObject.toJSON(),如果是解析泛型对象会报OutOfMemoryError错误
    docker启动springboot项目,中文打印乱码
    Spring事务嵌套抛异常org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only
    Springboot + SLF4j + Log4j2 打印异常日志时,耗时要5-6秒
    SVN设置全局忽略提交文件或者目录
  • 原文地址:https://www.cnblogs.com/jeshy/p/15244695.html
Copyright © 2011-2022 走看看