spark mongo Cannot cast STRING into a NullType (value: BsonString{value=‘fQhjMW8erDc0njFI‘}
spark read mongo 报:Cannot cast STRING into a NullType (value: BsonString{value='fQhjMW8erDc0njFI'}
使用
val mongoURL = s"mongodb://name:password@host1:port1,host2:port2/库.collection?authSource=认证库"
val spark = SparkSession.builder()
.appName("UserDetail")
.master("local[1]")
.config("spark.mongodb.input.uri", mongoURL)
.getOrCreate()
import spark.implicits._
MongoSpark.load(spark).toDF()
补充:
还是会偶尔出现这样的情况,很不稳定。后改为客户端方式连接,代码如下:
val mongoClient = MongoClients.create(s"mongodb://userName:password@node1:port,node2:port,node3:port/库名?authSource=库名")
val db = mongoClient.getDatabase("库名")
db.getCollection("集合名")
.find()
.projection(new Document("f1", 1).append("f2", 1).append("f3", 1)) //需要查询的列f1,f2,f3
.foreach(f => {
if (f != null) {
val f1= f.getInteger("f1")
val f2= f.getInteger("f2")
val f3= f.getInteger("f3")
}
})
更多推荐
所有评论(0)