2023/11/10周五总结

实验内容与完成情况:

1:创建local.txt和cloud.txt文件,分别存入一点内容,把cloud.txt文件上传。

 

上传成功

 

local.txt内容加到cloud.txt结尾

 

local.txt内容覆盖到cloud.txt

 

编程实现:

package com.xusheng.hdfs;

 

//import org.apache.commons.configuration2.Configuration;

import org.apache.hadoop.fs.FSDataOutputStream;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

 

import java.io.FileInputStream;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.*;

import java.io.*;

 

 

public class HDFSApi {

    /**

     * 判断路径是否存在

     */

    public static boolean test(Configuration conf, String path) throws IOException {

        FileSystem fs = FileSystem.get(conf);

        return fs.exists(new Path(path));

    }

 

    /**

     * 复制文件到指定路径

     * 若路径已存在,则进行覆盖

     */

    public static void copyFromLocalFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException {

        FileSystem fs = FileSystem.get(conf);

        Path localPath = new Path(localFilePath);

        Path remotePath = new Path(remoteFilePath);

        /* fs.copyFromLocalFile 第一个参数表示是否删除源文件,第二个参数表示是否覆盖 */

        fs.copyFromLocalFile(false, true, localPath, remotePath);

        fs.close();

    }

 

    /**

     * 追加文件内容

     */

    public static void appendToFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException {

        FileSystem fs = FileSystem.get(conf);

        Path remotePath = new Path(remoteFilePath);

        /* 创建一个文件读入流 */

        FileInputStream in = new FileInputStream(localFilePath);

        /* 创建一个文件输出流,输出的内容将追加到文件末尾 */

        FSDataOutputStream out = fs.append(remotePath);

        /* 读写文件内容 */

        byte[] data = new byte[1024];

        int read = -1;

        while ( (read = in.read(data)) > 0 ) {

            out.write(data, 0, read);

        }

        out.close();

        in.close();

        fs.close();

    }

 

    /**

     * 主函数

     */

    public static void main(String[] args) {

        Configuration conf = new Configuration();

        conf.set("fs.default.name"," hdfs://hadoop102:8020");

        //String localFilePath = "/local.txt";    // 本地路径

        //String remoteFilePath = "/export/server/hadoop-3.3.0/cloud.txt";    // HDFS路径

        String localFilePath = "/weiguo.txt";    // 本地路径

        String remoteFilePath = "/opt/module/hadoop-3.1.3/local.txt";    // HDFS路径

        String choice = "append";    // 若文件存在则追加到文件末尾

//    String choice = "overwrite";    // 若文件存在则覆盖

 

        try {

            /* 判断文件是否存在 */

            Boolean fileExists = false;

            if (HDFSApi.test(conf, remoteFilePath)) {

                fileExists = true;

                System.out.println(remoteFilePath + " 已存在.");

            } else {

                System.out.println(remoteFilePath + " 不存在.");

            }

            /* 进行处理 */

            if ( !fileExists) { // 文件不存在,则上传

                HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath);

                System.out.println(localFilePath + " 已上传至 " + remoteFilePath);

            } else if ( choice.equals("overwrite") ) {    // 选择覆盖

                HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath);

                System.out.println(localFilePath + " 已覆盖 " + remoteFilePath);

            } else if ( choice.equals("append") ) {   // 选择追加

                HDFSApi.appendToFile(conf, localFilePath, remoteFilePath);

                System.out.println(localFilePath + " 已追加至 " + remoteFilePath);

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

 

 

 

2:从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名。

 

 

3:将HDFS中指定文件的内容输出到终端中;

 

 

4:显示 HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;

 

 

5:给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息;

 

 

6: 提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。如果文件所在目录不存在,则自动创建目录;

删除文件

 

7:提供一个 HDFS的目录的路径,对该目录进行创建和删除操作。创建目录时,如果目录文件所在目录不存在则自动创建相应目录;删除目录时,由用户指定当该目录不为空时是否还删除该目录;

 

8:向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾;

开头:

 

末尾:

 

9:删除HDFS中指定的文件;

 

10:删除HDFS中指定的目录,由用户指定目录中如果存在文件时是否删除目录;

 

11:在 HDFS 中,将文件从源路径移动到目的路径。

 

(二):编程

    package com.xusheng.hdfs;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FSDataInputStream;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import java.io.*;

 

public class MyFSDataInputStream extends FSDataInputStream {

    public MyFSDataInputStream(InputStream in) {

        super(in);

    }

 

    /**

     * 实现按行读取

     * 每次读入一个字符,遇到"\n"结束,返回一行内容

     */

    public static String readline(BufferedReader br) throws IOException {

        char[] data = new char[1024];

        int read = -1;

        int off = 0;

// 循环执行时,br 每次会从上一次读取结束的位置继续读取

//因此该函数里,off 每次都从0开始

        while ( (read = br.read(data, off, 1)) != -1 ) {

            if (String.valueOf(data[off]).equals("\n") ) {

                off += 1;

                break;

            }

            off += 1;

        }

 

        if (off > 0) {

            return String.valueOf(data);

        } else {

            return null;

        }

    }

 

    /**

     * 读取文件内容

     */

    public static void cat(Configuration conf, String remoteFilePath) throws IOException {

        FileSystem fs = FileSystem.get(conf);

        Path remotePath = new Path(remoteFilePath);

        FSDataInputStream in = fs.open(remotePath);

        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        String line = null;

        while ( (line = MyFSDataInputStream.readline(br)) != null ) {

            System.out.println(line);

        }

        br.close();

        in.close();

        fs.close();

    }

 

    /**

     * 主函数

     */

    public static void main(String[] args) {

        Configuration conf = new Configuration();

        conf.set("fs.default.name","hdfs://node1.itcast.cn:8020");

        String remoteFilePath = "/user/hadoop/cloud.txt";    // HDFS路径

        try {

            MyFSDataInputStream.cat(conf, remoteFilePath);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

 

(三) :编程

package com.xusheng.hdfs;

import org.apache.hadoop.fs.*;

import org.apache.hadoop.io.IOUtils;

import java.io.*;

import java.net.URL;

 

public class HDFSApi11 {

    static{

        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());

    }

 

    /**

     * 主函数

     */

    public static void main(String[] args) throws Exception {

        String remoteFilePath = "hdfs://node1.itcast.cn:8020//user/hadoop/cloud.txt";    // HDFS文件

        InputStream in = null;

        try{

            /* 通过URL对象打开数据流,从中读取数据 */

            in = new URL(remoteFilePath).openStream();

            IOUtils.copyBytes(in,System.out,4096,false);

        } finally{

            IOUtils.closeStream(in);

        }

    }

}

 

posted @   liu_ru_jun  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示