與HDFS交互- By java API編程
阿新 • • 發佈:2018-10-03
ava java.net oca dir fault dem except 上傳 out
環境(ubuntu下)
jdk
eclipse
jar(很煩,整了很久才清楚)
- 導包方法
查看:
(1)”/usr/local/hadoop/share/hadoop/common”目錄下的hadoop-common-2.9.1.jar和haoop-nfs-2.9.1.jar; (2)“/usr/local/hadoop/share/hadoop/common/lib”目錄下的所有JAR包; (3)“/usr/local/hadoop/share/hadoop/hdfs”目錄下的haoop-hdfs-2.9.1.jar和haoop-hdfs-nfs-2.9.1.jar; (4)“/usr/local/hadoop/share/hadoop/hdfs/lib”目錄下的所有JAR包。
操作
文件的創建,讀入,寫入,刪除,上傳,下載
目錄的創建,刪除等
例子 - 文件的創建
//工具類
import java.io.BufferedOutputStream; import java.io.IOException; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path;public class Temp { public void createFileOnHDFS() { String rootPath="hdfs://Kouri:9000/"; Configuration conf=new Configuration(); conf.set("fs.defaultFS", "hdfs://Kouri:9000"); conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem"); try { FileSystem fs=FileSystem.get(URI.create(rootPath),conf); Path hdfsPath=new Path(rootPath+"/user/hadoop/demo1.txt"); System.out.println(""+fs.getHomeDirectory()); String con="hello world"; FSDataOutputStream fout=fs.create(hdfsPath); BufferedOutputStream bout=new BufferedOutputStream(fout); bout.write(con.getBytes(),0,con.getBytes().length); bout.close(); fout.close(); System.out.println(hdfsPath+"創建"); } catch (IOException e) { e.printStackTrace(); } } }
//測試類
public class Test {
public static void main(String []args) {
Temp temp=new Temp();
temp.createFileOnHDFS();
}
}
結果截圖:
參考:http://dblab.xmu.edu.cn/blog/290-2/
與HDFS交互- By java API編程