简体   繁体   English

Mongo Spark 连接器和 mongo 3.2,root 用户无法读取数据库

[英]Mongo Spark connector and mongo 3.2, root user cannot read database

I use the official mongo spark connector.我使用官方的 mongo spark 连接器。

  • my spark version is 2.0我的火花版本是 2.0
  • my mongo version is 3.2.x我的 mongo 版本是 3.2.x
  • my spark mongo connector is 1.1.0我的 spark mongo 连接器是 1.1.0

On my database i have one admin with root role, so he has all right.在我的数据库上,我有一个具有 root 角色的管理员,所以他没事。

i have created a config as follows :我创建了一个配置如下:

     val readConfig = ReadConfig(Map("spark.mongodb.auth.uri" -> "mongodb://<userName>:<password>@<ip>:27017/admin",
"spark.mongodb.input.uri" -> "mongodb://<ip>:27017/MyDatabase.myCollection"))

but when i try to read some data i get an error "not authorized to execute command."但是当我尝试读取一些数据时,我收到一个错误“无权执行命令”。

i don't understand why my root user is not authorized.我不明白为什么我的 root 用户没有被授权。

It's because "spark.mongodb.auth.uri" is not a configuration setting.这是因为"spark.mongodb.auth.uri"不是配置设置。

As the input uri doesn't have the authentication parameters the read is not authorised.由于输入 uri 没有身份验证参数,因此未授权读取。

Try:尝试:

 val readConfig = ReadConfig(Map(
     "uri" -> "mongodb://<userName>:<password>@<ip>:27017/myDatabase.myCollection?authSource=admin"))

or:或者:

 val readConfig = ReadConfig(Map(
     "uri" -> "mongodb://<userName>:<password>@<ip>:27017",  // uses the default db to auth against (admin)
     "database" -> "myDatabase",
     "collection" -> "myCollection"))

To avoid a full scan, you can do as below:为避免全盘扫描,您可以执行以下操作:

val rdd = MongoSpark.load(sc)

val aggregatedRdd = rdd.withPipeline(Seq(Document.parse("{ $match: { test : { $gt : 5 } } }")))
println(aggregatedRdd.count)
println(aggregatedRdd.first.toJson)
Find below implementation for Spark SQL Mongo Spark connector for admin DB having authentication:
1] Mongo uri:- mongodb://userid:pw@ip:port
2] sparkSessionCtx = SparkSession
  .builder()      
  .appName(appName)
  .config("spark.mongodb.input.uri", uri+ "/" + dbName + "." + collName+ "?authSource=admin")
  .config("spark.mongodb.output.uri", uri+ "/" + dbName + "." + collName+ "?authSource=admin")
  .getOrCreate()
3] val readConfiguration = ReadConfig(Map("database" -> dbName, "collection" -> collectionName, "readPreference.name" -> "primaryPreferred" ), Some(ReadConfig(sparkSessionCtx)))
mongodataframe = MongoSpark.load(sparkSessionCtx , readConfiguration)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM