向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 ...
随机推荐
- PhPMyadmin拿Shell
phpmyadmin 是一个以PHP为基础,以Web-Base方式架构在网站主机上的MySQL的数据库管理工具, --百度百科 1.入口寻找 目录扫描: 根据Linux对大小写敏感判断目标服 ...
- [NOIP2013 提高组] 货车运输
前言 使用算法:堆优化 \(prim\) , \(LCA\) . 题意 共有 \(n\) 个点,有 \(m\) 条边来连接这些点,每条边有权值.有 \(q\) 条类似于 \(u\) \(v\) 询问, ...
- Qt学习笔记-制作一个文本编辑器
创建一个MainWindow工程.添加一个TextEdit.垂直布局. 在menu上面创建新的action. 在新建的时候判断文本有没有被修改. 下面,将某个文件打开读入到TextEdit中. 保存文 ...
- 基于Opencv的简单图像处理
实验环境 本实验均在笔记本电脑完成,电脑的配置如表1所示: 系统 Windows 10 家庭版 处理器 英特尔 Core i5-6200 @ 2.30GHz 双核 主板 宏碁 Zoro_SL 内存 1 ...
- 如何在K8S中优雅的使用私有镜像库 (Docker版)
前言 在企业落地 K8S 的过程中,私有镜像库 (专用镜像库) 必不可少,特别是在 Docker Hub 开始对免费用户限流之后, 越发的体现了搭建私有镜像库的重要性. 私有镜像库不但可以加速镜像的拉 ...
- ProceedingJoinPoint 某些方法记录一下
转载与百度知道,记录一下.遇到在去看API 官方文档//拦截的实体类 Object target = point.getTarget(); //拦截的方法名称 String methodName = ...
- javaweb登陆实例
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncod ...
- 修改/查看ssh端口
修改ssh端口 vi /etc/ssh/sshd_config 将Port修改为需要的端口 Port 212 重启ssh服务 service sshd restart 查看ssh端口 netstat ...
- TCP实现网络通讯
Tcp server的流程:1.创建套接字:2.bind绑定ip和port3.listen使套接字变为可以被动链接:4.accept等待客户端的链接(返回为服务器分配的客户端的句柄和地址)5.reci ...
- linkedhashmap中关于LRU算法的实现
//LinkedHashMap的一个构造函数,当参数accessOrder为true时,即会按照访问顺序排序,最近访问的放在最前,最早访问的放在后面 public LinkedHashMap(int ...