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

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
向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾。的更多相关文章
- 向HDFS中追加内容
向生成好的hdfs文件中追加内容,但是线上使用的版本是1.0.3,查看官方文档发现,在1.0.4版本以后才支持文件append 以下是向hdfs中追加信息的操作方法 如果你只在某一个driver中追加 ...
- 每日学习心得:SharePoint 为列表中的文件夹添加子项(文件夹)、新增指定内容类型的子项、查询列表中指定的文件夹下的内容
前言: 这里主要是针对列表中的文件下新增子项的操作,同时在新建子项时,可以为子项指定特定的内容类型,在某些时候需要查询指定的文件夹下的内容,针对这些场景都一一给力示例和说明,都是一些很小的知识点,希望 ...
- hdfs中删除文件、文件夹、抓取内容
删除文件 bin/hdfs dfs -rm output2/* 删除文件夹 bin/hdfs dfs -rm -r output2 抓取内容 bin/hdfs dfs -cat /us ...
- HDFS中文件的压缩与解压
HDFS中文件的压缩与解压 文件的压缩有两大好处:1.可以减少存储文件所需要的磁盘空间:2.可以加速数据在网络和磁盘上的传输.尤其是在处理大数据时,这两大好处是相当重要的. 下面是一个使用gzip工具 ...
- flume 增量上传日志文件到HDFS中
1.采集日志文件时一个很常见的现象 采集需求:比如业务系统使用log4j生成日志,日志内容不断增加,需要把追加到日志文件中的数据实时采集到hdfs中. 1.1.根据需求,首先定义一下3大要素: 采集源 ...
- HDFS 中文件操作的错误集锦
问题1 Java ApI执行追加写入时:无法写入 问题描述: ①当前数据节点无法写入,②追加文件需要再次请求. 问题2 命令行执行追加写入时:无法写入 问题描述: 当前数据节点无法写入 问题3 ...
- hadoop学习;大数据集在HDFS中存为单个文件;安装linux下eclipse出错解决;查看.class文件插件
sudo apt-get install eclipse 安装后打开eclipse,提示出错 An error has occurred. See the log file /home/pengeor ...
- Java将文件中的内容转换为sql语句(和并发定时读取文件)
数据文件内容data.txt {USER_TYPE=1,CREATE_USER=ZHANG,UPDATE_USER=li,OPER_NUM=D001,SRC=2,UPDATE_TIME=2018-11 ...
- HTML 5 应用程序缓存(Application Cache)cache manifest 文件使用 html5 中创建manifest缓存以及更新方法 一个manifest文件会创建一份缓存,不同的manifest文件其缓存的内容是互不干扰的
HTML5 离线缓存-manifest简介 HTML 5 应用程序缓存 使用 HTML5,通过创建 cache manifest 文件,可以轻松地创建 web 应用的离线版本. 什么是应用程序缓存(A ...
随机推荐
- python初学者-判断今天是今年的第几天代码
判断今天是今年的第几天源代码 import time date =time.localtime() year,month,day=date[:3] day_month=[31,28,31,30,31, ...
- spring mvc与mybatis与maven+mysql框架整合
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- Android驱动学习-Eclipse安装与配置
在ubuntu系统下安装配置Eclipse软件.并且让其支持编译java程序和内核驱动程序. 1. 下载Eclipse软件. 打开官网:http://www.eclipse.org/ 点击 DOWN ...
- 什么是CDN?哪些是流行的jQuery CDN?使用CDN有什么好处?
内容传送网络或内容分发网络(CDN)是部署在因特网上的多个数据中心的大型分布式服务器系统.CDN的目标是为具有高可 用性和高性能的最终用户提供内容. 有3个流行的jQuery CDN:谷歌,微软jQu ...
- CAP理论和BASE理论及数据库的ACID中关于一致性及不同点的思考
CAP定理又被称作是布鲁尔定理,是加州大学伯克利分销计算机科学家里克在2000年提出,是分布式理论基础. CAP:是分布式系统的理论基础 [一致性 可用性 分区容错性] BASE理论是对CAP中 ...
- 24位PCM采样数据转成16位算法,已实现PCM转WAV在线工具源码支持24bits、16bits、8bits
目录 算法来源 js版24位PCM转8位.16位代码 js版8位.16位PCM转成24位 附:浏览器控制台下载数据文件代码 相关实现 最近收到几个24位的PCM录音源文件,Recoder库原有的PCM ...
- 2018年第九届蓝桥杯B组(201806-----递增三元组)
给定三个整数数组 A = [A1, A2, - AN], B = [B1, B2, - BN], C = [C1, C2, - CN], 请你统计有多少个三元组(i, j, k) 满足: 1 < ...
- PyCharm 2019、2020、2021专业版激活
PyCharm下载地址:https://www.jetbrains.com/pycharm/download/ PyCharm社区版功能基本够用,但是作为傲娇的程序员,咱都是上来就专业版,然后各种破解 ...
- 如何优雅地开发HarmonyOS APP应用
目录: 一.挖掘项目需求或者做项目移植 二.创建项目工程 三.功能模块实现的流程思路 四.养成良好的编程规范习惯以及运用设计模式 研究HarmonyOS有一段时间了,今天主要结合自己多年的项目开发经验 ...
- Qt开发的应用记录读取用户习惯设置的方法
Qt开发的应用记录读取用户习惯设置的方法 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/w ...