hive學習之五:java通過zookeeper獲取active namenode地址。
在專案開發前期,使用jdbc訪問hive資料倉儲,在配置檔案配置active namenode的地址資訊,上線的時候才發現如果namenode當機了,程式將無法執行。後來改成通過zookeeper獲取active namenode地址,這樣即使namenode當機了,也能通過zookeeper獲取備機地址從而不影響程式執行。記錄下程式碼:
package cp.app.preprocess.service;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.server.namenode.ha.proto.HAZKInfoProtos;
import org.apache.hadoop.hdfs.server.namenode.ha.proto.HAZKInfoProtos.ActiveNodeInfo;
import org.apache.log4j.Logger;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import com.google.protobuf.InvalidProtocolBufferException;
import cp.app.batch.utils.ConfigUtil;
/**
* hdfs公共服務類
* @author author
*
*/
public class HDFSService {
private static Logger log = Logger.getLogger(HDFSService.class.getName());
static ConfigUtil conf = new ConfigUtil("conf/zookeeper.properties");
private static final String ZOOKEEPER_IP = conf.getString("ZOOKEEPER_IP");
private static final int ZOOKEEPER_PORT = conf.getInt("ZOOKEEPER_PORT");
private static final int ZOOKEEPER_TIMEOUT = conf.getInt("ZOOKEEPER_TIMEOUT");
private static final String DATA_DIR = conf.getString("DATA_DIR");
/**
* 判斷檔案是否存在
* @param dirs hdfs路徑
* @return 存在ture 否則false
*/
public static boolean isExist(List<String> dirs,String ddate) {
if(dirs==null||dirs.size()<1){
log.info("Please Check your sign directory configure is correct!");
return false;
}
log.info("Check File or Directory, uri is:"+dirs.toString());
Configuration conf = new Configuration();
List<String> flags = new ArrayList<String>();
FileStatus status=null;
String hostname = getHostname(ZOOKEEPER_IP,ZOOKEEPER_PORT,ZOOKEEPER_TIMEOUT,DATA_DIR);
log.info("According to Zookeeper get the active namenode domain:"+hostname);
try {
for(String dir:dirs){
String url = dir.replaceAll("url", hostname);
FileSystem fs = FileSystem.get(URI.create(url+"dt="+ddate), conf);
status = fs.getFileStatus(new Path(url+"dt="+ddate));
if(status==null){
flags.add("false");
}else{
flags.add("true");
}
fs.close();
}
if(flags.contains("false")){
return false;
}else{
return true;
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
log.error(e);
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
log.error(e);
return false;
}
}
/**
* ͨ通過zookeeper獲取active namenode地址
* @param ZOOKEEPER_IP ip地址
* @param ZOOKEEPER_PORT 埠
* @param ZOOKEEPER_TIMEOUT 超時時間
* @return 地址
*/
public static String getHostname(String ZOOKEEPER_IP, int ZOOKEEPER_PORT,
int ZOOKEEPER_TIMEOUT,String DATA_DIR) {
String hostname = null;
Watcher watcher = new Watcher() {
public void process(org.apache.zookeeper.WatchedEvent event) {
log.info("event:"+event.toString());
}
};
ZooKeeper zk = null;
byte[] data1 = null;
String[] iparr = ZOOKEEPER_IP.split(";");
for (String ip : iparr) {
try {
zk = new ZooKeeper(ip + ":" + ZOOKEEPER_PORT,
ZOOKEEPER_TIMEOUT, watcher);
data1 = zk.getData(DATA_DIR,
true, new Stat());
} catch (Exception e) {
// TODO Auto-generated catch block
log.info("This ip is not active..."+ip);
continue;
}
if (data1 != null) {
log.info("This ip is normal..."+ip);
ActiveNodeInfo activeNodeInfo=null;
try {
activeNodeInfo = HAZKInfoProtos.ActiveNodeInfo.parseFrom(data1);
} catch (InvalidProtocolBufferException e) {
// TODO Auto-generated catch block
log.error(e);
}
hostname = activeNodeInfo.getHostname();
return hostname;
}
}
return hostname;
}
}
zookeeper配置檔案zookeeper.properties:
ZOOKEEPER_IP=192.168.1.1;192.168.1.2;192.168.1.3
ZOOKEEPER_PORT=2181
ZOOKEEPER_TIMEOUT=30000
DATA_DIR=/hadoop-ha/hdp/ActiveStandbyElectorLock
讀取配置檔案工具類:
package com.huateng.spdbccc.mkt24.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 配置檔案讀取工具類
* @author author
*
*/
public class ConfigUtil {
private Properties props = new Properties();
public ConfigUtil(String file){
InputStream is = ConfigUtil.class.getClassLoader().getResourceAsStream(file);
try {
props.load(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int getInt(String key){
return Integer.parseInt(props.getProperty(key));
}
public String getString(String key){
return props.getProperty(key);
}
}
hiveserer2的配置檔案:
driver=org.apache.hive.jdbc.HiveDriver
url=jdbc:hive2://192.168.1.1:10010/default
user=hdfs
password=""
相關文章
- hive學習筆記之五:分桶Hive筆記
- Hive學習之五 《Hive進階—UDF操作案例》 詳解Hive
- java獲取ip地址和mac地址JavaMac
- 通過Java反射動態獲取資訊Java反射
- Java 中獲取MAC地址 和IP地址JavaMac
- JAVA之反射學習1-如何獲取Class物件Java反射物件
- Zabbix學習(五)之Zabbix Agent Active 主動模式監控模式
- Java獲取本機ip地址Java
- Hive學習之Hive的安裝Hive
- HIVE學習之(三)Hive
- Kafka學習之(五)搭建kafka叢集之Zookeeper叢集搭建Kafka
- DHCP獲取IP地址的過程
- java web 通過request獲取客戶端IPJavaWeb客戶端
- 學習五:zooKeeper的安裝配置
- java獲取本機的ip地址Java
- Hive學習之基本操作Hive
- JAVA之反射學習2-獲取構造方法並使用Java反射構造方法
- Hive學習之六 《Hive進階— —hive jdbc》 詳解HiveJDBC
- Java——通過反射獲取函式引數名稱Java反射函式
- 通過EPROCESS獲取程式名
- Hive學習之JDBC訪問HiveJDBC
- 獲取URL地址
- Java獲取本機名稱、本機MAC地址、IP地址JavaMac
- JavaScript 通過class獲取元素物件JavaScript物件
- 通過用shellcode獲取shell
- [譯]通過超市買牛奶來學習快取快取
- java學習路程(javaEE)之獲取表單資料、獲取引數、request域、會話技術(Cookie)Java會話Cookie
- salesforce零基礎學習(一百一十三)Trigger中獲取IP地址的過程Salesforce
- 【Java學習筆記之五】java陣列詳解Java筆記陣列
- hive學習筆記之十一:UDTFHive筆記
- Hive學習之常用互動命令Hive
- Hive學習之型別轉化Hive型別
- C++ 獲取指定的過載函式地址C++函式
- saltstack獲取IP地址
- 獲取IP地址方法
- 獲取IP地址命令
- Grails通過sessionId獲取session物件AISession物件
- 通過web url獲取檔案資訊Web