-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update python-default template to use presets: catalog/schema
- Loading branch information
1 parent
4236e71
commit 922fb37
Showing
9 changed files
with
216 additions
and
41 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
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
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
34 changes: 26 additions & 8 deletions
34
...te/templates/default-python/template/{{.project_name}}/src/{{.project_name}}/main.py.tmpl
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 |
---|---|---|
@@ -1,21 +1,39 @@ | ||
from pyspark.sql import SparkSession, DataFrame | ||
import argparse | ||
|
||
def get_taxis(spark: SparkSession) -> DataFrame: | ||
return spark.read.table("samples.nyctaxi.trips") | ||
|
||
|
||
# Create a new Databricks Connect session. If this fails, | ||
# check that you have configured Databricks Connect correctly. | ||
# See https://docs.databricks.com/dev-tools/databricks-connect.html. | ||
def get_spark() -> SparkSession: | ||
""" | ||
Create a new Databricks Connect session. If this fails, | ||
check that you have configured Databricks Connect correctly. | ||
See https://docs.databricks.com/dev-tools/databricks-connect.html. | ||
""" | ||
try: | ||
from databricks.connect import DatabricksSession | ||
return DatabricksSession.builder.getOrCreate() | ||
except ImportError: | ||
return SparkSession.builder.getOrCreate() | ||
|
||
def get_taxis(spark: SparkSession) -> DataFrame: | ||
return spark.read.table("samples.nyctaxi.trips") | ||
|
||
def create_example_table(): | ||
""" | ||
Create a table called 'example' in the default catalog and schema. | ||
""" | ||
get_spark().sql("CREATE OR REPLACE TABLE example AS SELECT 'example table' AS text_column") | ||
|
||
def main(): | ||
get_taxis(get_spark()).show(5) | ||
# Set the catalog and schema for the current session. | ||
# In the default template, these parameters are set | ||
# using the 'catalog' and 'schema' presets in databricks.yml. | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--catalog', required=True) | ||
parser.add_argument('--schema', required=True) | ||
args, unknown = parser.parse_known_args() | ||
spark = get_spark() | ||
spark.sql(f"USE {args.catalog}.{args.schema}") | ||
|
||
create_example_table() | ||
|
||
if __name__ == '__main__': | ||
main() |
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