Accessing Snowflake with R
Contents
Snowflake is a well known cloud-based database. It’s used for Data Warehouses and other big data applications.
In this article and the following ones I want to show how to setup and access a snowflake database from various clients such as R, Tableau and PowerBI.
So let’s start using R.
Setup Snowflake
Assuming that we already have access to an instance of Snowflake we first setup a new environment for our tests. This environment consists of a warehouse, a database and an user.
Note: I will name all things we will create EXAMPLE_something. So we can recognize our new items.
Create Warehouse
A warehouse represents the computing power. A warehouse has a specific size which determines the computing power (and the price you pay for it). You only pay for the time the warehouse is used.
So switch to SYSADMIN role and create a new warehouse. We call it EXAMPLE_WH.
Don’t press Finish. Instead click onto Show SQL.
As you can see we can use SQL-commands (or SQL-like commands) to create this warehouse. Actually we’ll do this.
Copy this SQL-command and switch to Worksheets. As you can see we choose a smaller timeout for AUTO_SUSPEND. We also lower the value for STATEMENT_TIMEOUT_IN_SECONDS. That’s like a safety net in case something runs amok.
|
|
Create Database
Using the worksheet we can create a database, too. We call it EXAMPLE_DB.
|
|
Create Role
We also create a role called EXAMPLE_ROLE. This role will be used to manage permissions.
|
|
Create User
Now we can switch our own role to ACCOUNTADMIN and create a new user called EXAMPLE_USER.
As you can see it’s also possible to create a user using SQL-commands.
|
|
Grant permissions to Role
Now grant permissions for the database to our new role.
Also grant permissions for the warehouse to the role.
Create Schema
Now we should grant EXAMPLE_ROLE to SYSADMIN role because we can use our default user and switch to EXAMPLE_ROLE and create a schema within the database.
The new schema is called EXAMPLE_SCHEMA.
|
|
Create tables
Within the newly created schema we create two empty tables.
|
|
|
|
Accessing Snowflake from R
So now it’s time to connect to Snowflake from R:
Connection via DBI
You have to setup an odbc-connection to access Snowflake from R. The way to do this depends on your operation system. So have a look at the manual at Snowflake, please.
|
|
Writing some data
Let’s generate some sample data and push it to Snowflake.
|
|
As you can see we use copy_to()
to push a data.frame into a Snowflake database.
That’s not the preferred way. For small data it’s acceptable and a fast way
to get some data to Snowflake.
Reading some data
Reading from Snowflake is as simple as working with data.frames. We just bind
a table to an R object using tbl()
.
|
|
|
|
Complex statements
Even complext statements you use within tidyverse
are possible.
|
|
|
|
The last command collect()
fetches the data from the database. Until this
command nothing has happend.
SQL
Before calling collect()
we can inspect the SQL-code which will be send to
Snowflake.
Therefor you can call show_query()
.
|
|
|
|
Here’s the SQL-command which Snowflake shows in its history: It’s identical.
Next Articles
The next articles will show how to access this data from Tableau and PowerBI.