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

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.*;

import org.apache.hadoop.fs.FileSystem;

 

import java.io.*;

 

public class AppendToFile {

    /**

     * 判断路径是否存在

     */

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

        try (FileSystem fs = FileSystem.get(conf)) {

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

        } catch (IOException e) {

            e.printStackTrace();

            return false;

        }

    }

 

    /**

     * 追加文本内容

     */

    public static void appendContentToFile(Configuration conf, String content,

            String remoteFilePath) {

        try (FileSystem fs = FileSystem.get(conf)) {

            Path remotePath = new Path(remoteFilePath);

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

            FSDataOutputStream out = fs.append(remotePath);

            out.write(content.getBytes());

            out.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

 

    }

 

    /**

     * 追加文件内容

     */

    public static void appendToFile(Configuration conf, String localFilePath,

            String remoteFilePath) {

        Path remotePath = new Path(remoteFilePath);

        try (FileSystem fs = FileSystem.get(conf);

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

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    /**

     * 移动文件到本地 移动后,删除源文件

     */

    public static void moveToLocalFile(Configuration conf,

            String remoteFilePath, String localFilePath) {

        try (FileSystem fs = FileSystem.get(conf)) {

            Path remotePath = new Path(remoteFilePath);

            Path localPath = new Path(localFilePath);

            fs.moveToLocalFile(remotePath, localPath);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    /**

     * 创建文件

     */

    public static void touchz(Configuration conf, String remoteFilePath) {

        try (FileSystem fs = FileSystem.get(conf)) {

            Path remotePath = new Path(remoteFilePath);

            FSDataOutputStream outputStream = fs.create(remotePath);

            outputStream.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    /**

     * 主函数

     */

    public static void main(String[] args) {

        Configuration conf = new Configuration();

        conf.set("fs.defaultFS", "hdfs://master:9000");

        String remoteFilePath = "/user/hadoop/test.txt"; // HDFS文件

        String content = "新追加的内容\n";

        //String choice = "after"; // 追加到文件末尾

        String choice = "before"; // 追加到文件开头

 

        try {

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

            if (!AppendToFile.test(conf, remoteFilePath)) {

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

            } else {

                if (choice.equals("after")) { // 追加在文件末尾

                    AppendToFile.appendContentToFile(conf, content,

                            remoteFilePath);

                    System.out.println("已追加内容到文件末尾" + remoteFilePath);

                } else if (choice.equals("before")) { // 追加到文件开头

                    /* 没有相应的api可以直接操作,因此先把文件移动到本地,创建一个新的HDFS,再按顺序追加内容 */

                    String localTmpPath = "/usr/local/hadoop/tmp.txt";

                    AppendToFile.moveToLocalFile(conf, remoteFilePath,

                            localTmpPath); // 移动到本地

                    AppendToFile.touchz(conf, remoteFilePath); // 创建一个新文件

                    AppendToFile.appendContentToFile(conf, content,

                            remoteFilePath); // 先写入新内容

                    AppendToFile.appendToFile(conf, localTmpPath,

                            remoteFilePath); // 再写入原来内容

                    System.out.println("已追加内容到文件开头: " + remoteFilePath);

                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值