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();
        }
    
    }

    创建文件

     

     

    修改文件

     

     

    删除文件

     

     

  • 相关阅读:
    在github上用octopress搭建博客过程纪要
    各自用一句话来概括MVC、MVP、MVVM的差异特点
    二级域名使用下划线
    knockout.js的学习笔记2
    knockout.js的学习笔记
    knockout.js的学习笔记4
    利用GitHub Pages安装部署Octopress博客
    安装.net framework 4.0失败,出现HRESULT 0xc8000222错误代码
    版本控制工具GitHub发布Windows版
    C#多线程中 lock用法
  • 原文地址:https://www.cnblogs.com/wangdayang/p/15637464.html
Copyright © 2011-2022 走看看