import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;
public class append {
/**
* 通过API实现append追加操作
*
* @author Administrator
*
* [hadoop@hadoop01 ~]$ hdfs dfs -chmod -R a+w /user/hadoop
* [hadoop@hadoop01 ~]$ hdfs dfs -ls -R /
*
*
*/
@Test
public void appendfile() throws Exception {
//创建configuration对象,有个默认的加载顺序,先从core-default.xml,再到src目录中的文件,这里
//我们给定了
Configuration conf = new Configuration();
//通过conf的configuration对象创建了该分布式文件系统fs,默认如果不指定文件的话为本地文件系统
FileSystem fs = FileSystem.get(conf);
//定义一个URL的字符串
String file = “hdfs://hadoop01:9000/user/hadoop/data/hello.txt”;
//通过一个URL的字符串构建一个path对象
Path path = new Path(file);
FSDataOutputStream out = fs.append(path);
out.writeChars(“chuck/r/n”);
System.out.println(out.toString());
out.close();
}
}
原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/194129.html