Snowflake Snowpark API for Python: Master Class with Project
- Description
- Curriculum
- FAQ
- Reviews
Welcome to the Snowflake Snowpark API for Python: Master Class with Project! We are thrilled to have you on board for this exciting and comprehensive course. Get ready to embark on a learning journey that will empower you with the skills and knowledge to master Snowflake’s Snowpark API and apply it in real-world Python projects.
What You Can Expect:
In this course, you will dive deep into the Snowflake Snowpark API, unlocking its full potential through hands-on exercises, real-life examples, and a challenging project that will test and enhance your skills. Whether you are a beginner or an experienced Python developer, this course is designed to cater to all levels of expertise. By the end of this course, you will have a strong grasp of Snowflake Snowpark API concepts and the ability to create efficient and scalable Python applications using Snowflake.
Course Highlights:
-
Comprehensive Learning: Understand the Snowflake Snowpark API from its basics to advanced topics, ensuring a solid foundation for your skills.
-
Hands-on Experience: Dive into practical exercises and a stimulating project that will reinforce your learning and boost your confidence.
We are excited to have you in this course and can’t wait to see what amazing projects you will create using the Snowflake Snowpark API for Python. Buckle up and get ready for an enriching learning experience!
If you have any questions or need assistance, please don’t hesitate to reach out. Your success is our priority.
Happy learning!
-
1Know Your InstructorText lesson
-
2Understanding Snowflake Snowpark API For PythonText lesson
-
3Creating an account on SnowflakeVideo lesson
-
4Setting up Snowflake Snowpark Environment on Local MachineVideo lesson
You need to complete the initial setup scripts such as :
Python Environment Setup with Snowflake Snowpark using Anaconda and Microsoft Visual Studio Code
Snowflake Trial Account and Creation of Database with Notification Integration
You can download the course material from the external resources URL. I have attached a PDF Slide Deck and the Code Repository Link
-
5Basics of Snowflake Snowpark API and Connectivity SetupQuiz
Let's get a refresher on the Snowflake Snowpark API for Python and on setting up the local environment.
-
6Getting Started With Snowpark Session Method with Builder AttributeVideo lesson
Snowpark Library uses Python Connector to connect to Snowflake. This is done through the snowpark session object.
To establish the snowflake connection, you require a Session Object. You must provide connection parameters to connect with a Snowflake database.
Builder attribute of session object allows to set configurations and connectivity with snowflake
The important thing to note here is that the session object is not a thread-safe object
-
7Adding Imports and Packages to Snowpark SessionVideo lesson
Snowpark allows you to register a local or stage file as an import inside a user-defined function
The files will not be immediately gets uploaded to the snowflake stage, until and unless you register your UDF.
Adding two files with the same file name is also not allowed.
Snowpark session also allows you to add third-party packages as dependencies of a UDF. This can be done for all the Python packages available in Snowflake.
There are two ways in which you can add packages, either using the add_packages function or the add_requirement function.
-
8Getting and Setting Snowflake Session ConfigurationsVideo lesson
Returns the name of the current account for the Python connector session attached to this session
Returns the name of the current database for the Python connector session attached to this session
Returns the name of the primary role in use for the current session
Returns the name of the current schema for the Python connector session attached to this session
Returns the name of the warehouse in use for the current session
Returns the fully qualified name of the current schema for the session
Returns a list of imports added for user defined functions (UDFs). This list includes any Python or zip files that were added automatically by the library
Returns a dict of packages added for user-defined functions (UDFs). The key of this dict is the package name and the value of this dict is the corresponding requirement specifier
Returns the name of the temporary stage created by the Snowpark library for uploading and storing temporary artifacts for this session
Specifies the active/current database for the session
Specifies the active/current primary role for the session
Specifies the active/current schema for the session
Specifies the active/current secondary roles for the session. The currently-active secondary roles set the context that determines whether the current user has the necessary privileges to perform SQL actions
Specifies the active/current warehouse for the session
-
9Session Cleanup ProcessVideo lesson
While you have Session methods to add imports or third-party packages, Snowflake Snowpark API also provides the methods to remove individual or clear all the packages or imports. Also, You can terminate the entire session using Session.close()
Session.remove_import() - Removes a file in stage or local file from the imports of a user-defined function|
Session.remove_package() - Removes a third-party package from the dependency list of a user-defined function|
Session.clear() - Removes a file in stage or local file from the imports of a user-defined function|
Session.clear_imports() - Clears all files in a stage or local files from the imports of a user-defined function|
Session.clear_packages() - Clears all files in a stage or local files from the packages of a user-defined function|
Session.close() - Terminate the snowflake Session|
-
10Snowflake Snowpark Session Method QuizQuiz
Refresher quiz on understanding Snowpark Session Method, establishing the Connectivity with Snowflake using Snowpark Session Builder and setting up the session-level configuration for your snowpark coding journey
-
11Understanding Different Ways to Read Data Using Snowpark Session MethodsVideo lesson
There are different ways you can interact with snowflake to fetch the data from the tables or use a SQL syntax. Below are ways to read the data:
session.sql():
returns a new DataFrame representing the results of a SQL query.
You can use this method to execute an SQL statement.
Note that you still need to call DataFrame.collect() to execute this query in Snowflake.
for binding parameters. it only supports qmark bind variables.
session.table():
Returns a Table that points to the specified table.
If your table name contains special characters, use double quotes to mark it like this, session.table('"my table"'). For fully qualified names, you need to use double quotes separately like in this, session.table('"my db"."my schema"."my.table"').
session.table_function():
Creates a new DataFrame from the given snowflake SQL table function.
session.call() :
Calls a stored procedure by name.
sproc_name – The name of the stored procedure in Snowflake.
args – Arguments should be basic Python types.
statement_params – Dictionary of statement level parameters to be set while executing this action.
log_on_exception – Log warnings if they arise when trying to determine if the stored procedure as a table return type.
-
12Creating Dataframe for Query Results Along with Learning Misc. Snowpark MethodsVideo lesson
-
13Snowflake Snowpark Session for Accessing Data QuizQuiz
-
14Accessing and Loading Data using Snowpark DataFrameReader ClassVideo lesson
Provides methods to load data in various supported formats from a Snowflake stage to a DataFrame.
The paths provided to the DataFrameReader must refer to Snowflake stages.
Access an instance of a DataFrameReader by using the Session.read property for using this object
Specify any format-specific options and copy options by calling the option() or options() method.
These methods return a DataFrameReader that is configured with these options.
Specify the schema of the data that you plan to load by constructing a types.StructType object and passing it to the schema() method if the file format is CSV.
Except CSV, Other file formats such as JSON, XML, Parquet, ORC, and AVRO don’t accept a schema.
If you want to load only a subset of files from the stage, you can use the pattern option to specify a regular expression that matches the files that you want to load.
To load Parquet, ORC and AVRO files, no schema is accepted because the schema will be automatically inferred.
Inferring the schema can be disabled by setting option “infer_schema” to False.
Loading JSON and XML files doesn’t support schema either.
You also need to use $1 to access the column data as an OBJECT.
-
15Writing Data Output Destination using Snowpark DataFrameWriter ClassVideo lesson
DataFrameWriter
Provides methods for writing data from a DataFrame to supported output destinations.
We can create an instance of a DataFrameWriter by accessing the DataFrame.write property.
we can Specify the save mode by calling mode(), which can take append, overwrite, errorifexists and ignore
then we can Call save_as_table() or copy_into_location() to save the data to the specified destination.
-
16Exploring Various File Operations using Snowflake Snowpark API for PythonVideo lesson
FileOperations
Provides methods for working on files in a stage.
To access an object of this class, use Session.file.
get - Downloads the specified files from a path in a stage to a local directory.
get_stream - Downloads the specified files from a path in a stage and exposes it through a stream.
put - Uploads local files to the stage.
put_stream - Uploads local files to the stage via a file stream.
The put_stream uses the file/data already in memory using a BytesIO object and then writes the object to the internal stage. Where the PUT saves the physical file on the disk and then writes the file to the internal stage
-
17Snowflake Snowpark Input-Output Operations QuizQuiz
-
18Performing Various Table Operations Using Snowpark Table ClassVideo lesson
Snowflake Snowpark Table
Represents a lazily-evaluated Table.
It extends DataFrame so all DataFrame operations can be applied to it.
You can create a Table object by calling Session.table() with the name of the table in Snowflake.
It supports various Methods:
delete()
drop_table()
merge()
update()
-
19Snowflake Snowpark Table Operations QuizQuiz
-
20Performing Various Column Level Operations Using Snowpark Column ClassVideo lesson
Snowflake Snowpark Column
Represents a column or an expression in a DataFrame.
Snowflake object identifiers, including column names, may or may not be case sensitive depending on a set of rules.
To create a Column object that represents a constant value, use snowflake.snowpark.functions.lit()
Column objects can be built with the operators, such as + - * / ** & | == != > < >= <=
When you use |, & as logical operators on columns, you must always enclose column expressions with parentheses
Do not use and, or, and not logical operators on column objects
The reason is Python doesn’t have a magic method, or dunder method for them. It will raise an error and tell you to use |, & or ~, for which Python has magic methods.
To access elements of a semi-structured Object and Array, use [] on a Column object
This class has methods for the most frequently used column transformations and operators.
Module snowflake.snowpark.functions defines many functions to transform columns.
-
21Snowflake Snowpark Column Level Operations QuizQuiz
-
22Creating Stored Procedures & User Defined Function Using Snowpark API For PythonVideo lesson
class storedProcedureRegistration
Provides methods to register lambdas and functions as stored procedures in the Snowflake database.
session.sproc returns an object of this class. You can use this object to register stored procedures that you plan to use in the current session or permanently.
Note that the first parameter of your function should be a snowpark Session.
Also, you need to add snowflake-snowpark-python package (version >= 0.4.0) to your session before trying to create a stored procedure.
There are two ways to register a stored procedure with Snowpark:
Use sproc() or register(). By pointing to a runtime Python function, Snowpark uses cloudpickle to serialize this function to bytecode, and deserialize the bytecode to a Python function on the Snowflake server during stored procedure creation.
Use register_from_file(). By pointing to a Python file or a zip file containing Python source code and the target function name, Snowpark uploads this file to a stage (which can also be customized), and load the corresponding function from this file to the Python runtime on the Snowflake server during stored procedure creation.
class udfRegistration
Provides methods to register lambdas and functions as UDFs in the Snowflake database
session.udf returns an object of this class. You can use this object to register UDFs that you plan to use in the current session or permanently. The methods that register a UDF return a UserDefinedFunction object, which you can also use in Column expressions.
-
23Snowflake Snowpark UDFs and Stored Procedures QuizQuiz
External Links May Contain Affiliate Links read more