zoukankan      html  css  js  c++  java
  • 云计算-HDFS接口编程

    一、要求

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

    参考代码:

     

    package com.me.sy1;
    
    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 HDFSjkbc {
        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://hadoop100: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("/yjs/wzs.txt");
            //deleteFile("/yjs/wzs.txt");
            appendFile("/yjs/wzs.txt");
    
            fs.close();
        }
    
    }
    View Code
  • 相关阅读:
    Linux的基础优化
    Linux日志文件/var/log详解
    Linux下inittab文件详解
    Linux内核优化
    Linux虚拟机网络连接的三种方式
    Linux下ssh的使用
    nginx安装Lets Encrypt SSL免费HTTPS加密证书
    centos7.2 安装 nginx
    CentOS 7 安装php7
    linux tar 解压出错
  • 原文地址:https://www.cnblogs.com/20183544-wangzhengshuai/p/13831750.html
Copyright © 2011-2022 走看看