zoukankan      html  css  js  c++  java
  • MapReduce——调用HDFS

     MapReduce——调用HDFS

    调用HDFS文件接口实现对分布式文件系统中文件的访问,如创建、修改、删除等。

    实验代码

    package mapreduce;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FSDataInputStream;
    import org.apache.hadoop.fs.FSDataOutputStream;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.IOUtils;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.util.Scanner;
    
    public class test01 {
        private static FileSystem fs;
        private static Scanner input=new Scanner(System.in);
        //获取文件系统
        public static FileSystem init() throws URISyntaxException, IOException, InterruptedException {
            if(fs==null){
                Configuration conf = new Configuration();
                fs = FileSystem.get(new URI("hdfs://192.168.109.10:9000"),conf,"root");
            }
            return fs;
        }
    
        //关闭资源
        public void close() throws IOException {
            if(fs!=null){
                fs.close();
            }
    
        }
    
        //创建文件
        public static void createFile(String path) throws IOException, URISyntaxException, InterruptedException {
            fs=init();
            fs.create(new Path(path));
            if(isExist(path)){
                System.out.println("文件创建成功");
            }else {
                System.out.println("文件创建失败");
            }
        }
    
        //创建文件
        public static void deleteFile(String path) throws IOException, URISyntaxException, InterruptedException {
            fs=init();
            fs.delete(new Path(path),true);   //可递归删除
            if(!isExist(path)){
                System.out.println("文件删除成功");
            }else {
                System.out.println("文件删除失败");
            }
        }
    
        //向终端展示文件内容
        public static void showFile(String path) throws InterruptedException, IOException, URISyntaxException {
            fs=init();
            FSDataInputStream in = fs.open(new Path(path));
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
            String content =  bufferedReader.readLine(); //读取文件一行
            while (content!=null){
                System.out.println(content);
                content=bufferedReader.readLine();
            }
            bufferedReader.close();
            in.close();
        }
    
    
    
        //向已有文件追加内容
        public static void appendFile(String path) throws IOException, URISyntaxException, InterruptedException {
            fs=init();
            System.out.println("追加前");
            showFile(path);
    
            System.out.println("请输入你想追加的内容");
            String content = input.next();
            FSDataOutputStream out = fs.append(new Path(path));
            out.write(content.getBytes());  //默认追加在末尾
            IOUtils.closeStream(out);
    
            System.out.println("追加后");
            showFile(path);
        }
    
    
        //判断文件是否存在
        public static boolean isExist(String path) throws IOException {
            return fs.exists(new Path(path));
        }
    
    
    
        public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
            createFile("/wdy/test.txt");
            //deleteFile("/wdy/test.txt");
            //appendFile("/wdy/test.txt");
    
            fs.close();
        }
    
    }

    创建文件

     

     

    修改文件

     

     

    删除文件

     

     

  • 相关阅读:
    C++中的空类,编译器默认可以产生哪些成员函数
    野指针(Wild pointer)和悬垂指针(dangling pointer)
    WHY C++ ?(by Herb Sutter) & C++17 standard
    mapreduce 多路输出
    stdout 编码 vim 删除左边,右边
    积累碎片shell
    python logging模块
    shell 流程控制
    shell 变量
    时间管理法则
  • 原文地址:https://www.cnblogs.com/wangdayang/p/15637464.html
Copyright © 2011-2022 走看看