hdfs文件创建和内容的追加
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
public class HDFSTest {
private FileSystem fileSystem;
@Before
public void init() throws IOException, InterruptedException {
Configuration conf = new Configuration();
conf.set("dfs.replication", "3");
fileSystem = FileSystem.get(URI.create("hdfs://hadoop102:8020"), conf, "cz");
}
@After
public void destroy() throws IOException {
if (fileSystem != null) {
fileSystem.close();
}
}
@Test
public void testAppend() throws IOException {
Path path = new Path("/wcinput/m.txt");
FSDataOutputStream append=null;
if(fileSystem.exists(path)){
append= fileSystem.append(path);
}else {
append=fileSystem.create(path);
}
FileInputStream fileInputStream = new FileInputStream("F:\\百度云盘文档\\大数据学习\\V4.1.docx");
byte[] bytes = new byte[1024 *1024];
int a=0;
while ((a=fileInputStream.read(bytes))!=-1){
append.write(bytes,0,a);
}
append.close();
fileInputStream.close();
}
}