-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cosmos spark config infra
- Loading branch information
Showing
6 changed files
with
169 additions
and
15 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
100 changes: 100 additions & 0 deletions
100
...smos/azure-cosmos-spark_3-0_2-12/src/main/scala/com/azure/cosmos/spark/CosmosConfig.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,100 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.cosmos.spark | ||
|
||
import java.net.URL | ||
import java.util.Locale | ||
|
||
|
||
// each config category will be a case class: | ||
// TODO moderakh more configs | ||
//case class ClientConfig() | ||
//case class CosmosBatchWriteConfig() | ||
|
||
case class CosmosAccountConfig(endpoint: String, key: String) | ||
|
||
object CosmosAccountConfig { | ||
val CosmosAccountEndpointUri = CosmosConfigEntry[String](key = "spark.cosmos.accountEndpoint", | ||
mandatory = true, | ||
parseFromStringFunction = accountEndpointUri => { | ||
new URL(accountEndpointUri) | ||
accountEndpointUri | ||
}, | ||
helpMessage = "Cosmos DB Account Endpoint Uri") | ||
|
||
val CosmosKey = CosmosConfigEntry[String](key = "spark.cosmos.accountKey", | ||
mandatory = true, | ||
parseFromStringFunction = accountEndpointUri => accountEndpointUri, | ||
helpMessage = "Cosmos DB Account Key") | ||
|
||
def parseCosmosAccountConfig(cfg: Map[String, String]): CosmosAccountConfig = { | ||
val endpointOpt = CosmosConfigEntry.parse(cfg, CosmosAccountEndpointUri) | ||
val key = CosmosConfigEntry.parse(cfg, CosmosKey) | ||
|
||
// parsing above already validated these assertions | ||
assert(endpointOpt.isDefined) | ||
assert(key.isDefined) | ||
|
||
CosmosAccountConfig(endpointOpt.get, key.get) | ||
} | ||
} | ||
|
||
case class CosmosContainerConfig(database: String, container: String) | ||
|
||
object CosmosContainerConfig { | ||
val databaseName = CosmosConfigEntry[String](key = "spark.cosmos.database", | ||
mandatory = true, | ||
parseFromStringFunction = database => database, | ||
helpMessage = "Cosmos DB database name") | ||
|
||
val containerName = CosmosConfigEntry[String](key = "spark.cosmos.container", | ||
mandatory = true, | ||
parseFromStringFunction = container => container, | ||
helpMessage = "Cosmos DB container name") | ||
|
||
def parseCosmosContainerConfig(cfg: Map[String, String]): CosmosContainerConfig = { | ||
val databaseOpt = CosmosConfigEntry.parse(cfg, databaseName) | ||
val containerOpt = CosmosConfigEntry.parse(cfg, containerName) | ||
|
||
// parsing above already validated this | ||
assert(databaseOpt.isDefined) | ||
assert(containerOpt.isDefined) | ||
|
||
CosmosContainerConfig(databaseOpt.get, containerOpt.get) | ||
} | ||
} | ||
|
||
case class CosmosConfigEntry[T](key: String, | ||
mandatory: Boolean, | ||
defaultValue: Option[String] = Option.empty, | ||
parseFromStringFunction: String => T, | ||
helpMessage: String) { | ||
|
||
def parse(paramAsString: String) : T = { | ||
try { | ||
parseFromStringFunction(paramAsString) | ||
} catch { | ||
case e: Exception => throw new RuntimeException(s"invalid configuration for ${key}:${paramAsString}. Config description: ${helpMessage}", e) | ||
} | ||
} | ||
} | ||
|
||
// TODO: moderakh how to merge user config with SparkConf application config? | ||
object CosmosConfigEntry { | ||
def parse[T](configuration: Map[String, String], configEntry: CosmosConfigEntry[T]): Option[T] = { | ||
// TODO moderakh: where should we handle case sensitivity? | ||
// we are doing this here per config parsing for now | ||
val opt = configuration.map { case (key, value) => (key.toLowerCase(Locale.ROOT), value) }.get(configEntry.key.toLowerCase(Locale.ROOT)) | ||
if (opt.isDefined) { | ||
Option.apply(configEntry.parse(opt.get)) | ||
} | ||
else { | ||
if (configEntry.mandatory) { | ||
throw new RuntimeException(s"mandatory option ${configEntry.key} is missing. Config description: ${configEntry.helpMessage}") | ||
} else { | ||
Option.empty | ||
} | ||
} | ||
} | ||
} |
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
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
53 changes: 53 additions & 0 deletions
53
.../azure-cosmos-spark_3-0_2-12/src/test/scala/com/azure/cosmos/spark/CosmosConfigSpec.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,53 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
package com.azure.cosmos.spark | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
|
||
class CosmosConfigSpec extends UnitSpec { | ||
//scalastyle:off multiple.string.literals | ||
|
||
"account endpoint" should "be parsed" in { | ||
val userConfig = Map( | ||
"spark.cosmos.accountEndpoint" -> "https://localhsot:8081", | ||
"spark.cosmos.accountKey" -> "xyz" | ||
) | ||
|
||
val endpointConfig = CosmosAccountConfig.parseCosmosAccountConfig(userConfig) | ||
|
||
assertThat(endpointConfig.endpoint).isEqualTo( "https://localhsot:8081") | ||
assertThat(endpointConfig.key).isEqualTo( "xyz") | ||
} | ||
|
||
"account endpoint" should "be validated" in { | ||
val userConfig = Map( | ||
"spark.cosmos.accountEndpoint" -> "invalidUrl", | ||
"spark.cosmos.accountKey" -> "xyz" | ||
) | ||
|
||
try { | ||
CosmosAccountConfig.parseCosmosAccountConfig(userConfig) | ||
fail("invalid URL") | ||
} catch { | ||
case e: Exception => assertThat(e.getMessage).isEqualTo( | ||
"invalid configuration for spark.cosmos.accountEndpoint:invalidUrl." + | ||
" Config description: Cosmos DB Account Endpoint Uri") | ||
} | ||
} | ||
|
||
"account endpoint" should "mandatory config" in { | ||
val userConfig = Map( | ||
"spark.cosmos.accountKey" -> "xyz" | ||
) | ||
|
||
try { | ||
CosmosAccountConfig.parseCosmosAccountConfig(userConfig) | ||
fail("missing URL") | ||
} catch { | ||
case e: Exception => assertThat(e.getMessage).isEqualTo( | ||
"mandatory option spark.cosmos.accountEndpoint is missing." + | ||
" Config description: Cosmos DB Account Endpoint Uri") | ||
} | ||
} | ||
//scalastyle:on multiple.string.literals | ||
} |