All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mongodb.spark.sql.MongoRelation.scala Maven / Gradle / Ivy

There is a newer version: 2.2.10
Show newest version
/*
 * 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.sql

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.sources._
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.{Row, SQLContext}

import org.bson.BsonDocument
import com.mongodb.spark.Logging
import com.mongodb.spark.config.ReadConfig
import com.mongodb.spark.rdd.MongoRDD
import com.mongodb.spark.sql.MapFunctions.documentToRow
import com.mongodb.spark.sql.MongoRelationHelper.createPipeline

case class MongoRelation(mongoRDD: MongoRDD[BsonDocument], readConfig: ReadConfig, _schema: Option[StructType])(@transient val sqlContext: SQLContext)
    extends BaseRelation
    with PrunedFilteredScan
    with Logging {

  override lazy val schema: StructType = _schema.getOrElse(MongoInferSchema(sqlContext.sparkContext))

  override def buildScan(requiredColumns: Array[String], filters: Array[Filter]): RDD[Row] = {
    if (requiredColumns.nonEmpty || filters.nonEmpty) {
      logInfo(s"requiredColumns: ${requiredColumns.mkString(", ")}, filters: ${filters.mkString(", ")}")
    }
    mongoRDD.appendPipeline(createPipeline(requiredColumns, filters)).map(doc => documentToRow(doc, schema, requiredColumns))
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy