机架感知脚本
使用python3编写机架感知脚本,报存到topology.py,给予执行权限
import sys import os DEFAULT_RACK="/default-rack" HADOOP_CONF_DIR = os.environ['HADOOP_CONF_DIR'] FILE="topology.data" host_rack={} for line in open(os.path.join(HADOOP_CONF_DIR, FILE)): (host, rack) = line.split() host_rack[host] = rack #获取命令行的输入 for host in sys.argv[1:]: if host in host_rack: print(host_rack[host]) else: print(DEFAULT_RACK)
topology.data文件内容如下:
[root@test ~ 18:29:00]#cat /data3/hadoop/hadoop/etc/hadoop/topology.data 192.168.4.46 /dn1/rack 192.168.4.47 /dn2/rack
执行脚本
[root@test script_py 18:33:38]#python topology.py 192.168.4.46 192.168.4.47 192.168.4.48 /dn1/rack /dn2/rack /default-rack
shell编写脚本,报存到topology.sh,给予执行权限。
#!/bin/bash #HADOOP_CONF=/tmp while [ $# -gt 0 ] ; do nodeArg=$1 #文件内容输入到标准输入流 exec<${HADOOP_CONF_DIR}/topology.data result="" while read line do #把输入的每一行定义为数组 ar=( $line ) if [ "${ar[0]}" = "$nodeArg" ] #||[ "${ar[1]}" = "$nodeArg" ] then result="${ar[1]}" fi done shift if [ -z "$result" ] then echo "/default-rack" else echo "$result" fi done
执行脚本:
[root@test ~ 18:35:07]#bash topology.sh 192.168.4.46 192.168.4.47 192.168.4.48 /dn1/rack /dn2/rack /default-rack
配置core-site.xml文件
<property> <name>topology.script.file.name</name> <value>${HADOOP_CONF_DIR}/topology.sh</value> </property>