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筆記
- Zabbix學習(五)之Zabbix Agent Active 主動模式監控模式
- JAVA之反射學習1-如何獲取Class物件Java反射物件
- java web 通過request獲取客戶端IPJavaWeb客戶端
- java獲取本機的ip地址Java
- Hive學習之Hive的安裝Hive
- HIVE學習之(三)Hive
- JAVA之反射學習2-獲取構造方法並使用Java反射構造方法
- salesforce零基礎學習(一百一十三)Trigger中獲取IP地址的過程Salesforce
- 通過用shellcode獲取shell
- Hive學習之基本操作Hive
- 獲取URL地址
- Grails通過sessionId獲取session物件AISession物件
- JavaScript 通過class獲取元素物件JavaScript物件
- C++ 獲取指定的過載函式地址C++函式
- Hive學習之JDBC訪問HiveJDBC
- saltstack獲取IP地址
- java學習路程(javaEE)之獲取表單資料、獲取引數、request域、會話技術(Cookie)Java會話Cookie
- [譯]通過超市買牛奶來學習快取快取
- JAVA之反射學習3-反射獲取成員變數並賦值Java反射變數賦值
- Hive學習之常用互動命令Hive
- Hive學習之型別轉化Hive型別
- hive學習筆記之八:SqoopHive筆記OOP
- hive學習筆記之十一:UDTFHive筆記
- 原始碼|HDFS之NameNode:啟動過程原始碼
- 獲取配置的mock地址Mock
- 通過WebGoat學習java反序列化漏洞WebGoJava
- Android 通過名稱獲取資源IDAndroid
- 通過反射獲取私有內部類物件反射物件
- 通過web url獲取檔案資訊Web
- Unity通過子物件獲取父物件例子Unity物件
- 如何通過WinDbg獲取方法引數值
- android通過MediaProjection獲取螢幕內容AndroidProject
- Hive學習Hive
- 009 通過連結串列學習Rust之IntoIterRust
- 010 通過連結串列學習Rust之IterRust
- 011 通過連結串列學習Rust之IterMutRust
- Hive學習之常見屬性配置Hive