【问题标题】:Unable to bypass proxy while connecting to Azure CosmosDB from IntelliJ using scala使用 scala 从 IntelliJ 连接到 Azure CosmosDB 时无法绕过代理
【发布时间】:2019-08-09 07:06:09
【问题描述】:

我正在尝试使用 Scala 语言从 intelliJ 连接到 azure cosmos DB。

当我从家庭网络连接笔记本电脑时,我能够从数据库中获取数据。但是当我从办公室网络连接笔记本电脑时出现超时错误。这似乎是一些代理问题,所以我在 intelliJ 中配置了代理设置,如下所示。

我试图从代理设置页面测试我的连接,但它抛出如下错误。

Problem with connection: Request failed with status code 401

我尝试从浏览器打开相同的内容并收到以下错误。

{"code":"Unauthorized","message":"Required Header authorization is missing. Ensure a valid Authorization token is passed.\r\nActivityId: af2d771f-25a3-494a-90dd-b33cd66104e2, Microsoft.Azure.Documents.Common/2.5.1"}

以下是我尝试过的 intelliJ 代码,该代码有效。

mport org.apache.spark.sql.SparkSession
import com.microsoft.azure.cosmosdb.spark.schema._
import com.microsoft.azure.cosmosdb.spark._
import com.microsoft.azure.cosmosdb.spark.config.Config
import org.apache.log4j.Logger

object DeleteData {
  /* Get the logger */
  val log = Logger.getLogger(getClass.getName)

  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().master("local").appName("DeleteData").getOrCreate()

    // Configure connection to your collection
    val readConfig = Config(Map(
      "Endpoint" -> "https://***.documents.azure.com:443/",
      "Masterkey" -> "***",
      "Database" -> "ConnectivityDB",
      "Collection" -> "historicData",
      "ConnectionMode" -> "Gateway",
      "SamplingRatio" -> "1.0",
      "query_custom" -> "SELECT c.NUM from c where c._ts = 1564566440"
    ))
    // Connect via azure-cosmosdb-spark to create Spark DataFrame
    val docs = spark.read.cosmosDB(readConfig)
    println("Total Count: " +docs.count())
    docs.show(5)

    spark.stop()
  }
}

有没有办法绕过代理从 intelliJ 连接到 cosmos DB?

【问题讨论】:

  • 请不要在公共论坛上显示您的私人信息,这对您来说风险很大。即使我已经为您编辑了,您最好重新生成您的主密钥。
  • @JayGong 谢谢 :) 甚至我提供的端点和主键也不是精确值。今后我会负责隐藏私人信息。

标签: scala azure intellij-idea azure-cosmosdb azure-cosmosdb-sqlapi


【解决方案1】:

我担心IntelliJ代理设置不会影响你的代码,你可以尝试通过Fiddler工具捕获请求。

也许您可以在代码中设置代理设置。请参阅link

import java.net.Authenticator
import java.net.PasswordAuthentication

class ProxyAuthenticator(user: String, password: String) extends Authenticator {

  def this() = this("default_user_name", "default_password")

  override def getPasswordAuthentication(): PasswordAuthentication = {
    return new PasswordAuthentication(user, password.toCharArray());
  }
}

【讨论】:

  • Intellij IDEA 代理设置适用于 IDE 内部网络服务,如插件市场或更新。
  • @jay Gong: 上面的解决方案不起作用.. 即使我试过这个val connectionPolicy = new ConnectionPolicy connectionPolicy.setProxy("proxyhost", portnum) 但不起作用
  • @Antony 还是遇到同样的问题?
  • 是的。即使在代码中设置代理后,我也面临同样的问题。
猜你喜欢
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 2019-06-29
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-31
相关资源
最近更新 更多