This repository has been archived by the owner on Mar 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathDefaultSource.scala
152 lines (131 loc) · 5.62 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
/*******************************************************************************
* Copyright (c) 2015 IBM Corp.
*
* 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.cloudant.spark
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.Row
import org.apache.spark.sql.types._
import org.apache.spark.sql.sources._
import org.apache.spark.sql.DataFrame
import org.apache.spark.sql.SaveMode
import org.apache.spark.SparkEnv
import com.cloudant.spark.common._
import org.slf4j.LoggerFactory
/**
* @author yanglei
*/
case class CloudantReadWriteRelation (config:CloudantConfig, schema: StructType, allDocsDF: DataFrame = null)
(@transient val sqlContext: SQLContext)
extends BaseRelation with PrunedFilteredScan with InsertableRelation
{
@transient lazy val dataAccess = {new JsonStoreDataAccess(config)}
implicit lazy val logger = LoggerFactory.getLogger(getClass)
def buildScan(requiredColumns: Array[String],
filters: Array[Filter]): RDD[Row] = {
val colsLength = requiredColumns.length
if (allDocsDF != null) {
if (colsLength == 0) {
allDocsDF.select().rdd
} else if (colsLength == 1) {
allDocsDF.select(requiredColumns(0)).rdd
} else {
val colsExceptCol0 = for (i <- 1 until colsLength) yield requiredColumns(i)
allDocsDF.select(requiredColumns(0), colsExceptCol0: _*).rdd
}
} else {
val filterInterpreter = new FilterInterpreter(filters)
var searchField:String = {
if (filterInterpreter.containsFiltersFor(config.pkField)) config.pkField
else filterInterpreter.firstField
}
val (min, minInclusive, max, maxInclusive) = filterInterpreter.getInfo(searchField)
implicit val columns = requiredColumns
val (url: String, pusheddown: Boolean) = config.getRangeUrl(searchField,
min, minInclusive, max, maxInclusive, false)
if (!pusheddown) searchField = null
implicit val attrToFilters = filterInterpreter.getFiltersForPostProcess(searchField)
val cloudantRDD = new JsonStoreRDD(sqlContext.sparkContext,config,url)
val df = sqlContext.read.json(cloudantRDD)
if (colsLength > 1) {
val colsExceptCol0 = for (i <- 1 until colsLength) yield requiredColumns(i)
df.select(requiredColumns(0), colsExceptCol0: _*).rdd
} else {
df.rdd
}
}
}
def insert( data:DataFrame, overwrite: Boolean) ={
if (config.getCreateDBonSave()) {
dataAccess.createDB()
}
if (data.count() == 0){
logger.warn(("Database " + config.getDbname() +
": nothing was saved because the number of records was 0!"))
} else {
val result = data.toJSON.foreachPartition { x =>
val list = x.toList // Has to pass as List, Iterator results in 0 data
dataAccess.saveAll(list)
}
}
}
}
/**
* @author yanglei
*/
class DefaultSource extends RelationProvider with CreatableRelationProvider with SchemaRelationProvider{
val logger = LoggerFactory.getLogger(getClass)
def createRelation(sqlContext: SQLContext, parameters: Map[String, String]) = {
create(sqlContext, parameters, null)
}
private def create(sqlContext: SQLContext, parameters: Map[String, String], inSchema: StructType) = {
val config: CloudantConfig = JsonStoreConfigManager.getConfig(sqlContext, parameters)
var allDocsDF: DataFrame = null
val schema: StructType = {
if (inSchema!=null)
inSchema
else{
val df = if (config.getSchemaSampleSize() == JsonStoreConfigManager.SCHEMA_FOR_ALL_DOCS_NUM
&& config.viewName == null && config.indexName == null) {
val filterInterpreter = new FilterInterpreter(null)
var searchField = null
val (min, minInclusive, max, maxInclusive) =
filterInterpreter.getInfo(searchField)
val (url: String, pusheddown: Boolean) = config.getRangeUrl(searchField,
min, minInclusive, max, maxInclusive, false)
val cloudantRDD = new JsonStoreRDD(sqlContext.sparkContext,config, url)
allDocsDF = sqlContext.read.json(cloudantRDD)
allDocsDF
} else {
val dataAccess = new JsonStoreDataAccess(config)
val aRDD = sqlContext.sparkContext.parallelize(
dataAccess.getMany(config.getSchemaSampleSize()))
sqlContext.read.json(aRDD)
}
df.schema
}
}
CloudantReadWriteRelation(config, schema, allDocsDF)(sqlContext)
}
def createRelation(sqlContext:SQLContext,mode:SaveMode, parameters:Map[String,String], data:DataFrame) =
{
val relation =create(sqlContext, parameters, data.schema)
relation.insert(data, mode==SaveMode.Overwrite)
relation
}
def createRelation(sqlContext:SQLContext, parameters:Map[String,String], schema: StructType) ={
create(sqlContext, parameters, schema)
}
}