HDFS内容追加
配置:hdfs-site.xml
<property>
<name>dfs.support.append</name>
<value>true</value>
</property>
追加一个文件
package com.wyp; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils; import java.io.*;
import java.net.URI; /**
* blog: http://www.iteblog.com/
* Date: 14-1-2
* Time: 下午6:09
*/
public class AppendContent {
public static void main(String[] args) {
String hdfs_path = "hdfs://mycluster/home/wyp/wyp.txt";//文件路径
Configuration conf = new Configuration();
conf.setBoolean("dfs.support.append", true); String inpath = "/home/wyp/append.txt";
FileSystem fs = null;
try {
fs = FileSystem.get(URI.create(hdfs_path), conf);
//要追加的文件流,inpath为文件
InputStream in = new
BufferedInputStream(new FileInputStream(inpath));
OutputStream out = fs.append(new Path(hdfs_path));
IOUtils.copyBytes(in, out, 4096, true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
追加一行内容
/**
* 以append方式将内容添加到HDFS上文件的末尾;注意:文件更新,需要在hdfs-site.xml中添<property><name>dfs.
* append.support</name><value>true</value></property>
*/
private static void appendToHdfs() throws FileNotFoundException,
IOException {
String dst = "hdfs://192.168.1.11:9000/usr/yujing/test.txt";
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dst), conf);
FSDataOutputStream out = fs.append(new Path(dst)); int readLen = "zhangzk add by hdfs java api".getBytes().length; while (-1 != readLen) {
out.write("zhangzk add by hdfs java api".getBytes(), 0, readLen);
}
out.close();
fs.close();
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDFS内容追加的更多相关文章
- [置顶] HDFS文件内容追加(Append)
HDFS设计之处并不支持给文件追加内容,这样的设计是有其背景的(如果想了解更多关于HDFS的append的曲折实现,可以参考<File Appends in HDFS>:http://bl ...
- 向HDFS中追加内容
向生成好的hdfs文件中追加内容,但是线上使用的版本是1.0.3,查看官方文档发现,在1.0.4版本以后才支持文件append 以下是向hdfs中追加信息的操作方法 如果你只在某一个driver中追加 ...
- linux 将foo制定n, m之间行的内容, 追加到bar文件
sed -ne '196, 207 p' foo >> bar;把文件foo 196-行207行的内容追加到 bar文件
- java程序向hdfs中追加数据,异常以及解决方案
今天在学习hdfs时,遇到问题,就是在向hdfs中追加数据总是报错,在经过好几个小时的努力之下终于将他搞定 解决方案如下:在hadoop的hdfs-sit.xml中添加一下三项 <propert ...
- 向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾。
1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.text.SimpleDateFormat; ...
- java对txt文件内容追加
package com.test; import java.io.FileOutputStream; /** * 对txt文件在文本追加内容 * @author Wdnncey * */ public ...
- PHP内置函数file_put_content(),将数据写入文件,使用FILE_APPEND 参数进行内容追加
file_put_contents(fileName,data,flags,context) 入参说明: 参数 说明 fileName 要写入数据的文件名 data 要写入的数据.类型可以是 stri ...
- C#文本文件或其他文件新内容追加
以txt文本为例,以下代码实现a.txt文件中追加内容 FileStream mystream = new FileStream("C:\\a.txt", FileMode.Ope ...
- Go文件内容追加问题
今天在用文件追加时,遇到点小问题,一直没法追加,起初是这样的 fp,err1 = os.OpenFile(filename,os.O_APPEND,0666) 上面这个死活加不了,后面改成 fp,er ...
随机推荐
- Jooq比较偏的用法
count public Integer count(Integer id) { return dslContext.selectCount().from(Tables.<table_name& ...
- 【leetcode刷题笔记】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...
- HDU1232 畅通工程,并查集
这里要补充一些知识点,并查集三操作 1.找到父节点递归写法int Findf(int x){ if(father[x]!=x) father[x]=Findf(father[x]); return f ...
- Oracle可能会遇到问题和解决方法
1.plsql developer查询一列用中文,出现乱码 添加环境变量NLS_LANG = SIMPLIFIED CHINESE_CHINA.ZHS16GBK->软件重启 2.ORA-1251 ...
- 访问虚拟机中的架设的Web服务器
环境: 1.虚拟机中安装了CentOS,虚拟机使用NAT的方式 2.在CentOS中安装了APACHE 并且使用 http://127.0.0.1可以正常访问,通过ifconfig查到IP地址是 19 ...
- Spring- 异常org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/context/; lineNumber: 1; columnNumber: 55; 在 publicId 和 systemId 之间需要有空格。
抛出异常 六月 03, 2018 7:40:44 下午 org.springframework.context.support.AbstractApplicationContext prepareRe ...
- HIVE- 新建UDF范例
首先pom文件导入依赖,Hadoop和hive的依赖导入自己机器的版本,hive记得导jdbc <dependency> <groupId>org.apache.hadoop& ...
- IE6、IE7、IE8及其他浏览器多个元素并排显示
IE6.IE7.IE8及其他浏览器多个元素并排显示 HTML代码 <div class="line"> <h1>全部input框</h1> &l ...
- Codeforces 455B A Lot of Games:博弈dp【多局游戏】
题目链接:http://codeforces.com/problemset/problem/455/B 题意: 给你n个字符串,然后进行k局游戏. 每局游戏开始有一个空串,然后双方轮流给这个串的末尾添 ...
- 与STL文件相关的各类学习地址
几个网址: 1.STL :https://en.wikipedia.org/wiki/STL_(file_format)#ASCII_STL 2.一个博客的文章地址: 三维图形数据格式 STL的 读取 ...