Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update samples #1

Merged
merged 4 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions sdk/tables/azure-data-tables/samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
page_type: sample
languages:
- python
products:
- azure
- azure-data-tables
urlFragment: tables-samples
---

# Samples for Azure Tables client library for Python

These code samples show common scenario operations with the Azure Text Analytics client library.
The async versions of the samples require Python 3.5 or later.

You can authenticate your client with a Tables API key:
* See [sample_authentication.py][sample_authentication] and [sample_authentication_async.py][sample_authentication_async] for how to authenticate in the above cases.

These sample programs show common scenarios for the Text Analytics client's offerings.

|**File Name**|**Description**|
|-------------|---------------|
|[sample_create_client.py][create_client] and [sample_create_client_async.py][create_client_async]|Instantiate a table client|Authorizing a `TableServiceClient` object|
|[sample_create_delete_table.py][create_delete_table] and [sample_create_delete_table_async.py][create_delete_table_async]|Creating a table in a storage account|
|[sample_insert_delete_entities.py][insert_delete_entities] and [sample_insert_delete_entities_async.py][insert_delete_entities_async]|Inserting and deleting individual entities into a table|
|[sample_query_table.py][query_table] and [sample_query_table_async.py][query_table_async]|Querying entities in a table|

### Prerequisites
* Python 2.7, or 3.5 or later is required to use this package.
* You must have an [Azure subscription](https://azure.microsoft.com/free/) and an
[Azure storage account](https://docs.microsoft.com/azure/storage/common/storage-account-overview) to use this package
or you must have a [Azure Cosmos Account](https://docs.microsoft.com/azure/cosmos-db/account-overview).

## Setup

1. Install the Azure Data Tables client library for Python with [pip](https://pypi.org/project/pip/):

```bash
pip install --pre azure-data-tables
```


2. Clone or download this sample repository
3. Open the sample folder in Visual Studio Code or your IDE of choice.

## Running the samples

1. Open a terminal window and `cd` to the directory that the samples are saved in.
2. Set the environment variables specified in the sample file you wish to run.
3. Follow the usage described in the file, e.g. `python sample_detect_language.py`

## Next steps

Check out the [API reference documentation][api_reference_documentation] to learn more about
what you can do with the Azure Text Analytics client library.


<!-- LINKS -->
[api_reference_documentation]: https://aka.ms/azsdk/python/tables/docs

[sample_authentication]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/tables/azure-data-tables/samples/samples_authentication.py
[sample_authentication_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/tables/azure-data-tables/samples/samples_authentication_async.py

[create_delete_table]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/tables/azure-data-tables/samples/sample_create_delete_table.py
[create_delete_table_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/tables/azure-data-tables/samples/sample_create_delete_table_async.py

[insert_delete_entities_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/tables/azure-data-tables/samples/sample_insert_delete_entities.py
[insert_delete_entities_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/tables/azure-data-tables/samples/sample_insert_delete_entities_async.py

[query_table]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/tables/azure-data-tables/samples/sample_insert_delete_entities_async.py
[query_table_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/tables/azure-data-tables/samples/sample_insert_delete_entities_async.py
30 changes: 0 additions & 30 deletions sdk/tables/azure-data-tables/samples/create_query_entities.py

This file was deleted.

63 changes: 0 additions & 63 deletions sdk/tables/azure-data-tables/samples/creation_deletion_of_table.py

This file was deleted.

This file was deleted.

34 changes: 22 additions & 12 deletions sdk/tables/azure-data-tables/samples/querying_table.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import os

class QueryTable(object):
connection_string = "DefaultEndpointsProtocol=https;AccountName=example;AccountKey=fasgfbhBDFAShjDQ4jkvbnaBFHJOWS6gkjngdakeKFNLK==;EndpointSuffix=core.windows.net"
table_name = "NAME"
account_url = "https://example.table.core.windows.net/"
account_name = "example"
access_key = "fasgfbhBDFAShjDQ4jkvbnaBFHJOWS6gkjngdakeKFNLK=="
connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
access_key = os.getenv("AZURE_TABLES_KEY")
account_url = os.getenv("AZURE_TABLES_ACCOUNT_URL")
account_name = os.getenv("AZURE_TABLES_ACCOUNT_NAME")
table_name = "OfficeSupplies"

# Creating query filter for that table
table_name = "Office Supplies"
name_filter = "TableName eq '{}'".format(table_name)

@classmethod
def query_tables(self):
from azure.data.tables import TableServiceClient
from azure.core.exceptions import ResourceExistsError

# table_service_client = TableServiceClient(account_url=self.account_url, credential=self.access_key)
table_service_client = TableServiceClient.from_connection_string(self.connection_string)

table_service_client = TableServiceClient(account_url=self.account_url, credential=self.access_key)
# Create Tables to query
my_table = table_service_client.create_table(table_name=self.table_name)
print(my_table)
try:
my_table = table_service_client.create_table(table_name=self.table_name)
print(my_table.table_name)
except ResourceExistsError:
print("Table already exists!")

# Query tables
queried_tables = table_service_client.query_tables(filter=self.name_filter, results_per_page=10)
# table_client.query_tables() returns an itemPaged
# queried_tables is a list of filtered tables

for table in queried_tables:
print(table)
print(table.table_name)

if __name__ == "__main__":
QueryTable.query_tables()
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,26 @@


class TableAuthSamples(object):

connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING")

account_url = os.getenv("AZURE_STORAGE_ACCOUNT_URL")
account_name = os.getenv("AZURE_STORAGE_ACCOUNT_NAME")
access_key = os.getenv("AZURE_STORAGE_ACCESS_KEY")
connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
access_key = os.getenv("AZURE_TABLES_KEY")
account_url = os.getenv("AZURE_TABLES_ACCOUNT_URL")
account_name = os.getenv("AZURE_TABLES_ACCOUNT_NAME")

def authentication_by_connection_string(self):
# Instantiate a TableServiceClient using a connection string
# [START auth_from_connection_string]
from azure.data.tables import TableServiceClient
table_service = TableServiceClient.from_connection_string(conn_str=self.connection_string)
# [END auth_from_connection_string]

# Get information for the Table Service
properties = table_service.get_service_properties()
print("Connection String: {}".format(properties))

def authentication_by_shared_key(self):
# Instantiate a TableServiceClient using a shared access key
# [START create_Table_service_client]
from azure.data.tables import TableServiceClient
table_service = TableServiceClient(account_url=self.account_url, credential=self.access_key)
# [END create_table_service_client]

# Get information for the Table Service
properties = table_service.get_service_properties()
print("Shared Key: {}".format(properties))

def authentication_by_shared_access_signature(self):
# Instantiate a TableServiceClient using a connection string
Expand All @@ -66,7 +60,7 @@ def authentication_by_shared_access_signature(self):

# Create a SAS token to use for authentication of a client
from azure.data.tables import generate_account_sas, ResourceTypes, AccountSasPermissions

print(self.account_name)
sas_token = generate_account_sas(
self.account_name,
self.access_key,
Expand All @@ -77,9 +71,8 @@ def authentication_by_shared_access_signature(self):

token_auth_table_service = TableServiceClient(account_url=self.account_url, credential=sas_token)

# Get information for the Table Service
properties = token_auth_table_service.get_service_properties()

properties = table_service.get_service_properties()
print("Shared Access Signature: {}".format(properties))

if __name__ == '__main__':
sample = TableAuthSamples()
Expand Down
Loading