gpt4 book ai didi

groovy - Spark/Gradle -- 在 build.gradle 中获取 IP 地址以用于启动 master 和 worker

转载 作者:行者123 更新时间:2023-12-04 05:59:56 25 4
gpt4 key购买 nike

我在基本层面上了解 build.gradle 构建脚本的各个移动部分,但在将它们捆绑在一起时遇到了麻烦。

在 Apache Spark 独立模式下,只需尝试从 build.gradle 在同一个盒子上启动 master 和 worker。 (稍后将扩展调用 $SPARK_HOME/sbin/start-slaves 并使用正确的 masterIP 参数。)

问题:如何将我的 IP 地址分配给 Groovy/build.gradle 中的变量,以便将其传递给 Exec 任务中的命令?我们希望它在几个不同的开发机器上运行。

我们有一个(我认为相当标准的)/etc/hosts 配置,其中 FQDN 和主机名分配给 127.0.1.1。驱动程序可以解决这个问题,但不能选择使用主机名启动主机和从机,我需要 IP 地址。

我在尝试:

task getMasterIP (type: Exec){
// declare script scope variable using no def or
executable "hostname"
args += "-I"

// need results of hostname call assigned to script scope variable
sparkMasterIP = <resultsOfHostnameCall>
}

// added this because startSlave stops if Master is already running
task startSlaveOnly(dependsOn:'getMasterIP', type: Exec){
executable "/usr/local/spark/sbin/start-slave.sh"
args += "spark://$sparkMasterIP:7077"
doLast {
println "enslaved"
}
}

// now make startSlave call startSlaveOnly after the initial startMaster
task startSlave(dependsOn:'startMaster', type: Exec) {
finalizedBy 'startSlaveOnly'
}

当我尝试 something like suggested in the docs for Exec for Groovy calls :
task getMasterIP (type: Exec){
// declare script scope variable using no def or
sparkMasterIP = executable "hostname"
args += "-I"
}

我收到无法识别可执行文件的警告。

“关于我在想什么的更多背景”部分,不是主要问题。

谷歌搜索“build.gradle 脚本范围变量”并查看前两个结果, in the basic docs I only see one type of variable and ext properties to be used .

16.4. Declaring variables -- There are two kinds of variables that can be declared in a build script: local variables and extra properties.



但是在这个 other Gradle doc Appendix B. Potential Traps除了 ext 属性之外,我还看到了两种变量范围:

For Gradle users it is important to understand how Groovy deals with script variables. Groovy has two types of script variables. One with a local scope and one with a script-wide scope.



使用此示例用法:
String localScope1 = 'localScope1'
def localScope2 = 'localScope2'
scriptScope = 'scriptScope'

我假设我应该使用没有“def”或类型声明的脚本范围变量。对完全不同的方法完全开放,谢谢。

最佳答案

要获取本地 IP:

//return all v4 addresses 
def getLocalIPv4() {
def ip4s = []
NetworkInterface.getNetworkInterfaces()
.findAll { it.isUp() && !it.isLoopback() && !it.isVirtual() }
.each {
it.getInetAddresses()
.findAll { !it.isLoopbackAddress() && it instanceof Inet4Address }
.each { ip4s << it }
}
return ip4s
}

//optionally, return all ipv6 addresses
def getLocalIPv6() {
def ip6s = []
NetworkInterface.getNetworkInterfaces()
.findAll { it.isUp() && !it.isLoopback() && !it.isVirtual() }
.each {
it.getInetAddresses()
.findAll { !it.isLoopbackAddress() && it instanceof Inet6Address }
.each { ip6s << it }
}
return ip6s
}



task printIP()<<{
println getLocalIPv4()
println getLocalIPv6()
}

上面的两个函数分别返回一个 ipv4 或 ipv6 地址列表。您可能会注意到我跳过了所有本地主机、未启动的接口(interface)、所有环回和虚拟接口(interface)。如果你想使用第一个 ipv4 地址,你可以在别处使用它:
getLocalIPv4()[0]

或者在你的情况下:
args += "spark:/"+ getLocalIPv4()[0] + ":7077"

关于groovy - Spark/Gradle -- 在 build.gradle 中获取 IP 地址以用于启动 master 和 worker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34778873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com