import io.delta.tables.DeltaTable import net.snowflake.spark.snowflake.Utils.runQuery import org.apache.spark.sql.avro.functions.from_avro import org.apache.spark.sql.DataFrame import org.apache.spark.sql.expressions.Window import org.apache.spark.sql.functions._ import org.apache.spark.sql.streaming.OutputMode import org.apache.spark.sql.streaming.Trigger import org.apache.spark.sql.types.StructType import org.apache.spark.sql.types.DateType import net.snowflake.spark.snowflake.Utils.SNOWFLAKE_SOURCE_NAME import com.databricks.dbutils_v1.DBUtilsHolder.dbutils import org.apache.spark.sql.SQLContext ; import org.apache.spark.SparkConf; class TestNotebook(){ dbutils.widgets.removeAll() dbutils.widgets.dropdown("environment", "dev", Seq("dev","stg","prd") ,"Environment") dbutils.widgets.text("state", "", "State") dbutils.widgets.text("database", "", "Database") val storageLocation = spark.conf.get("spark.databricks.workspaceUrl") match { case "adb-6993636295605502.2.azuredatabricks.net" => "abfss://[email protected]" case "adb-2159805908649335.15.azuredatabricks.net" => "abfss://[email protected]" case "adb-7454574069878794.14.azuredatabricks.net" => "abfss://[email protected]" } val environment = dbutils.widgets.get("environment") val database = dbutils.widgets.get("database") val state = dbutils.widgets.get("state") val schema = s"OCN_${state}" val sfAccount = environment match { case "stg" => "snowflakeStg" case "dev" => "snowflakeDev" case "prd" => "snowflakePrd" } val sfWarehouse = environment match { case "stg" => "OCDP_STG_STREAM_WH" case "dev" => "OCDP_DEV_STREAM_WH" case "prd" => "OCDP_PRD_STREAM_WH" } var tenantSK = state.toUpperCase() match { case "OH" => "7" case "CT" => "24" case "IN" => "9" case "NY" => "8" case "OR" => "10" } val snowflakeOptions = Map( "sfUrl" -> dbutils.secrets.get(sfAccount, "sfUrl"), "sfUser" -> dbutils.secrets.get(sfAccount, "sfUser"), "pem_private_key" -> dbutils.secrets.get(sfAccount, "pem_private_key"), "sfDatabase" -> database, "sfSchema" -> schema, "sfWarehouse" -> sfWarehouse ) var tableName = s"ORG_HIERARCHY_${state}" val dcount = sqlContext.sql(s""" SELECT COUNT(*) as count FROM delta.`${storageLocation}/delta/bronze/${database}/${schema}/ORG_HIERARCHY_CT_PREDQ` WHERE TIMESTAMP = (SELECT MAX(TIMESTAMP) FROM delta.`${storageLocation}/delta/bronze/${database}/${schema}/ORG_HIERARCHY_CT_PREDQ`) GROUP BY FILE_NAME """); var m = dcount.select("count").as[String].collect.head var count = m.toInt val query =s"""update "${database}"."OCN_${state}"."ROW_COUNT_${state}" as t set t.DELTA_TABLE_COUNT = """+lit(count)+s""" from ( select max(timestamp) as time from "${database}"."OCN_${state}"."ROW_COUNT_${state}") t2 where t.timestamp = t2.time""" runQuery(snowflakeOptions, query) }
Write, Run & Share Scala code online using OneCompiler's Scala online compiler for free. It's one of the robust, feature-rich online compilers for Scala language, running on the latest version 2.13.8. Getting started with the OneCompiler's Scala compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Scala
and start coding.
OneCompiler's Scala online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Scala program which takes name as input and prints hello message with your name.
object Hello {
def main(args: Array[String]): Unit = {
val name = scala.io.StdIn.readLine() // Read input from STDIN
println("Hello " + name )
}
}
Scala is both object-oriented and functional programming language by Martin Odersky in the year 2003.
Variable is a name given to the storage area in order to identify them in our programs.
var or val Variable-name [: Data-Type] = [Initial Value];
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression){
//code
}
if(conditional-expression) {
//code if condition is true
} else {
//code if condition is false
}
if(condition-expression1) {
//code if above condition is true
} else if (condition-expression2) {
//code if above condition is true
}
else if(condition-expression3) {
//code if above condition is true
}
...
else {
//code if all the above conditions are false
}
For loop is used to iterate a set of statements based on a criteria.
for(index <- range){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition)
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
def functionname(parameters : parameters-type) : returntype = { //code
}
You can either use =
or not in the function definition. If =
is not present, function will not return any value.