|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.sources |
| 19 | + |
| 20 | +import org.apache.spark.sql.SQLContext |
| 21 | +import org.apache.spark.sql.catalyst.types.{StructType, StructField} |
| 22 | +import org.apache.spark.sql.execution.RunnableCommand |
| 23 | +import org.apache.spark.util.Utils |
| 24 | + |
| 25 | +private[sql] case class CreateTableUsing( |
| 26 | + tableName: String, |
| 27 | + tableCols: Seq[StructField], |
| 28 | + provider: String, |
| 29 | + options: Map[String, String]) extends RunnableCommand { |
| 30 | + |
| 31 | + def run(sqlContext: SQLContext) = { |
| 32 | + val loader = Utils.getContextOrSparkClassLoader |
| 33 | + val clazz: Class[_] = try loader.loadClass(provider) catch { |
| 34 | + case cnf: java.lang.ClassNotFoundException => |
| 35 | + try loader.loadClass(provider + ".DefaultSource") catch { |
| 36 | + case cnf: java.lang.ClassNotFoundException => |
| 37 | + sys.error(s"Failed to load class for data source: $provider") |
| 38 | + } |
| 39 | + } |
| 40 | + val relation = clazz.newInstance match { |
| 41 | + case dataSource: org.apache.spark.sql.sources.RelationProvider => |
| 42 | + dataSource |
| 43 | + .asInstanceOf[org.apache.spark.sql.sources.RelationProvider] |
| 44 | + .createRelation(sqlContext, new CaseInsensitiveMap(options)) |
| 45 | + case dataSource: org.apache.spark.sql.sources.SchemaRelationProvider => |
| 46 | + if(tableCols.isEmpty) { |
| 47 | + dataSource |
| 48 | + .asInstanceOf[org.apache.spark.sql.sources.SchemaRelationProvider] |
| 49 | + .createRelation(sqlContext, new CaseInsensitiveMap(options)) |
| 50 | + } else { |
| 51 | + dataSource |
| 52 | + .asInstanceOf[org.apache.spark.sql.sources.SchemaRelationProvider] |
| 53 | + .createRelation( |
| 54 | + sqlContext, new CaseInsensitiveMap(options), Some(StructType(tableCols))) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + sqlContext.baseRelationToSchemaRDD(relation).registerTempTable(tableName) |
| 59 | + Seq.empty |
| 60 | + } |
| 61 | +} |
0 commit comments