Loading...

Azure Table Storage Tutorial - Python Azure Tables SDK with NBA Stats API

Azure Table Storage Tutorial - Python Azure Tables SDK with NBA Stats API

Scenario:
The NBA playoffs start on 15 April following the conclusion of the Play-In Tournament on 14 April. In this tutorial, we use the nba_api Python API Client for NBA.com to fetch NBA teams stats and then run the Python Azure Tables SDK to create Azure table and entries. Finally, with Azure Storage Explorer we can easily build search query and visualize the results.

 

Objective:
To illustrate Azure Tables Python SDK usage using NBA API, followed by querying Azure Table Storage with Azure Storage Explorer's Query Builder.

Pre-requisites:

For this example, you would need:

  • An Azure Table Storage

Steps:

  1. Install nba_api
  2. Install Azure Tables client library for Python
  3. Python code to retrieve data, create table, and insert entries
  4. Run nba.py
  5. Visualize the NBA table (with Azure Storage Explorer)
  6. Sample query (with Azure Storage Explorer)

 

[STEP 1]: Install nba_api

 

 

pip install nba_api

 

 

[STEP 2]: Install Azure Tables client library for Python

 

 

pip install azure-data-tables

 

 

[STEP 3]: Python code to retrieve data, create table, and insert entries

 

Note:

  • replace STORAGE_CONNECTION_STRING with your storage connection string.
  • I use "state" as PartitionKey and "abbreviation" as RowKey.
  • This article describes best practice when choosing partition & row key for entities.
  • The response from calling teams.get_teams() is like the following:

 

 

[ { "id":1610612737, "full_name":"Atlanta Hawks", "abbreviation":"ATL", "nickname":"Hawks", "city":"Atlanta", "state":"Georgia", "year_founded":1949 }, { "id":1610612738, "full_name":"Boston Celtics", "abbreviation":"BOS", "nickname":"Celtics", "city":"Boston", "state":"Massachusetts", "year_founded":1946 }, { "id":1610612739, "full_name":"Cleveland Cavaliers", "abbreviation":"CLE", "nickname":"Cavaliers", "city":"Cleveland", "state":"Ohio", "year_founded":1970 } ]

 

 

nba.py

 

 

from nba_api.stats.static import teams # Retrieve Data result = teams.get_teams() print("- NBA Stats Retrieved") # Create Table from azure.data.tables import TableServiceClient table_service_client = TableServiceClient.from_connection_string(conn_str=STORAGE_CONNECTION_STRING) table_name = "NBA" table_client = table_service_client.create_table(table_name=table_name) print("- Table created") # Create Entity from azure.data.tables import TableServiceClient for team in result: my_entity = { u'PartitionKey': team['state'], u'RowKey': team['abbreviation'], u'id': team['id'], u'city': team['city'], u'state': team['state'], u'nickname': team['nickname'], u'full_name': team['full_name'], u'year_founded': team['year_founded'], u'abbreviation': team['abbreviation'] } entity = table_client.create_entity(entity=my_entity) print("- Entity created")

 

 

[STEP 4]: Run nba.py

 

 

PS C:\Users\xxxxxx> python .\nba.py - NBA Stats Retrieved - Table created - Entity created

 

 

 

[STEP 5]: Visualize the NBA table (with Azure Storage Explorer)

 

The NBA table contains data of the 30 NBA teams with the following info:

  • ID
  • City Name
  • State Name
  • Nickname
  • Full Name
  • Year Founded
  • Abbreviation

charleswang_0-1682342106101.png

 

[STEP 6]: Sample query (with Azure Storage Explorer)

 

Query Example: To find out all NBA teams that are based in California and were founded in 1948.

 

1. Click the "Query" button on the upper-left corner of the table.

charleswang_2-1682343028263.png

 

2. Use Query Builder to build the query. Then click ▶ to execute.

charleswang_0-1682344636358.png

 

3. Result is Lakers (LAL) & Kings (SAC).

 

Conclusion:

This example shows how to use the nba_api to retrieve NBA teams stats and the Azure Tables Python SDK to work with Azure Table storage. The Azure Tables client library for Python provides a simple and intuitive API for working with Azure Table Storage, with methods for creating and managing table clients, table operations, and entities.

 

References:

 

Published on:

Learn more
Azure PaaS Blog articles
Azure PaaS Blog articles

Azure PaaS Blog articles

Share post:

Related posts

Selecting the Optimal Container for Azure AI: Docker, ACI, or AKS?

Deploying Azure AI services in containers like Docker, Azure Container Instances (ACI), or Azure Kubernetes Service (...

1 hour ago

Azure Elastic SAN for Azure VMware Solution: now Generally Available

Have you been looking to expand your storage on Azure VMware Solution (AVS), but do not need the extra compute performance and the associated ...

11 hours ago

Introducing Pull Request Annotation for CodeQL and Dependency Scanning in GitHub Advanced Security for Azure DevOps

In the world of software development, security is paramount. As developers, we strive to write clean, efficient, and most importantly, secure ...

1 day ago

Accelerate metadata heavy workloads with Metadata Caching preview for Azure Premium Files SMB & REST

Azure Files previously announced the limited preview of Metadata caching highlighting improvements on the metadata latency (up to 55...

2 days ago

How to Choose the Right Models for Your Apps | Azure AI

With more than 1700 models to choose from on Azure, selecting the right one is key to enabling the right capabilities, at the right price poin...

2 days ago

MMR Call Redirection for Azure Virtual Desktop, Windows 365 now available

Today, I am pleased to share the launch of Multimedia Redirection (MMR) Call Redirection for Azure Virtual Desktop and Windows 365. Call Redir...

2 days ago

Liquid Cooling in Air Cooled Data Centers on Microsoft Azure

With the advent of artificial intelligence and machine learning (AI/ML), hyperscale datacenters are increasingly accommodating AI accelerators...

2 days ago

Introducing Azure Product Retirement Livestreams

The Azure Retirements team, in collaboration with key partner groups, is excited t...

2 days ago

Azure Developer CLI (azd) – October 2024

This post announces the October release of the Azure Developer CLI (`azd`), including configurable api-version for ACA. The post Azure Develop...

3 days ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy