在 CosmosDB MongoDB API 数据库上连接 AzureDatabricks
Connecting AzureDatabricks on a CosmosDB MongoDB API database
我正在尝试在 CosmosDB MongoDB API 数据库上的 Azure Databricks 集群中连接一个 Python notebook。
我正在使用 mongo 连接器 2.11.2.4.2
Python3
我的代码如下:
ReadConfig = {
"Endpoint" : "https://<my_name>.mongo.cosmos.azure.com:443/",
"Masterkey" : "<my_key>",
"Database" : "database",
"preferredRegions" : "West US 2",
"Collection": "collection1",
"schema_samplesize" : "1000",
"query_pagesize" : "200000",
"query_custom" : "SELECT * FROM c"
}
df = spark.read.format("mongo").options(**ReadConfig).load()
df.createOrReplaceTempView("dfSQL")
我得到的错误是无法初始化 class com.mongodb.spark.config.ReadConfig$.
我该如何解决这个问题?
确保使用最新的 Azure Cosmos DB Spark 连接器。
为您使用的 Apache Spark 版本下载最新的 azure-cosmosdb-spark 库 运行:
Spark 2.4:azure-cosmosdb-spark_2.4.0_2.11-2.1.2-uber.jar
Spark 2.3:azure-cosmosdb-spark_2.3.0_2.11-1.2.2-uber.jar
Spark 2.2:azure-cosmosdb-spark_2.2.0_2.11-1.1.1-uber.jar
按照上传 Jar、Python Egg 或 Python Wheel 中的说明将下载的 JAR 文件上传到 Databricks。
将上传的库安装到您的 Databricks 集群中。
回答我自己的问题。
使用 MAVEN 作为源,我使用路径
将正确的库安装到我的集群
org.mongodb.spark:mongo-spark-connector_2.11:2.4.0
Spark 2.4
我使用的代码示例如下(给想尝试的人):
# Read Configuration
readConfig = {
"URI": "<URI>",
"Database": "<database>",
"Collection": "<collection>",
"ReadingBatchSize" : "<batchSize>"
}
pipelineAccounts = "{'$sort' : {'account_contact': 1}}"
# Connect via azure-cosmosdb-spark to create Spark DataFrame
accountsTest = (spark.read.
format("com.mongodb.spark.sql").
options(**readConfig).
option("pipeline", pipelineAccounts).
load())
accountsTest.select("account_id").show()
我正在尝试在 CosmosDB MongoDB API 数据库上的 Azure Databricks 集群中连接一个 Python notebook。
我正在使用 mongo 连接器 2.11.2.4.2 Python3
我的代码如下:
ReadConfig = {
"Endpoint" : "https://<my_name>.mongo.cosmos.azure.com:443/",
"Masterkey" : "<my_key>",
"Database" : "database",
"preferredRegions" : "West US 2",
"Collection": "collection1",
"schema_samplesize" : "1000",
"query_pagesize" : "200000",
"query_custom" : "SELECT * FROM c"
}
df = spark.read.format("mongo").options(**ReadConfig).load()
df.createOrReplaceTempView("dfSQL")
我得到的错误是无法初始化 class com.mongodb.spark.config.ReadConfig$.
我该如何解决这个问题?
确保使用最新的 Azure Cosmos DB Spark 连接器。
为您使用的 Apache Spark 版本下载最新的 azure-cosmosdb-spark 库 运行:
Spark 2.4:azure-cosmosdb-spark_2.4.0_2.11-2.1.2-uber.jar
Spark 2.3:azure-cosmosdb-spark_2.3.0_2.11-1.2.2-uber.jar
Spark 2.2:azure-cosmosdb-spark_2.2.0_2.11-1.1.1-uber.jar
按照上传 Jar、Python Egg 或 Python Wheel 中的说明将下载的 JAR 文件上传到 Databricks。
将上传的库安装到您的 Databricks 集群中。
回答我自己的问题。
使用 MAVEN 作为源,我使用路径
将正确的库安装到我的集群org.mongodb.spark:mongo-spark-connector_2.11:2.4.0
Spark 2.4
我使用的代码示例如下(给想尝试的人):
# Read Configuration
readConfig = {
"URI": "<URI>",
"Database": "<database>",
"Collection": "<collection>",
"ReadingBatchSize" : "<batchSize>"
}
pipelineAccounts = "{'$sort' : {'account_contact': 1}}"
# Connect via azure-cosmosdb-spark to create Spark DataFrame
accountsTest = (spark.read.
format("com.mongodb.spark.sql").
options(**readConfig).
option("pipeline", pipelineAccounts).
load())
accountsTest.select("account_id").show()