|
| 1 | +--- |
| 2 | +title: "Quickstart" |
| 3 | +linkTitle: "Quickstart" |
| 4 | +weight: 2 |
| 5 | +description: Get started with the LocalStack Snowflake emulator in a few simple steps |
| 6 | +--- |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +This guide explains how to set up the LocalStack Snowflake emulator and develop a Python program using the Snowflake Connector for Python (`snowflake-connector-python`) to interact with emulated Snowflake running on your local machine. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- [`localstack` CLI](https://docs.localstack.cloud/getting-started/installation/#localstack-cli) |
| 15 | +- [LocalStack Snowflake emulator]({{< ref "installation" >}}) |
| 16 | +- Python 3.10 or later |
| 17 | +- [`snowflake-connector-python` library](https://docs.snowflake.com/en/developer-guide/python-connector/python-connector-install) |
| 18 | + |
| 19 | +## Instructions |
| 20 | + |
| 21 | +Before you begin, install the LocalStack Snowflake emulator and start the LocalStack container with the following commands: |
| 22 | + |
| 23 | +{{< command >}} |
| 24 | +$ export LOCALSTACK_AUTH_TOKEN=<your_auth_token> |
| 25 | +$ localstack start |
| 26 | +{{< / command >}} |
| 27 | + |
| 28 | +Check the emulator's availability by running: |
| 29 | + |
| 30 | +{{< command >}} |
| 31 | +$ localstack extensions list |
| 32 | +<disable-copy> |
| 33 | +┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━┓ |
| 34 | +┃ Name ┃ Summary ┃ Version ┃ Author ┃ Plugin name ┃ |
| 35 | +┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━┩ |
| 36 | +│ localstack-extension-snowflake │ LocalStack Extension: Snowflake │ 0.1.22 │ LocalStack │ snowflake │ |
| 37 | +└────────────────────────────────┴─────────────────────────────────┴─────────┴────────────┴─────────────┘ |
| 38 | +</disable-copy> |
| 39 | +{{< / command >}} |
| 40 | + |
| 41 | +### Connect to LocalStack Snowflake emulator |
| 42 | + |
| 43 | +Create a new Python file named `main.py` and use the following code to connect to the LocalStack Snowflake emulator: |
| 44 | + |
| 45 | +```python |
| 46 | +import snowflake.connector as sf |
| 47 | + |
| 48 | +sf_conn_obj = sf.connect( |
| 49 | + user="test", |
| 50 | + password="test", |
| 51 | + account="test", |
| 52 | + database="test", |
| 53 | + host="snowflake.localhost.localstack.cloud", |
| 54 | +) |
| 55 | +``` |
| 56 | + |
| 57 | +Specify the `host` parameter as `snowflake.localhost.localstack.cloud` and the other parameters as `test` to avoid connecting to the real Snowflake instance. |
| 58 | + |
| 59 | +### Create and execute a query |
| 60 | + |
| 61 | +Extend the Python program to insert rows from a list object into the emulated Snowflake table. Create a cursor object and execute the query: |
| 62 | + |
| 63 | +```python |
| 64 | +print("1. Insert lot of rows from a list object to Snowflake table") |
| 65 | +print("2. Creating a cursor object") |
| 66 | +sf_cur_obj = sf_conn_obj.cursor() |
| 67 | + |
| 68 | +print("3. Executing a query on cursor object") |
| 69 | +try: |
| 70 | + sf_cur_obj.execute( |
| 71 | + "create or replace table " |
| 72 | + "ability(name string, skill string )") |
| 73 | + |
| 74 | + rows_to_insert = [('John', 'SQL'), ('Alex', 'Java'), ('Pete', 'Snowflake')] |
| 75 | + |
| 76 | + sf_cur_obj.executemany( |
| 77 | + " insert into ability (name, skill) values (%s,%s) " ,rows_to_insert) |
| 78 | + |
| 79 | + sf_cur_obj.execute("select name, skill from ability") |
| 80 | + |
| 81 | + print("4. Fetching the results") |
| 82 | + result = sf_cur_obj.fetchall() |
| 83 | + print("Total # of rows :" , len(result)) |
| 84 | + print("Row-1 =>",result[0]) |
| 85 | + print("Row-2 =>",result[1]) |
| 86 | +finally: |
| 87 | + sf_cur_obj.close() |
| 88 | +``` |
| 89 | + |
| 90 | +This program creates a table named `ability`, inserts rows, and fetches the results. |
| 91 | + |
| 92 | +### Run the Python program |
| 93 | + |
| 94 | +Execute the Python program with: |
| 95 | + |
| 96 | +{{< command >}} |
| 97 | +$ python main.py |
| 98 | +{{< / command >}} |
| 99 | + |
| 100 | +The output should be: |
| 101 | + |
| 102 | +```bash |
| 103 | +Insert lot of rows from a list object to Snowflake table |
| 104 | +1. Insert lot of rows from a list object to Snowflake table |
| 105 | +2. Creating a cursor object |
| 106 | +3. Executing a query on cursor object |
| 107 | +4. Fetching the results |
| 108 | +Total # of rows : 3 |
| 109 | +Row-1 => ('John', 'SQL') |
| 110 | +Row-2 => ('Alex', 'Java') |
| 111 | +``` |
| 112 | + |
| 113 | +Verify the results by navigating to the LocalStack logs: |
| 114 | + |
| 115 | +```bash |
| 116 | +2024-02-22T06:03:13.627 INFO --- [ asgi_gw_0] localstack.request.http : POST /session/v1/login-request => 200 |
| 117 | +2024-02-22T06:03:16.122 WARN --- [ asgi_gw_0] l.packages.core : postgresql will be installed as an OS package, even though install target is _not_ set to be static. |
| 118 | +2024-02-22T06:03:45.917 INFO --- [ asgi_gw_0] localstack.request.http : POST /queries/v1/query-request => 200 |
| 119 | +2024-02-22T06:03:46.016 INFO --- [ asgi_gw_1] localstack.request.http : POST /queries/v1/query-request => 200 |
| 120 | +2024-02-22T06:03:49.361 INFO --- [ asgi_gw_0] localstack.request.http : POST /queries/v1/query-request => 200 |
| 121 | +2024-02-22T06:03:49.412 INFO --- [ asgi_gw_1] localstack.request.http : POST /session => 200 |
| 122 | +``` |
| 123 | + |
| 124 | +### Destroy the local infrastructure |
| 125 | + |
| 126 | +To stop LocalStack and remove locally created resources, use: |
| 127 | + |
| 128 | +{{< command >}} |
| 129 | +$ localstack stop |
| 130 | +{{< / command >}} |
| 131 | + |
| 132 | +LocalStack is ephemeral and doesn't persist data across restarts. It runs inside a Docker container, and once it’s stopped, all locally created resources are automatically removed. In a future release of the Snowflake emulator, we will provide proper persistence and integration with our [Cloud Pods](https://docs.localstack.cloud/user-guide/state-management/cloud-pods/) feature as well. |
| 133 | + |
| 134 | +## Next steps |
| 135 | + |
| 136 | +You can now explore the following resources to learn more about the LocalStack Snowflake emulator: |
| 137 | + |
| 138 | +- [User Guide]({{< ref "user-guide" >}}): Learn about the LocalStack Snowflake emulator's features and how to use them. |
| 139 | +- [Tutorials]({{< ref "tutorials" >}}): Explore tutorials to use the LocalStack Snowflake emulator for local development and testing. |
| 140 | +- [References]({{< ref "references" >}}): Find information about the LocalStack Snowflake emulator's configuration, changelog, and function coverage. |
0 commit comments