向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾。
2024-08-31 06:22:08
1 import java.io.FileInputStream;
2 import java.io.IOException;
3 import java.text.SimpleDateFormat;
4 import java.util.Scanner;
5
6 import org.apache.hadoop.conf.Configuration;
7 import org.apache.hadoop.fs.FSDataInputStream;
8 import org.apache.hadoop.fs.FSDataOutputStream;
9 import org.apache.hadoop.fs.FileSystem;
10 import org.apache.hadoop.fs.Path;
11
12 public class H_AppendorBefore {
13 public static void DelFile(FileSystem fs, Path p_remotepath) {
14 try {
15 if (fs.delete(p_remotepath, true)) {
16 ;
17 }
18 } catch (Exception e) {
19 e.printStackTrace();
20 }
21
22 }
23
24 public static void appendToFileBefore(FileSystem fs, String localFilePath,
25 String remoteFilePath) {
26 Path remotePath = new Path(remoteFilePath);
27
28 try {
29 FileInputStream in_local = new FileInputStream(localFilePath);
30 FSDataInputStream in_remote = fs.open(remotePath);
31 DelFile(fs, remotePath);
32 FSDataOutputStream out = fs.create(remotePath);
33 out.close();
34 out = fs.append(remotePath);
35 byte[] data = new byte[1024];
36 int read = -1;
37 while ((read = in_local.read(data)) > 0) {
38 out.write(data, 0, read);
39 }
40 while ((read = in_remote.read(data)) > 0) {
41 out.write(data, 0, read);
42 }
43 out.close();
44 System.out.println("write_before success");
45 } catch (IOException e) {
46 e.printStackTrace();
47 }
48 }
49
50 public static void appendToFile(FileSystem fs, String localFilePath,
51 String remoteFilePath) {
52 Path remotePath = new Path(remoteFilePath);
53 try {
54 FileInputStream in = new FileInputStream(localFilePath);
55 FSDataOutputStream out = fs.append(remotePath);
56 byte[] data = new byte[1024];
57 int read = -1;
58 while ((read = in.read(data)) > 0) {
59 out.write(data, 0, read);
60 }
61 out.close();
62 System.out.println("write_append success");
63 } catch (IOException e) {
64 e.printStackTrace();
65 }
66 }
67
68 public static void main(String[] args) {
69 try {
70 Var_init var = new Var_init();
71 Scanner sc = new Scanner(System.in);
72 System.out.println("input wa to write_append, wb to write_before");
73 String str = sc.next();
74 if (str.equals("wa")) {
75 appendToFile(var.fs, var.s_localFilePath, var.s_remoteFilePath);
76 } else if (str.equals("wb")) {
77 appendToFileBefore(var.fs, var.s_localFilePath,
78 var.s_remoteFilePath);
79 }
80 } catch (Exception e) {
81 e.printStackTrace();
82 }
83 }
84
85 }
Var_init类参考https://www.cnblogs.com/MiraculousB/p/13848744.html
最新文章
- MVC跨项目路由
- *HDU 1385 最短路 路径
- git pull 然后 ahead of origin/master * commit 消失
- Flash Builder快捷键
- Spring-task-timer定时器
- DW(五):polybase集群安装
- 【转】java代码中实现android背景选择的selector-StateListDrawable的应用
- SQL 数据类型、约束、索引及视图
- mysql select简单用法
- 带宽 VS CDN (转载)
- 我的定时关机程序(MFC实现) .
- LPC1768IAP(详解,有上位机)
- MySQL - 扩展性 3 负载均衡:眼花缭乱迷人眼
- frameset基础了解
- 自制操作系统Antz(11)——实现shell(下)命令响应
- NGUI外包开发总结一下今天的收获
- 同步手绘板——PC端实现画板
- QT5入门之23 -QT串口编程(转)
- Flask web开发之路十
- 服务程序 -st
热门文章
- Django中关于“CSRF verification failed. Request aborted”的问题
- CSS中margin:auto什么意思?margin:auto属性的用法详解
- 本地缓存高性能之王Caffeine
- Java学习日报8.6
- Linux 时间同步 02 ntpd、ntpdate的区别
- 数据库1 --- >; 数据库概念、安装、卸载
- 一些php文件函数
- MySQL45讲笔记-事务隔离级别,为什么你改了数据我看不见
- 浅谈sql索引
- Java并发包源码学习系列:挂起与唤醒线程LockSupport工具类