1. Java/Scala客户端获取HA高可用HDFS的Active NameNode
通过轮询的方式判断多个Namenode是否可用。如果当前NameNode不可用,则继续判断下一个;如果可用,则获取当前NameNode的Uri
示例代码
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.FileSystem
import org.apache.hadoop.ipc.StandbyException
import scala.util.control.Breaks.{break, breakable}
object HadoopTest {
def main(args: Array[String]): Unit = {
val hadoopConf = new Configuration()
val hdfsUris = Array(
"hdfs://192.168.23.101:8020",
"hdfs://192.168.23.102:8020",
"hdfs://192.168.23.103:8020"
)
var hdfsCli: FileSystem = null
var hdfsCapacity: Long = -1L
var activeHdfsUri: String = null
breakable {
for (hdfsUri <- hdfsUris) {
hadoopConf.set("fs.defaultFS", hdfsUri)
hdfsCli = FileSystem.get(hadoopConf)
try {
hdfsCapacity = hdfsCli.getStatus.getCapacity
activeHdfsUri = hdfsUri
break
} catch {
case hdfsException: StandbyException => {}
}
}
}
println(activeHdfsUri)
}
}