先贴一段报错 java.lang.IllegalArgumentException: Invalid BSON field name creaImpcntat org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:494)at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:188)at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:131)at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)at com.mongodb.connection.UpdateCommandMessage.writeTheWrites(UpdateCommandMessage.java:84)at com.mongodb.connection.UpdateCommandMessage.writeTheWrites(UpdateCommandMessage.java:42)at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBodyWithMetadata(BaseWriteCommandMessage.java:129)at com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160)at com.mongodb.connection.WriteCommandProtocol.sendMessage(WriteCommandProtocol.java:212)at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:101)at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:64)at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:37)at com.mongodb.connection.DefaultServer D e f a u l t S e r v e r P r o t o c o l E x e c u t o r . e x e c u t e ( D e f a u l t S e r v e r . j a v a : 159 ) a t c o m . m o n g o d b . c o n n e c t i o n . D e f a u l t S e r v e r C o n n e c t i o n . e x e c u t e P r o t o c o l ( D e f a u l t S e r v e r C o n n e c t i o n . j a v a : 286 ) a t c o m . m o n g o d b . c o n n e c t i o n . D e f a u l t S e r v e r C o n n e c t i o n . u p d a t e C o m m a n d ( D e f a u l t S e r v e r C o n n e c t i o n . j a v a : 140 ) a t c o m . m o n g o d b . o p e r a t i o n . M i x e d B u l k W r i t e O p e r a t i o n DefaultServerProtocolExecutor.execute(DefaultServer.java:159)at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)at com.mongodb.connection.DefaultServerConnection.updateCommand(DefaultServerConnection.java:140)at com.mongodb.operation.MixedBulkWriteOperation DefaultServerProtocolExecutor.execute(DefaultServer.java:159)atcom.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)atcom.mongodb.connection.DefaultServerConnection.updateCommand(DefaultServerConnection.java:140)atcom.mongodb.operation.MixedBulkWriteOperationRun 3. e x e c u t e W r i t e C o m m a n d P r o t o c o l ( M i x e d B u l k W r i t e O p e r a t i o n . j a v a : 480 ) a t c o m . m o n g o d b . o p e r a t i o n . M i x e d B u l k W r i t e O p e r a t i o n 3.executeWriteCommandProtocol(MixedBulkWriteOperation.java:480)at com.mongodb.operation.MixedBulkWriteOperation 3.executeWriteCommandProtocol(MixedBulkWriteOperation.java:480)atcom.mongodb.operation.MixedBulkWriteOperationRun R u n E x e c u t o r . e x e c u t e ( M i x e d B u l k W r i t e O p e r a t i o n . j a v a : 646 ) a t c o m . m o n g o d b . o p e r a t i o n . M i x e d B u l k W r i t e O p e r a t i o n RunExecutor.execute(MixedBulkWriteOperation.java:646)at com.mongodb.operation.MixedBulkWriteOperation RunExecutor.execute(MixedBulkWriteOperation.java:646)atcom.mongodb.operation.MixedBulkWriteOperationRun.execute(MixedBulkWriteOperation.java:399)at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:179)at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:168)at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:230)at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221)at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:168)at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:74)at com.mongodb.Mongo.execute(Mongo.java:781)at com.mongodb.Mongo 2. e x e c u t e ( M o n g o . j a v a : 764 ) a t c o m . m o n g o d b . M o n g o C o l l e c t i o n I m p l . e x e c u t e S i n g l e W r i t e R e q u e s t ( M o n g o C o l l e c t i o n I m p l . j a v a : 515 ) a t c o m . m o n g o d b . M o n g o C o l l e c t i o n I m p l . u p d a t e ( M o n g o C o l l e c t i o n I m p l . j a v a : 508 ) a t c o m . m o n g o d b . M o n g o C o l l e c t i o n I m p l . u p d a t e O n e ( M o n g o C o l l e c t i o n I m p l . j a v a : 355 ) a t c o m . m o n g o d b . M o n g o C o l l e c t i o n I m p l . u p d a t e O n e ( M o n g o C o l l e c t i o n I m p l . j a v a : 350 ) a t c o m . y e a h m o b i . b i g d a t a . a n a l y s i s . d s p . d w . G a t e W a y W i n A n d I m p r e s s S t a t i c s A l i 2.execute(Mongo.java:764)at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:515)at com.mongodb.MongoCollectionImpl.update(MongoCollectionImpl.java:508)at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:355)at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:350)at com.yeahmobi.bigdata.analysis.dsp.dw.GateWayWinAndImpressStatics_Ali 2.execute(Mongo.java:764)atcom.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:515)atcom.mongodb.MongoCollectionImpl.update(MongoCollectionImpl.java:508)atcom.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:355)atcom.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:350)atcom.yeahmobi.bigdata.analysis.dsp.dw.GateWayWinAndImpressStaticsAli a n o n f u n anonfun anonfunwriteToMongo 1 1 1 a n o n f u n anonfun anonfunapply 1. a p p l y ( G a t e W a y W i n A n d I m p r e s s S t a t i c s A l i . s c a l a : 301 ) a t c o m . y e a h m o b i . b i g d a t a . a n a l y s i s . d s p . d w . G a t e W a y W i n A n d I m p r e s s S t a t i c s A l i 1.apply(GateWayWinAndImpressStatics_Ali.scala:301)at com.yeahmobi.bigdata.analysis.dsp.dw.GateWayWinAndImpressStatics_Ali 1.apply(GateWayWinAndImpressStaticsAli.scala:301)atcom.yeahmobi.bigdata.analysis.dsp.dw.GateWayWinAndImpressStaticsAli a n o n f u n anonfun anonfunwriteToMongo 1 1 1 a n o n f u n anonfun anonfunapply 1. a p p l y ( G a t e W a y W i n A n d I m p r e s s S t a t i c s A l i . s c a l a : 301 ) a t c o m . m o n g o d b . s p a r k . M o n g o C o n n e c t o r 1.apply(GateWayWinAndImpressStatics_Ali.scala:301)at com.mongodb.spark.MongoConnector 1.apply(GateWayWinAndImpressStaticsAli.scala:301)atcom.mongodb.spark.MongoConnector a n o n f u n anonfun anonfunwithCollectionDo 1. a p p l y ( M o n g o C o n n e c t o r . s c a l a : 186 ) a t c o m . m o n g o d b . s p a r k . M o n g o C o n n e c t o r 1.apply(MongoConnector.scala:186)at com.mongodb.spark.MongoConnector 1.apply(MongoConnector.scala:186)atcom.mongodb.spark.MongoConnector a n o n f u n anonfun anonfunwithCollectionDo 1. a p p l y ( M o n g o C o n n e c t o r . s c a l a : 184 ) a t c o m . m o n g o d b . s p a r k . M o n g o C o n n e c t o r 1.apply(MongoConnector.scala:184)at com.mongodb.spark.MongoConnector 1.apply(MongoConnector.scala:184)atcom.mongodb.spark.MongoConnector a n o n f u n anonfun anonfunwithDatabaseDo 1. a p p l y ( M o n g o C o n n e c t o r . s c a l a : 171 ) a t c o m . m o n g o d b . s p a r k . M o n g o C o n n e c t o r 1.apply(MongoConnector.scala:171)at com.mongodb.spark.MongoConnector 1.apply(MongoConnector.scala:171)atcom.mongodb.spark.MongoConnector a n o n f u n anonfun anonfunwithDatabaseDo 1. a p p l y ( M o n g o C o n n e c t o r . s c a l a : 171 ) a t c o m . m o n g o d b . s p a r k . M o n g o C o n n e c t o r . w i t h M o n g o C l i e n t D o ( M o n g o C o n n e c t o r . s c a l a : 154 ) a t c o m . m o n g o d b . s p a r k . M o n g o C o n n e c t o r . w i t h D a t a b a s e D o ( M o n g o C o n n e c t o r . s c a l a : 171 ) a t c o m . m o n g o d b . s p a r k . M o n g o C o n n e c t o r . w i t h C o l l e c t i o n D o ( M o n g o C o n n e c t o r . s c a l a : 184 ) a t c o m . y e a h m o b i . b i g d a t a . a n a l y s i s . d s p . d w . G a t e W a y W i n A n d I m p r e s s S t a t i c s A l i 1.apply(MongoConnector.scala:171)at com.mongodb.spark.MongoConnector.withMongoClientDo(MongoConnector.scala:154)at com.mongodb.spark.MongoConnector.withDatabaseDo(MongoConnector.scala:171)at com.mongodb.spark.MongoConnector.withCollectionDo(MongoConnector.scala:184)at com.yeahmobi.bigdata.analysis.dsp.dw.GateWayWinAndImpressStatics_Ali 1.apply(MongoConnector.scala:171)atcom.mongodb.spark.MongoConnector.withMongoClientDo(MongoConnector.scala:154)atcom.mongodb.spark.MongoConnector.withDatabaseDo(MongoConnector.scala:171)atcom.mongodb.spark.MongoConnector.withCollectionDo(MongoConnector.scala:184)atcom.yeahmobi.bigdata.analysis.dsp.dw.GateWayWinAndImpressStaticsAli a n o n f u n anonfun anonfunwriteToMongo 1. a p p l y ( G a t e W a y W i n A n d I m p r e s s S t a t i c s A l i . s c a l a : 300 ) a t c o m . y e a h m o b i . b i g d a t a . a n a l y s i s . d s p . d w . G a t e W a y W i n A n d I m p r e s s S t a t i c s A l i 1.apply(GateWayWinAndImpressStatics_Ali.scala:300)at com.yeahmobi.bigdata.analysis.dsp.dw.GateWayWinAndImpressStatics_Ali 1.apply(GateWayWinAndImpressStaticsAli.scala:300)atcom.yeahmobi.bigdata.analysis.dsp.dw.GateWayWinAndImpressStaticsAli a n o n f u n anonfun anonfunwriteToMongo$1.apply(GateWayWinAndImpressStatics_Ali.scala:279)
造成这个原因是用mongoClinet updateone时候没有 加$set
代码如下:
val dateFormat=new SimpleDateFormat("yyyyMMdd")
val ttlTime: Date = dateFormat.parse(updateDate)
finalDf.rdd.repartition(100).foreach(itm => {
var bsons = new util.ArrayList[Bson]()
bsons.add(Filters.eq("app",itm(0)))
bsons.add(Filters.eq("day",itm(1)))
bsons.add(Filters.eq("hour",itm(2)))
bsons.add(Filters.eq("adx",itm(3)))
bsons.add(Filters.eq("os",itm(4)))
bsons.add(Filters.eq("osv",itm(5)))
bsons.add(Filters.eq("impType",itm(6)))
bsons.add(Filters.eq("country",itm(7)))
bsons.add(Filters.eq("updateDate",updateDate))
val InsertOne = new Document("app", itm(0)).append("day", itm(1)).append("hour", itm(2)).append("adx", itm(3))
.append("os", itm(4)).append("osv", itm(5)).append("impType",itm(6)).append("country", itm(7)).append("ttlTime",ttlTime)
mongoConn.withCollectionDo(new WriteConfig(databaseName,
ResultCol, Option(MongoConn)), { col: MongoCollection[Document] => col.updateOne(Filters.and(bsons),InsertOne) })
错误主要在:
mongoConn.withCollectionDo(new WriteConfig(databaseName,
ResultCol, Option(MongoConn)), { col: MongoCollection[Document] => col.updateOne(Filters.and(bsons),InsertOne) })
修改后 给InsertOne加上$set修改后的代码为
mongoConn.withCollectionDo(new WriteConfig(databaseName,
ResultCol, Option(MongoConn)), { col: MongoCollection[Document] => col.updateOne(Filters.and(bsons),new Document("$set",updateOne),new UpdateOptions().upsert(true)) })
错误解决!