/
MongoConnector.scala
244 lines (220 loc) · 8.97 KB
/
MongoConnector.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright 2016 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.spark
import java.io.{Closeable, Serializable}
import java.util
import java.util.concurrent.TimeUnit
import scala.collection.JavaConverters._
import scala.concurrent.duration.Duration
import scala.reflect.ClassTag
import org.apache.spark.api.java.JavaSparkContext
import org.apache.spark.api.java.function.Function
import org.apache.spark.{SparkConf, SparkContext}
import org.bson.codecs.configuration.CodecRegistry
import org.bson.{BsonDocument, Document}
import com.mongodb.client.{MongoClient, MongoCollection, MongoDatabase}
import com.mongodb.connection.ServerVersion
import com.mongodb.spark.config.{MongoCollectionConfig, ReadConfig, WriteConfig}
import com.mongodb.spark.connection.{DefaultMongoClientFactory, MongoClientCache}
/**
* The MongoConnector companion object
*
* @since 1.0
*/
object MongoConnector {
/**
* Creates a MongoConnector using the [[com.mongodb.spark.config.ReadConfig.mongoURIProperty]] from the `sparkConf`.
*
* @param sparkContext the Spark context
* @return the MongoConnector
*/
def apply(sparkContext: SparkContext): MongoConnector = apply(sparkContext.getConf)
/**
* Creates a MongoConnector using the [[com.mongodb.spark.config.ReadConfig]] from the `sparkConf`.
*
* @param sparkConf the Spark configuration.
* @return the MongoConnector
*/
def apply(sparkConf: SparkConf): MongoConnector = apply(ReadConfig(sparkConf).asOptions)
/**
* Creates a MongoConnector
*
* @param readConfig the readConfig
* @return the MongoConnector
*/
def apply(readConfig: ReadConfig): MongoConnector = apply(readConfig.asOptions)
/**
* Creates a MongoConnector
*
* @param options the configuration options
* @return the MongoConnector
*/
def apply(options: collection.Map[String, String]): MongoConnector = new MongoConnector(DefaultMongoClientFactory(options))
/**
* Creates a MongoConnector using the [[com.mongodb.spark.config.ReadConfig]] from the `sparkConf`.
*
* @param javaSparkContext the Java Spark context
* @return the MongoConnector
*/
def create(javaSparkContext: JavaSparkContext): MongoConnector = {
notNull("javaSparkContext", javaSparkContext)
MongoConnector(javaSparkContext.sc)
}
/**
* Creates a MongoConnector using the [[com.mongodb.spark.config.ReadConfig]] from the `sparkConf`.
*
* @param sparkConf the spark configuration
* @return the MongoConnector
*/
def create(sparkConf: SparkConf): MongoConnector = {
notNull("sparkConf", sparkConf)
apply(sparkConf)
}
/**
* Creates a MongoConnector
*
* @param options the configuration options
* @return the MongoConnector
*/
def create(options: java.util.Map[String, String]): MongoConnector = {
notNull("options", options)
apply(options.asScala)
}
/**
* Creates a MongoConnector
*
* @param mongoClientFactory the factory for creating the MongoClient
* @return the MongoConnector
*/
def create(mongoClientFactory: MongoClientFactory): MongoConnector = {
notNull("mongoClientFactory", mongoClientFactory)
MongoConnector(mongoClientFactory)
}
private[spark] val mongoClientKeepAlive = Duration(System.getProperty(
"mongodb.keep_alive_ms",
System.getProperty("spark.mongodb.keep_alive_ms", "5000")
).toInt, TimeUnit.MILLISECONDS)
private val mongoClientCache = new MongoClientCache(mongoClientKeepAlive)
Runtime.getRuntime.addShutdownHook(new Thread(new Runnable {
def run() {
mongoClientCache.shutdown()
}
}))
}
/**
* The MongoConnector
*
* Connects Spark to MongoDB
*
* @param mongoClientFactory the factory that can be used to create a MongoClient
* @since 1.0
*/
case class MongoConnector(mongoClientFactory: MongoClientFactory)
extends Logging with Serializable with Closeable {
/**
* Execute some code on a `MongoClient`
*
* '''Note:''' The MongoClient is reference counted and loaned to the `code` method and should only be used inside that function.
*
* @param code the code block that is passed
* @tparam T the result of the code function
* @return the result
*/
def withMongoClientDo[T](code: MongoClient => T): T = {
val client = acquireClient()
try {
code(client)
} finally {
releaseClient(client)
}
}
/**
* Execute some code on a database
*
* '''Note:''' The `MongoDatabase` is reference counted and loaned to the `code` method and should only be used inside that function.
*
* @param config the [[com.mongodb.spark.config.MongoCollectionConfig]] determining which database to connect to
* @param code the code block that is executed
* @tparam T the result of the code function
* @return the result
*/
def withDatabaseDo[T](config: MongoCollectionConfig, code: MongoDatabase => T): T =
withMongoClientDo({ client => code(client.getDatabase(config.databaseName)) })
/**
* Execute some code on a collection
*
* '''Note:''' The `MongoCollection` is reference counted and loaned to the `code` method and should only be used inside that function.
*
* @param config the [[com.mongodb.spark.config.MongoCollectionConfig]] determining which database and collection to connect to
* @param code the code block that is executed
* @tparam T the result of the code function
* @return the result
*/
def withCollectionDo[D, T](config: MongoCollectionConfig, code: MongoCollection[D] => T)(implicit ct: ClassTag[D]): T =
withDatabaseDo(config, { db =>
val collection = db.getCollection[D](config.collectionName, classTagToClassOf(ct))
code(config match {
case writeConfig: WriteConfig => collection.withWriteConcern(writeConfig.writeConcern)
case readConfig: ReadConfig => collection.withReadConcern(readConfig.readConcern).withReadPreference(readConfig.readPreference)
case _ => collection
})
})
/**
* A Java friendly way to execute some code on a `MongoClient`
*
* '''Note:''' The MongoClient is reference counted and loaned to the `code` method and should only be used inside that function.
*
* @param code the code block that is passed
* @tparam T the result of the code function
* @return the result
*/
def withMongoClientDo[T](code: Function[MongoClient, T]): T = withMongoClientDo[T]({ client => code.call(client) })
/**
* A Java friendly way to execute some code on a database
*
* '''Note:''' The `MongoDatabase` is reference counted and loaned to the `code` method and should only be used inside that function.
*
* @param config the [[com.mongodb.spark.config.MongoCollectionConfig]] determining which database to connect to
* @param code the code block that is executed
* @tparam T the result of the code function
* @return the result
*/
def withDatabaseDo[T](config: MongoCollectionConfig, code: Function[MongoDatabase, T]): T =
withDatabaseDo[T](config, { mongoDatabase => code.call(mongoDatabase) })
/**
* A Java friendly way to execute some code on a collection
*
* '''Note:''' The `MongoCollection` is reference counted and loaned to the `code` method and should only be used inside that function.
*
* @param config the [[com.mongodb.spark.config.MongoCollectionConfig]] determining which database and collection to connect to
* @param clazz the class representing documents from the collection
* @param code the code block that is executed
* @tparam T the result of the code function
* @return the result
*/
def withCollectionDo[D, T](config: MongoCollectionConfig, clazz: Class[D], code: Function[MongoCollection[D], T]): T = {
implicit def ct: ClassTag[D] = ClassTag(clazz)
withCollectionDo[D, T](config, { collection => code.call(collection) })
}
private[spark] def hasSampleAggregateOperator(readConfig: ReadConfig): Boolean = {
val buildInfo: BsonDocument = withDatabaseDo(readConfig, { db => db.runCommand(new Document("buildInfo", 1), classOf[BsonDocument]) })
val versionArray: util.List[Integer] = buildInfo.getArray("versionArray").asScala.take(3).map(_.asInt32().getValue.asInstanceOf[Integer]).asJava
new ServerVersion(versionArray).compareTo(new ServerVersion(3, 2)) >= 0
}
private[spark] def acquireClient(): MongoClient = MongoConnector.mongoClientCache.acquire(mongoClientFactory)
private[spark] def releaseClient(client: MongoClient): Unit = MongoConnector.mongoClientCache.release(client)
override def close(): Unit = MongoConnector.mongoClientCache.shutdown()
}