Java: Hadoop檔案系統的讀寫操作
阿新 • • 發佈:2018-11-24
所需jar包路徑:
hadoop-2.8.5/share/hadoop/common
hadoop-2.8.5/share/hadoop/common/bin
hadoop-2.8.5/share/hadoop/hdfs
hadoop-2.8.5/share/hadoop/hdfs/bin
java程式碼例項
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;
import java.net.URISyntaxException;
/**
* HDFS操作,需要匯入common和hdsf資料夾下的jar
*/
public class HDSFDemo {
private FileSystem fs = null;
HDSFDemo() throws IOException, URISyntaxException, InterruptedException {
Configuration conf = new Configuration ();
fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
}
/**
* 下載檔案
*
* @throws IOException
*/
public void download() throws IOException {
InputStream in = fs.open(new Path("/java"));
OutputStream out = new FileOutputStream("/Users/Desktop" );
IOUtils.copyBytes(in, out, 4096, true);
}
/**
* 上傳檔案
*
* @throws IOException
*/
public void upload() throws IOException {
System.out.println("開始上傳...");
InputStream in = new FileInputStream("/Users/Desktop/compare.py");
OutputStream out = this.fs.create(new Path("/compare.py"));
IOUtils.copyBytes(in, out, 4096, true);
}
/**
* 刪除檔案
*
* @throws IOException
*/
public void delete() throws IOException {
boolean ret = this.fs.delete(new Path("/java"), true);
System.out.println("檔案刪除結果:" + ret);
}
/**
* 建立資料夾
*
* @throws IOException
*/
public void mkdir() throws IOException {
boolean ret = this.fs.mkdirs(new Path("/temp"));
System.out.println("檔案建立成功:" + ret);
}
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
HDSFDemo hdfs = new HDSFDemo();
hdfs.upload();
}
}
備註:
URI的埠號一定要和配置檔案中的埠號一致
問題:
log4j:WARN Please initialize the log4j system properly
可參考:log4j:WARN Please initialize the log4j system properly 問題解決