Skip to content

Commit

Permalink
add notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-mrojas committed Jul 11, 2024
1 parent 6147274 commit eb8c1bc
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions extras/jdbc_read/using_jdbc_reader_notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"metadata": {
"kernelspec": {
"display_name": "Streamlit Notebook",
"name": "streamlit"
}
},
"nbformat_minor": 5,
"nbformat": 4,
"cells": [
{
"cell_type": "markdown",
"id": "ea607b97-cb31-4e81-b89b-2e866f53c976",
"metadata": {
"name": "cell3",
"collapsed": false
},
"source": "# Using JDBC Reader in your notebooks\n\nThis notebook uses the code from the [medium article](https://medium.com/@orellabac/ingest-external-data-into-snowflake-with-snowpark-and-jdbc-eb487b61078c)\n\nIt explains for example how to get it working in your notebook"
},
{
"cell_type": "markdown",
"id": "de5aab3b-0db1-4bb6-b54c-4e399cf15cdb",
"metadata": {
"name": "cell4",
"collapsed": false
},
"source": "For making it easier we have provided a [simple file](https://raw.githubusercontent.com/Snowflake-Labs/snowpark-extensions-py/main/extras/jdbc_read/jdbc_reader.py) you can use to load this \"extension\" and\nregister the UDF.\n\n> You need an external access integration and a network rule previously created so you can register the UDF. You can see the [documentation for more details](https://docs.snowflake.com/en/developer-guide/external-network-access/creating-using-external-network-access).\n\n"
},
{
"cell_type": "markdown",
"id": "b6392f8a-0fcf-452c-887a-8549e0cd87d9",
"metadata": {
"name": "cell1",
"collapsed": false
},
"source": "An easy setup is to setup an stage for example `JDBC_DRIVERS` \n![jdbc_drivers_stage](https://raw.githubusercontent.com/Snowflake-Labs/snowpark-extensions-py/main/extras/jdbc_read/jdbc_driver.png)"
},
{
"cell_type": "markdown",
"id": "48f4e46d-2ce0-4b39-9587-b3c57e319c42",
"metadata": {
"name": "cell6",
"collapsed": false
},
"source": "After you have setup this stage add a reference to the `jdbc_reader.py` \n![](https://github.com/Snowflake-Labs/snowpark-extensions-py/blob/main/extras/jdbc_read/packages.png?raw=true)"
},
{
"cell_type": "markdown",
"id": "5ee81b2a-6f56-47ec-91d2-55aa153aff29",
"metadata": {
"name": "cell7",
"collapsed": false
},
"source": "Ok once you have done that we can import the `jdbc_reader` module and use the \n`jdbc_reader.register_jdbc_reader` function.\n\nWe just need to pass the name of the STAGE where we have all our jars and the name of the integration. You can also pass a name to a secret which is more secure way to store credentials.\n"
},
{
"cell_type": "code",
"id": "8d50cbf4-0c8d-4950-86cb-114990437ac9",
"metadata": {
"language": "python",
"name": "cell2",
"collapsed": false
},
"source": "from snowflake.snowpark.context import get_active_session\nsession = get_active_session()\n\nimport jdbc_reader\njdbc_reader.register_jdbc_reader(\"JDBC_DRIVERS\",\"ALLOW_ALL_EAI\")",
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"id": "c1577ada-4c67-4798-a0b9-0eb2f9fcfb70",
"metadata": {
"name": "cell8",
"collapsed": false
},
"source": "After registering the function you can now used the python extensions to read from an external JDBC source. For example from a Azure SQL Database"
},
{
"cell_type": "code",
"id": "f95d84f8-d485-406b-b439-862c0f7685e8",
"metadata": {
"language": "python",
"name": "cell5",
"collapsed": false
},
"outputs": [],
"source": "from snowflake.snowpark.functions import object_keys,sql_expr,typeof\nurl=\"jdbc:sqlserver://snowpark-sqlserver-tests.database.windows.net:1433;database=external-access-database\"\n\ndata = session.read.format(\"jdbc\")\\\n.option(\"driver\", \"com.microsoft.sqlserver.jdbc.SQLServerDriver\")\\\n.option(\"url\", url)\\\n.option(\"username\", \"admin@snowpark-sqlserver-tests\")\\\n.option(\"password\", \"xxxxx\")\\\n.query(\"\"\"\nSELECT AddressID, AddressLine1, AddressLine2, City\nFROM [external-access-database].SalesLT.Address\"\"\").load().cache_result()\n\ndata",
"execution_count": null
}
]
}

0 comments on commit eb8c1bc

Please sign in to comment.