设置环境实时可用。 Google Bigquery

时间:2017-02-20 12:04:15

标签: hadoop apache-spark google-bigquery

我正在编写一个用于spark的Google大查询连接器,并在其下方使用google hadoop连接器。

目前google hadoop连接器需要一个指向creds json文件的Google env变量。

当您在数据交换世界之外启动集群时设置这可能很烦人

在代码中实时设置是不好的做法?或者是否有一种解决方法告诉hadoop连接器忽略env变量,因为它已在“fs.gs.auth.service.account.json.keyfile”hadoop配置中设置?

Dennis既然你是该项目的贡献者,也许你也可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

对于那些感兴趣的人,我只是在运行时使用scala中的下面的要点

设置它们

https://gist.github.com/jaytaylor/770bc416f0dd5954cf0f

但是这里是代码以防gist永远离线

trait EnvHacker {
/**
 * Portable method for setting env vars on both *nix and Windows.
 * @see http://stackoverflow.com/a/7201825/293064
 */
def setEnv(newEnv: Map[String, String]): Unit = {
    try {
        val processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment")
        val theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment")
        theEnvironmentField.setAccessible(true)
        val env = theEnvironmentField.get(null).asInstanceOf[JavaMap[String, String]]
        env.putAll(newEnv)
        val theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment")
        theCaseInsensitiveEnvironmentField.setAccessible(true)
        val cienv = theCaseInsensitiveEnvironmentField.get(null).asInstanceOf[JavaMap[String, String]]
        cienv.putAll(newEnv)
    } catch {
        case e: NoSuchFieldException =>
            try {
                val classes = classOf[Collections].getDeclaredClasses()
                val env = System.getenv()
                for (cl <- classes) {
                    if (cl.getName() == "java.util.Collections$UnmodifiableMap") {
                        val field = cl.getDeclaredField("m")
                        field.setAccessible(true)
                        val obj = field.get(env)
                        val map = obj.asInstanceOf[JavaMap[String, String]]
                        map.clear()
                        map.putAll(newEnv)
                    }
                }
            } catch {
                case e2: Exception => e2.printStackTrace()
            }

        case e1: Exception => e1.printStackTrace()
    }
}

}