|
|
@@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* this file is used to complete hdfs operates and mongodb operates
|
|
|
@@ -24,19 +25,42 @@ public class HdfsDao {
|
|
|
//connect to hdfs
|
|
|
private FileSystem hdfs;
|
|
|
private Configuration conf;
|
|
|
+
|
|
|
+ private String hdfsServerAddr;
|
|
|
private String hdfsUri; //get hdfs Address
|
|
|
|
|
|
+
|
|
|
@Autowired
|
|
|
- public HdfsDao(AppConfig appConfig) {
|
|
|
+ public HdfsDao(AppConfig appConfig) { //在初始化方法中使用到的bean需要以注入初始化方法的形式导入,否则读取到的引用可能是null。
|
|
|
this.appConfig = appConfig;
|
|
|
+ //hdfs主机地址 hdfs://ip:port/
|
|
|
+ hdfsServerAddr= appConfig.getHdfsServerAddr();
|
|
|
+ //hdfs存储位置 hdfs://ip:port/user-space/*/
|
|
|
+ hdfsUri= appConfig.getHdfsUri();
|
|
|
conf = new Configuration();
|
|
|
- hdfsUri = appConfig.getHdfsServerAddr();
|
|
|
+
|
|
|
+ setUserSpace();
|
|
|
setConf();
|
|
|
}
|
|
|
|
|
|
+ //保证hdfs存储位置可以访问
|
|
|
+ private void setUserSpace(){
|
|
|
+ //先根据主机ip设置配置
|
|
|
+ conf.set("fs.defaultFS", hdfsServerAddr);
|
|
|
+ try {
|
|
|
+ hdfs = FileSystem.get(conf);
|
|
|
+ //若不存在文件夹,新建
|
|
|
+ //若文件夹已存在,新建失败
|
|
|
+ hdfs.mkdirs(new Path(hdfsUri));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
//set the configuration and get connection
|
|
|
private void setConf(){
|
|
|
conf.set("fs.defaultFS", hdfsUri);
|
|
|
+ log.info("当前主机下的hdfs目录是:"+hdfsUri);
|
|
|
//System.out.println(conf);
|
|
|
try {
|
|
|
hdfs = FileSystem.get(conf);
|