import java.io.ByteArrayOutputStream;java
import java.io.FileInputStream;apache
import java.io.FileOutputStream;app
import java.io.InputStream;分佈式
import java.net.URL;ide
import org.apache.hadoop.conf.Configuration;oop
import org.apache.hadoop.fs.FSDataInputStream;spa
import org.apache.hadoop.fs.FSDataOutputStream;.net
import org.apache.hadoop.fs.FileSystem;xml
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();
}
}