【问题标题】:Setting Environment Vairable in Real Time. Google Bigquery实时设置环境变量。谷歌大查询
【发布时间】:2017-02-20 12:04:14
【问题描述】:

我正在为 spark 编写一个谷歌大查询连接器,在它下面使用谷歌 hadoop 连接器。

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

当您在 dataproc 世界之外启动集群时,设置这可能很烦人

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

Dennis 既然你是这个项目的贡献者,这次也许你也可以帮忙?

【问题讨论】:

  • @dennis-huo 你被提到了
  • 嗯,我不记得在哪里使用了环境变量;它应该只使用 Hadoop 配置键。你有一个指向代码环境变量在哪里被使用的指针吗?
  • @DennisHuo 它在创建客户端时使用 val bigquery = { val credential = GoogleCredential.getApplicationDefault.createScoped(SCOPES) new Bigquery.Builder(new NetHttpTransport, new JacksonFactory, credential) .setApplicationName("spark- bigquery") .build()

标签: hadoop apache-spark google-bigquery


【解决方案1】:

对于那些感兴趣的人,我只是使用 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()
    }
}

}

【讨论】:

    猜你喜欢
    • 2019-10-21
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2019-07-31
    • 2018-07-13
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多