scala - 使用 scala 从 IntelliJ 连接到 Azure CosmosDB 时无法绕过代理
问题描述
我正在尝试使用 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 的代理?
解决方案
恐怕 IntelliJ 代理设置不会影响您的代码,您可以尝试通过 Fiddler 工具捕获请求。
也许您可以在代码中设置代理设置。请参阅此链接。
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());
}
}
推荐阅读
- ios - nib 中的单元重用标识符与标识符不匹配
- reactjs - 当服务器返回 gzip 文件时,如何下载文件以做出反应?
- javascript - 如何在正确的时间和星期几切换 div
- javascript - Bot Framework 使用空内容更新 Teams 活动
- intellij-idea - 什么是“尽可能使用模块目标 JDK 中的编译器”选项?
- node.js - 使用 node.js 从 FastAPI 服务器获取数据
- firebase - 如何仅存储日期,而不是日期时间
- python - 处理数据集中的 NaN 值以进行关键点坐标检测
- php - 在 wordpress 插件“wp paypal 购物车”中配置通知网址
- redcap - 如何根据 REDCap 中的计算数字自动选择分类变量?