• 通过idea测试Hadoop增删改查


      1 package day01;
      2 
      3 import org.apache.hadoop.conf.Configuration;
      4 import org.apache.hadoop.fs.*;
      5 
      6 import java.io.*;
      7 import java.net.URI;
      8 
      9 /**
     10  * @autho 通过idea进行hadoop测试增删改查
     11  * @create 2019-09-16 14:15
     12  **/
     13 public class HadoopClients {
     14 
     15    static FileSystem fileSystem = null;
     16 
     17     public static void main(String[] args) throws Exception {
     18         /*mkdir();*/
     19 
     20         /*mv();*/
     21 
     22        /* rm();*/
     23 
     24         /*up();*/
     25 
     26         /*see();*/
     27 
     28         readWrite();
     29     }
     30 
     31     //静态代码块
     32 
     33     static {
     34         Configuration conf = new Configuration();
     35         //hadoop的链接对象
     36         try {
     37              fileSystem = FileSystem.get(new URI("hdfs://hadoop01:9000"),conf,"root");
     38         } catch (Exception e) {
     39             e.printStackTrace();
     40         }
     41     }
     42     //创建目录
     43     public static void mkdir() throws Exception{
     44             fileSystem.mkdirs(new Path("/test1"));
     45             System.out.println("创建成功!!");
     46     }
     47 
     48     //修改目录
     49     public static void mv() throws Exception{
     50         fileSystem.rename(new Path("/test1"),new Path("/test2"));
     51         System.out.println("修改成功!!");
     52     }
     53 
     54     //删除目录
     55     public static void rm() throws Exception{
     56         fileSystem.delete(new Path("/test2"),true);
     57         System.out.println("删除成功!!");
     58     }
     59 
     60     //上传数据
     61     public static void up() throws Exception{
     62         fileSystem.copyFromLocalFile(new Path("B:\wc.txt"),new Path("/beida"));
     63         System.out.println("上传成功!!");
     64     }
     65 
     66     //查看目录
     67     public static void see() throws Exception{
     68         RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(new Path("/"), true);
     69         //迭代器取数据
     70         while (iterator.hasNext()){
     71             LocatedFileStatus next = iterator.next();
     72             System.out.println(next.getPath());
     73             System.out.println(next.getLen());
     74             System.out.println(next.getBlockSize());
     75             System.out.println(next.getReplication());
     76             System.out.println("-------------------------------------");
     77         }
     78     }
     79     
     80     //模拟数据的读写
     81     public static void readWrite() throws Exception{
     82         
     83         FSDataInputStream open = fileSystem.open(new Path("/beida/wc.txt"));
     84 
     85         FSDataOutputStream create = fileSystem.create(new Path("/result.txt"));
     86         
     87         //要把IO流数据转化为String类型
     88         BufferedReader br = new BufferedReader(new InputStreamReader(open));
     89         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(create));
     90         //处理数据
     91         String line = null;
     92         //br.readLine!==null是判断有没有数据
     93         while ((line=br.readLine())!=null){
     94             String[] words = line.split(",");
     95             //数据写出去
     96             bw.write(line);
     97             bw.newLine();
     98             bw.flush();
     99         }
    100         System.out.println("写出成功!!");
    101         //关闭
    102         br.close();
    103         bw.close();
    104     }
    105 }
  • 相关阅读:
    AirtestIDE 游戏自动化(unity3d跨 Windows、Mac、IOS、Android)
    Python josn 实例说明
    CS0012 类型“DbContext”在未引用的程序集中定义。必须添加对程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的引用。 Repository E:项目wxRepositoryDbContextFac
    利用Senparc.Weixin SDK 实现微信用户的授权,并获取信息
    关于SVN浏览服务器的错误
    P1074 靶形数独
    P1941 飞扬的小鸟
    P3178 [HAOI2015]树上操作
    [校内模拟题3]
    P4231 三步必杀
  • 原文地址:https://www.cnblogs.com/VisionY/p/11567512.html
Copyright © 2020-2023  润新知