Einfaches Beispiel für die Verwendung von Snowpark Scala

Das folgende Beispiel gibt die Anzahl und die Namen der Tabellen in der aktuellen Datenbank aus. Ersetzen Sie die <Platzhalter> durch Werte, die Sie zur Verbindung mit Snowflake verwenden.

import com.snowflake.snowpark._
import com.snowflake.snowpark.functions._

object Main {
  def main(args: Array[String]) {

    // Create a Session, specifying the properties used to
    // connect to the Snowflake database.
    val builder = Session.builder.configs(Map(
      "URL" -> "https://<account_identifier>.snowflakecomputing.com",
      "USER" -> "<username>",
      "PASSWORD" -> "<password>",
      "ROLE" -> "<role_name_with_access_to_public_schema>",
      "WAREHOUSE" -> "<warehouse_name>",
      "DB" -> "<database_name>",
      "SCHEMA" -> "<schema_name>"
    ))
    val session = builder.create

    // Get the number of tables in the PUBLIC schema.
    var dfTables = session.table("INFORMATION_SCHEMA.TABLES").filter(col("TABLE_SCHEMA") === "PUBLIC")
    var tableCount = dfTables.count()
    var currentDb = session.getCurrentDatabase.getOrElse("<no current database>")
    println(s"Number of tables in the PUBLIC schema in the $currentDb database: $tableCount")

    // Get the list of table names in the PUBLIC schema.
    var dfPublicSchemaTables = dfTables.select(col("TABLE_NAME"))
    dfPublicSchemaTables.show()
  }
}
Copy

Dies gibt die Anzahl der Tabellen und die Liste der Tabellen im Schema aus:

Number of tables in the PUBLIC schema in the "MY_DB" database: 8
...
---------------------
|"TABLE_NAME"       |
---------------------
|A_TABLE            |
...
Copy