cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to Pull Cached SQL Table into Python Dictionary?

drewtoby
New Contributor II

Hello,

I have been working on this issue as a proof of concept - it would be extremely helpful to iterate through tables via loops in a few scenarios. I have a simple three column dimension that I added to a cached table.

cache lazy table hedis_cache select * from hofhc.hedis_dim

I then tried the following two methods. The first is coming up empty, whereas the second is returning data as a dataframe, not a python dictionary.

Method 1 

Method 2 

Any advice? Thanks in advance for all the help!

1 ACCEPTED SOLUTION

Accepted Solutions

daniel_sahal
Esteemed Contributor

@Andrew Begg​ 

First of all, cache table creates a view, not table, so you won't be able to use pd.read_table (https://docs.databricks.com/sql/language-manual/sql-ref-syntax-aux-cache-cache-table.html).

About the second method - .select() still gives a dataframe as an output. You need to find a way to convert DF to dict.

View solution in original post

2 REPLIES 2

daniel_sahal
Esteemed Contributor

@Andrew Begg​ 

First of all, cache table creates a view, not table, so you won't be able to use pd.read_table (https://docs.databricks.com/sql/language-manual/sql-ref-syntax-aux-cache-cache-table.html).

About the second method - .select() still gives a dataframe as an output. You need to find a way to convert DF to dict.

drewtoby
New Contributor II

Got it to work, thank you for the tip! I needed to convert the dataframe over to a pandas dataframe

https://www.geeksforgeeks.org/convert-pyspark-dataframe-to-dictionary-in-python/

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group