Hadoop的配置
阿新 • • 發佈:2018-11-09
package xxx.bigdata.common;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.hadoop.conf.Configuration;
/**
* Hadoop連線配置
*
* @author Ethan
* @version 1.0 2017-03-12
*/
public class ConfigurationHadoop {
private static final String PROPERTY_FILE = "hadoop.properties";
/**
* 獲取預設連線
*
* @return
*/
public static Configuration getConfigurationHadoop() {
return getConfigurationHadoop(PROPERTY_FILE);
}
/**
* 獲取指定的Hadoop連線
*
* @param propertiesFile
* @return
*/
public static Configuration getConfigurationHadoop(String propertiesFile) {
Properties pro = getProperties(propertiesFile);
Configuration conf = new Configuration();
conf.set("fs.default.name", pro.getProperty("fs.default.name"));
conf.set("hbase.master", pro.getProperty("hbase.master"));
conf.set("hbase.zookeeper.quorum", pro.getProperty("hbase.zookeeper.quorum"));
conf.set("hbase.zookeeper.property.clientPort", pro.getProperty("hbase.zookeeper.property.clientPort"));
System.setProperty("HADOOP_USER_NAME", pro.getProperty("user.name"));
return conf;
}
/**
* 載入對應連線的資原始檔
* @param propertiesFile
* @return
*/
private static Properties getProperties(String propertiesFile) {
Properties pro = new Properties();
try {
pro.load(ConfigurationHadoop.class.getResourceAsStream(propertiesFile));
} catch (FileNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
return pro;
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = getConfigurationHadoop();
System.out.println(conf.get("fs.default.name"));
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.hadoop.conf.Configuration;
/**
* Hadoop連線配置
*
* @author Ethan
* @version 1.0 2017-03-12
*/
public class ConfigurationHadoop {
/**
* 獲取預設連線
*
* @return
*/
public static Configuration getConfigurationHadoop() {
return getConfigurationHadoop(PROPERTY_FILE);
}
/**
* 獲取指定的Hadoop連線
*
* @param propertiesFile
*/
public static Configuration getConfigurationHadoop(String propertiesFile) {
Properties pro = getProperties(propertiesFile);
Configuration conf = new Configuration();
conf.set("fs.default.name", pro.getProperty("fs.default.name"));
conf.set("hbase.master", pro.getProperty("hbase.master"));
conf.set("hbase.zookeeper.property.clientPort", pro.getProperty("hbase.zookeeper.property.clientPort"));
System.setProperty("HADOOP_USER_NAME", pro.getProperty("user.name"));
return conf;
}
/**
* 載入對應連線的資原始檔
* @param propertiesFile
* @return
*/
private static Properties getProperties(String propertiesFile) {
Properties pro = new Properties();
try {
pro.load(ConfigurationHadoop.class.getResourceAsStream(propertiesFile));
} catch (FileNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
return pro;
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = getConfigurationHadoop();
System.out.println(conf.get("fs.default.name"));
}
}
hadoop.properties檔案:
fs.default.name=hdfs://cmaster1.cn:8020
user.name=oozie
hbase.master=cmaster1.cn:16000
hbase.zookeeper.quorum=cmaster1.cn,cmaster0.cn,cslave0.cn
hbase.zookeeper.property.clientPort=2181