forked from elastic/elasticsearch
/
DefaultSource.scala
492 lines (438 loc) · 21 KB
/
DefaultSource.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package org.elasticsearch.spark.sql
import java.util.Calendar
import java.util.Date
import java.util.Locale
import scala.collection.JavaConverters.mapAsJavaMapConverter
import scala.collection.mutable.LinkedHashMap
import scala.collection.mutable.LinkedHashSet
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.DataFrame
import org.apache.spark.sql.Row
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.SaveMode
import org.apache.spark.sql.SaveMode.Append
import org.apache.spark.sql.SaveMode.ErrorIfExists
import org.apache.spark.sql.SaveMode.Ignore
import org.apache.spark.sql.SaveMode.Overwrite
import org.apache.spark.sql.sources.And
import org.apache.spark.sql.sources.BaseRelation
import org.apache.spark.sql.sources.CreatableRelationProvider
import org.apache.spark.sql.sources.EqualTo
import org.apache.spark.sql.sources.Filter
import org.apache.spark.sql.sources.GreaterThan
import org.apache.spark.sql.sources.GreaterThanOrEqual
import org.apache.spark.sql.sources.In
import org.apache.spark.sql.sources.InsertableRelation
import org.apache.spark.sql.sources.IsNotNull
import org.apache.spark.sql.sources.IsNull
import org.apache.spark.sql.sources.LessThan
import org.apache.spark.sql.sources.LessThanOrEqual
import org.apache.spark.sql.sources.Not
import org.apache.spark.sql.sources.Or
import org.apache.spark.sql.sources.PrunedFilteredScan
import org.apache.spark.sql.sources.RelationProvider
import org.apache.spark.sql.sources.SchemaRelationProvider
import org.apache.spark.sql.types.DateType
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.types.TimestampType
import org.elasticsearch.hadoop.EsHadoopIllegalArgumentException
import org.elasticsearch.hadoop.EsHadoopIllegalStateException
import org.elasticsearch.hadoop.cfg.ConfigurationOptions
import org.elasticsearch.hadoop.cfg.InternalConfigurationOptions
import org.elasticsearch.hadoop.rest.{InitializationUtils, RestClient, RestRepository}
import org.elasticsearch.hadoop.serialization.builder.JdkValueWriter
import org.elasticsearch.hadoop.serialization.json.JacksonJsonGenerator
import org.elasticsearch.hadoop.util.FastByteArrayOutputStream
import org.elasticsearch.hadoop.util.IOUtils
import org.elasticsearch.hadoop.util.SettingsUtils
import org.elasticsearch.hadoop.util.StringUtils
import org.elasticsearch.hadoop.util.Version
import org.elasticsearch.spark.cfg.SparkSettingsManager
import org.elasticsearch.spark.serialization.ScalaValueWriter
import javax.xml.bind.DatatypeConverter
import org.elasticsearch.hadoop.serialization.field.ConstantFieldExtractor
private[sql] class DefaultSource extends RelationProvider with SchemaRelationProvider with CreatableRelationProvider {
Version.logVersion()
override def createRelation(@transient sqlContext: SQLContext, parameters: Map[String, String]): BaseRelation = {
ElasticsearchRelation(params(parameters), sqlContext)
}
override def createRelation(@transient sqlContext: SQLContext, parameters: Map[String, String], schema: StructType): BaseRelation = {
ElasticsearchRelation(params(parameters), sqlContext, Some(schema))
}
override def createRelation(@transient sqlContext: SQLContext, mode: SaveMode, parameters: Map[String, String], data: DataFrame): BaseRelation = {
val relation = ElasticsearchRelation(params(parameters), sqlContext, Some(data.schema))
mode match {
case Append => relation.insert(data, false)
case Overwrite => relation.insert(data, true)
case ErrorIfExists => {
if (relation.isEmpty()) relation.insert(data, false)
else throw new EsHadoopIllegalStateException(s"SaveMode is set to ErrorIfExists and " +
s"index ${relation.cfg.getResourceWrite} exists and contains data. Consider changing the SaveMode")
}
case Ignore => if (relation.isEmpty()) { relation.insert(data, false) }
}
relation
}
private def params(parameters: Map[String, String]) = {
// '.' seems to be problematic when specifying the options
val params = parameters.map { case (k, v) => (k.replace('_', '.'), v)}. map { case (k, v) =>
if (k.startsWith("es.")) (k, v)
else if (k == "path") (ConfigurationOptions.ES_RESOURCE, v)
else if (k == "pushdown") (Utils.DATA_SOURCE_PUSH_DOWN, v)
else if (k == "strict") (Utils.DATA_SOURCE_PUSH_DOWN_STRICT, v)
else if (k == "double.filtering") (Utils.DATA_SOURCE_KEEP_HANDLED_FILTERS, v)
else ("es." + k, v)
}
// validate path
params.getOrElse(ConfigurationOptions.ES_RESOURCE_READ,
params.getOrElse(ConfigurationOptions.ES_RESOURCE, throw new EsHadoopIllegalArgumentException("resource must be specified for Elasticsearch resources.")))
params
}
}
private[sql] case class ElasticsearchRelation(parameters: Map[String, String], @transient val sqlContext: SQLContext, userSchema: Option[StructType] = None)
extends BaseRelation with PrunedFilteredScan with InsertableRelation
{
@transient lazy val cfg = { new SparkSettingsManager().load(sqlContext.sparkContext.getConf).merge(parameters.asJava) }
@transient lazy val lazySchema = { SchemaUtils.discoverMapping(cfg) }
@transient lazy val valueWriter = { new ScalaValueWriter }
override def schema = userSchema.getOrElse(lazySchema.struct)
// TableScan
def buildScan(): RDD[Row] = buildScan(Array.empty)
// PrunedScan
def buildScan(requiredColumns: Array[String]): RDD[Row] = buildScan(requiredColumns, Array.empty)
// PrunedFilteredScan
def buildScan(requiredColumns: Array[String], filters: Array[Filter]): RDD[Row] = {
val paramWithScan = LinkedHashMap[String, String]() ++ parameters
var filteredColumns = requiredColumns
// scroll fields only apply to source fields; handle metadata separately
if (cfg.getReadMetadata) {
val metadata = cfg.getReadMetadataField
// if metadata is not selected, don't ask for it
if (!requiredColumns.contains(metadata)) {
paramWithScan += (ConfigurationOptions.ES_READ_METADATA -> false.toString())
}
else {
filteredColumns = requiredColumns.filter( _ != metadata)
}
}
paramWithScan += (InternalConfigurationOptions.INTERNAL_ES_TARGET_FIELDS ->
StringUtils.concatenate(filteredColumns.asInstanceOf[Array[Object]], StringUtils.DEFAULT_DELIMITER))
if (filters != null && filters.size > 0) {
if (Utils.isPushDown(cfg)) {
if (Utils.LOGGER.isDebugEnabled()) {
Utils.LOGGER.debug(s"Pushing down filters ${filters.mkString("[", ",", "]")}")
}
val filterString = createDSLFromFilters(filters, Utils.isPushDownStrict(cfg), SettingsUtils.isEs50(cfg))
if (Utils.LOGGER.isTraceEnabled()) {
Utils.LOGGER.trace(s"Transformed filters into DSL ${filterString.mkString("[", ",", "]")}")
}
paramWithScan += (InternalConfigurationOptions.INTERNAL_ES_QUERY_FILTERS -> IOUtils.serializeToBase64(filterString))
}
else {
if (Utils.LOGGER.isTraceEnabled()) {
Utils.LOGGER.trace("Push-down is disabled; ignoring Spark filters...")
}
}
}
new ScalaEsRowRDD(sqlContext.sparkContext, paramWithScan, lazySchema)
}
// introduced in Spark 1.6
override def unhandledFilters(filters: Array[Filter]): Array[Filter] = {
if (Utils.isKeepHandledFilters(cfg) || filters == null || filters.size == 0)
return filters;
// walk the filters (things like And / Or) and see whether we recognize all of them
// if we do, skip the filter, otherwise let it in there even though we might push some of it
def unhandled(filter: Filter): Boolean = {
filter match {
case EqualTo(_, _) => false
case GreaterThan(_, _) => false
case GreaterThanOrEqual(_, _) => false
case LessThan(_, _) => false
case LessThanOrEqual(_, _) => false
// In is problematic - see translate, don't filter it
case In(_, _) => true
case IsNull(_) => false
case IsNotNull(_) => false
case And(left, right) => unhandled(left) || unhandled(right)
case Or(left, right) => unhandled(left) || unhandled(right)
case Not(pred) => unhandled(pred)
// Spark 1.3.1+
case f:Product if isClass(f, "org.apache.spark.sql.sources.StringStartsWith") => false
case f:Product if isClass(f, "org.apache.spark.sql.sources.StringEndsWith") => false
case f:Product if isClass(f, "org.apache.spark.sql.sources.StringContains") => false
// Spark 1.5+
case f:Product if isClass(f, "org.apache.spark.sql.sources.EqualNullSafe") => false
// unknown
case _ => true
}
}
val filtered = filters.filter(unhandled)
if (Utils.LOGGER.isTraceEnabled()) {
Utils.LOGGER.trace(s"Unhandled filters from ${filters.mkString("[", ",", "]")} to ${filtered.mkString("[", ",", "]")}")
}
filtered
}
private def createDSLFromFilters(filters: Array[Filter], strictPushDown: Boolean, isES50: Boolean) = {
filters.map(filter => translateFilter(filter, strictPushDown, isES50)).filter(query => StringUtils.hasText(query))
}
// string interpolation FTW
private def translateFilter(filter: Filter, strictPushDown: Boolean, isES50: Boolean):String = {
// the pushdown can be strict - i.e. use only filters and thus match the value exactly (works with non-analyzed)
// or non-strict meaning queries will be used instead that is the filters will be analyzed as well
filter match {
case EqualTo(attribute, value) => {
// if we get a null, translate it into a missing query (we're extra careful - Spark should translate the equals into isMissing anyway)
if (value == null || value == None || value == Unit) {
if (isES50) {
s"""{"bool":{"must_not":{"exists":{"field":"$attribute"}}}}"""
}
else {
s"""{"missing":{"field":"$attribute"}}"""
}
}
if (strictPushDown) s"""{"term":{"$attribute":${extract(value)}}}"""
else {
if (isES50) {
s"""{"match":{"$attribute":${extract(value)}}}"""
}
else {
s"""{"query":{"match":{"$attribute":${extract(value)}}}}"""
}
}
}
case GreaterThan(attribute, value) => s"""{"range":{"$attribute":{"gt" :${extract(value)}}}}"""
case GreaterThanOrEqual(attribute, value) => s"""{"range":{"$attribute":{"gte":${extract(value)}}}}"""
case LessThan(attribute, value) => s"""{"range":{"$attribute":{"lt" :${extract(value)}}}}"""
case LessThanOrEqual(attribute, value) => s"""{"range":{"$attribute":{"lte":${extract(value)}}}}"""
case In(attribute, values) => {
// when dealing with mixed types (strings and numbers) Spark converts the Strings to null (gets confused by the type field)
// this leads to incorrect query DSL hence why nulls are filtered
val filtered = values filter (_ != null)
if (filtered.isEmpty) {
return ""
}
// further more, match query only makes sense with String types so for other types apply a terms query (aka strictPushDown)
val attrType = lazySchema.struct(attribute).dataType
val isStrictType = attrType match {
case DateType |
TimestampType => true
case _ => false
}
if (!strictPushDown && isStrictType) {
if (Utils.LOGGER.isDebugEnabled()) {
Utils.LOGGER.debug(s"Attribute $attribute type $attrType not suitable for match query; using terms (strict) instead")
}
}
if (strictPushDown || isStrictType) s"""{"terms":{"$attribute":${extractAsJsonArray(filtered)}}}"""
else {
if (isES50) {
s"""{"bool":{"should":[${extractMatchArray(attribute, filtered)}]}}"""
}
else {
s"""{"or":{"filters":[${extractMatchArray(attribute, filtered)}]}}"""
}
}
}
case IsNull(attribute) => {
if (isES50) {
s"""{"bool":{"must_not":{"exists":{"field":"$attribute"}}}}"""
}
else {
s"""{"missing":{"field":"$attribute"}}"""
}
}
case IsNotNull(attribute) => s"""{"exists":{"field":"$attribute"}}"""
case And(left, right) => {
if (isES50) {
s"""{"bool":{"filter":[${translateFilter(left, strictPushDown, isES50)}, ${translateFilter(right, strictPushDown, isES50)}]}}"""
}
else {
s"""{"and":{"filters":[${translateFilter(left, strictPushDown, isES50)}, ${translateFilter(right, strictPushDown, isES50)}]}}"""
}
}
case Or(left, right) => {
if (isES50) {
s"""{"bool":{"should":[{"bool":{"filter":${translateFilter(left, strictPushDown, isES50)}}}, {"bool":{"filter":${translateFilter(right, strictPushDown, isES50)}}}]}}"""
}
else {
s"""{"or":{"filters":[${translateFilter(left, strictPushDown, isES50)}, ${translateFilter(right, strictPushDown, isES50)}]}}"""
}
}
case Not(filterToNeg) => {
if (isES50) {
s"""{"bool":{"must_not":${translateFilter(filterToNeg, strictPushDown, isES50)}}}"""
}
else {
s"""{"not":{"filter":${translateFilter(filterToNeg, strictPushDown, isES50)}}}"""
}
}
// the filter below are available only from Spark 1.3.1 (not 1.3.0)
//
// String Filter notes:
//
// the DSL will be quite slow (linear to the number of terms in the index) but there's no easy way around them
// we could use regexp filter however it's a bit overkill and there are plenty of chars to escape
// s"""{"regexp":{"$attribute":"$value.*"}}"""
// as an alternative we could use a query string but still, the analyzed / non-analyzed is there as the DSL is slightly more complicated
// s"""{"query":{"query_string":{"default_field":"$attribute","query":"$value*"}}}"""
// instead wildcard query is used, with the value lowercased (to match analyzed fields)
case f:Product if isClass(f, "org.apache.spark.sql.sources.StringStartsWith") => {
var arg = f.productElement(1).toString()
if (!strictPushDown) { arg = arg.toLowerCase(Locale.ROOT) }
if (isES50) {
s"""{"wildcard":{"${f.productElement(0)}":"$arg*"}}"""
}
else {
s"""{"query":{"wildcard":{"${f.productElement(0)}":"$arg*"}}}"""
}
}
case f:Product if isClass(f, "org.apache.spark.sql.sources.StringEndsWith") => {
var arg = f.productElement(1).toString()
if (!strictPushDown) { arg = arg.toLowerCase(Locale.ROOT) }
if (isES50) {
s"""{"wildcard":{"${f.productElement(0)}":"*$arg"}}"""
}
else {
s"""{"query":{"wildcard":{"${f.productElement(0)}":"*$arg"}}}"""
}
}
case f:Product if isClass(f, "org.apache.spark.sql.sources.StringContains") => {
var arg = f.productElement(1).toString()
if (!strictPushDown) { arg = arg.toLowerCase(Locale.ROOT) }
if (isES50) {
s"""{"wildcard":{"${f.productElement(0)}":"*$arg*"}}"""
}
else {
s"""{"query":{"wildcard":{"${f.productElement(0)}":"*$arg*"}}}"""
}
}
// the filters below are available only from Spark 1.5.0
case f:Product if isClass(f, "org.apache.spark.sql.sources.EqualNullSafe") => {
var arg = extract(f.productElement(1))
if (strictPushDown) s"""{"term":{"${f.productElement(0)}":$arg}}"""
else {
if (isES50) {
s"""{"match":{"${f.productElement(0)}":$arg}}"""
}
else {
s"""{"query":{"match":{"${f.productElement(0)}":$arg}}}"""
}
}
}
case _ => ""
}
}
private def isClass(obj: Any, className: String) = {
className.equals(obj.getClass().getName())
}
private def extract(value: Any):String = {
extract(value, true, false)
}
private def extractAsJsonArray(value: Any):String = {
extract(value, true, true)
}
private def extractMatchArray(attribute: String, ar: Array[Any]):String = {
// use a set to avoid duplicate values
// especially since Spark conversion might turn each user param into null
val numbers = LinkedHashSet.empty[AnyRef]
val strings = LinkedHashSet.empty[AnyRef]
// move numbers into a separate list for a terms query combined with a bool
for (i <- ar) i.asInstanceOf[AnyRef] match {
case null => // ignore
case n:Number => numbers += extract(i, false, false)
case _ => strings += extract(i, false, false)
}
if (numbers.isEmpty) {
if (strings.isEmpty) {
return StringUtils.EMPTY
}
return {
if (SettingsUtils.isEs50(cfg)) {
s"""{"match":{"$attribute":${strings.mkString("\"", " ", "\"")}}}"""
}
else {
s"""{"query":{"match":{"$attribute":${strings.mkString("\"", " ", "\"")}}}}"""
}
}
//s"""{"query":{"$attribute":${strings.mkString("\"", " ", "\"")}}}"""
}
else {
// translate the numbers into a terms query
val str = s"""{"terms":{"$attribute":${numbers.mkString("[", ",", "]")}}}"""
if (strings.isEmpty) return str
// if needed, add the strings as a match query
else return str +
{
if (SettingsUtils.isEs50(cfg)) {
s""",{"match":{"$attribute":${strings.mkString("\"", " ", "\"")}}}"""
}
else {
s""",{"query":{"match":{"$attribute":${strings.mkString("\"", " ", "\"")}}}}"""
}
}
}
}
private def extract(value: Any, inJsonFormat: Boolean, asJsonArray: Boolean):String = {
// common-case implies primitives and String so try these before using the full-blown ValueWriter
value match {
case null => "null"
case u: Unit => "null"
case b: Boolean => b.toString
case by: Byte => by.toString
case s: Short => s.toString
case i: Int => i.toString
case l: Long => l.toString
case f: Float => f.toString
case d: Double => d.toString
case bd: BigDecimal => bd.toString
case _: Char |
_: String |
_: Array[Byte] => if (inJsonFormat) StringUtils.toJsonString(value.toString) else value.toString()
// handle Timestamp also
case dt: Date => {
val cal = Calendar.getInstance()
cal.setTime(dt)
val str = DatatypeConverter.printDateTime(cal)
if (inJsonFormat) StringUtils.toJsonString(str) else str
}
case ar: Array[Any] =>
if (asJsonArray) (for (i <- ar) yield extract(i, true, false)).distinct.mkString("[", ",", "]")
else (for (i <- ar) yield extract(i, false, false)).distinct.mkString("\"", " ", "\"")
// new in Spark 1.4
case utf if (isClass(utf, "org.apache.spark.sql.types.UTF8String")
// new in Spark 1.5
|| isClass(utf, "org.apache.spark.unsafe.types.UTF8String"))
=> if (inJsonFormat) StringUtils.toJsonString(utf.toString()) else utf.toString()
case a: AnyRef => {
val storage = new FastByteArrayOutputStream()
val generator = new JacksonJsonGenerator(storage)
valueWriter.write(a, generator)
generator.flush()
generator.close()
storage.toString()
}
}
}
def insert(data: DataFrame, overwrite: Boolean) {
if (overwrite) {
Utils.LOGGER.info(s"Overwriting data for ${cfg.getResourceWrite}")
// perform a scan-scroll delete
val cfgCopy = cfg.copy()
InitializationUtils.setValueWriterIfNotSet(cfgCopy, classOf[JdkValueWriter], null)
InitializationUtils.setFieldExtractorIfNotSet(cfgCopy, classOf[ConstantFieldExtractor], null) //throw away extractor
cfgCopy.setProperty(ConfigurationOptions.ES_BATCH_FLUSH_MANUAL, "false")
cfgCopy.setProperty(ConfigurationOptions.ES_BATCH_SIZE_ENTRIES, "1000")
cfgCopy.setProperty(ConfigurationOptions.ES_BATCH_SIZE_BYTES, "1mb")
val rr = new RestRepository(cfgCopy)
rr.delete()
rr.close()
}
EsSparkSQL.saveToEs(data, parameters)
}
def isEmpty(): Boolean = {
val rr = new RestRepository(cfg)
val empty = rr.isEmpty(true)
rr.close()
empty
}
}