@Test
public void mkDir() throws Exception{
System.out.println(fileSystem.mkdirs(new Path("/Complete")));
}
2.2 建立檔案并寫入
@Test
public void create() throws Exception{
FSDataOutputStream out = fileSystem.create(new Path("/HDFSApp/write/2.txt"));
out.writeUTF("hello! first wirte to HDFS file!");
out.flush();
out.close();
}
2.3 拷貝本地檔案到遠端
@Test
public void copyFromLocal() throws IOException {
Path localFile = new Path("F:/校驗大師_2.7.5.1632.zip");
Path remoteFile = new Path("/HDFSApp/write/校驗大師_2.7.5.1632.zip");
fileSystem.copyFromLocalFile(localFile, remoteFile);
}
2.4 拷貝大檔案,帶進度條
@Test
public void copyFromLocalLargeFile(){
String localFile = "F:/校驗大師_2.7.5.1632.zip";
Path remoteFile = new Path("/HDFSApp/write/校驗大師_2.7.5.1633.zip");
InputStream in = null;
FSDataOutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(localFile));
out = fileSystem.create(remoteFile, new Progressable() {
@Override
public void progress() {
System.out.println("!");
}
});
} catch (IOException e) {
e.printStackTrace();
System.out.println("建立輸入輸出流失敗!");
}
if(in != null && out != null){
try {
IOUtils.copyBytes(in, out,2048, true);
} catch (IOException e) {
e.printStackTrace();
System.out.println("上傳過程中失敗!");
}
}
}
3. 查詢檢索
3.1 讀檔案内容
@Test
public void text() throws Exception{
FSDataInputStream in = fileSystem.open(new Path("/output/part-r-00000"));
IOUtils.copyBytes(in, System.out, 1024);
}
3.2 下載下傳hdfs檔案到本地
@Test
public void copyToLocal() throws IOException {
Path localFile = new Path("F:/");
Path remoteFile = new Path("/HDFSApp/write/校驗大師_2.7.5.1632.zip");
fileSystem.copyToLocalFile(remoteFile, localFile);
}