-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chen Dai <[email protected]>
- Loading branch information
Showing
9 changed files
with
199 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...ntegration/src/main/scala/org/opensearch/flint/spark/FlintSparkIndexMetadataBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.spark | ||
|
||
import scala.collection.JavaConverters.mapAsJavaMapConverter | ||
|
||
import org.json4s.JObject | ||
import org.opensearch.flint.core.metadata.FlintMetadata | ||
import org.opensearch.flint.spark.FlintSparkIndex.populateEnvToMetadata | ||
|
||
import org.apache.spark.sql.flint.datatype.FlintDataType | ||
import org.apache.spark.sql.types.StructType | ||
|
||
/** | ||
* Flint Spark metadata builder with common build logic. | ||
*/ | ||
class FlintSparkIndexMetadataBuilder(index: FlintSparkIndex) extends FlintMetadata.Builder { | ||
|
||
def schema(allFieldTypes: Map[String, String]): FlintSparkIndexMetadataBuilder = { | ||
val catalogDDL = | ||
allFieldTypes | ||
.map { case (colName, colType) => s"$colName $colType not null" } | ||
.mkString(",") | ||
val struckType = StructType.fromDDL(catalogDDL) | ||
|
||
// Assume each value is an JSON Object | ||
struckType.fields.foreach(field => { | ||
val (fieldName, fieldType) = FlintDataType.serializeField(field) | ||
val fieldTypeMap = | ||
fieldType | ||
.asInstanceOf[JObject] | ||
.values | ||
.mapValues { | ||
case v: Map[_, _] => v.asJava | ||
case other => other | ||
} | ||
.asJava | ||
addSchemaField(fieldName, fieldTypeMap) | ||
}) | ||
this | ||
} | ||
|
||
override def build(): FlintMetadata = { | ||
// Common fields in all Flint Spark index | ||
kind(index.kind) | ||
name(index.name()) | ||
options(index.options.options.mapValues(_.asInstanceOf[AnyRef]).asJava) | ||
|
||
val envs = populateEnvToMetadata | ||
if (envs.nonEmpty) { | ||
addProperty("env", envs.asJava) | ||
} | ||
|
||
val settings = index.options.indexSettings() | ||
if (settings.isDefined) { | ||
indexSettings(settings.get) | ||
} | ||
super.build() | ||
} | ||
} | ||
|
||
object FlintSparkIndexMetadataBuilder { | ||
|
||
def builder(index: FlintSparkIndex): FlintSparkIndexMetadataBuilder = | ||
new FlintSparkIndexMetadataBuilder(index) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.