A Tablestore client provides various methods for performing operations on tables and data in Tablestore. This topic describes how to use Tablestore SDK for Python to initialize a Tablestore client.
This topic describes how to initialize a Tablestore client using the AccessKey pair of an Alibaba Cloud account as an example. If you want to use the AccessKey pair of a Resource Access Management (RAM) user or temporary access credentials from Security Token Service (STS) to initialize the client, see Use the AccessKey pair of a RAM user to access Tablestore and Use temporary access credentials from STS to access Tablestore.
Preparations
Before you initialize a Tablestore client, you must obtain information about the Tablestore instance that you want to access, install Tablestore SDK for Java, and configure access credentials.
Obtain information about the instance that you want to access
Region ID: The ID of the region in which the instance resides. For example, the region ID of China (Hangzhou) is cn-hangzhou.
Instance name and endpoint: Each Tablestore instance has an endpoint. You must specify the endpoint in your application to perform operations on tables and data. You can obtain the instance name and endpoint as follows.
Log on to the Tablestore console.
In the top navigation bar, select a resource group and a region.
On the Overview page, click the instance alias or click Manage Instance in the Actions column.
On the Instance Details tab, view the name and endpoint of the instance.
ImportantBy default, Internet access is disabled for new instances. To access resources in an instance over the Internet, you must enable Internet access for the instance.
Install Tablestore SDK for Python
Run the following command to use pip to install Tablestore SDK for Python:
sudo pip install tablestoreFor more information, see Install Tablestore SDK for Python.
Configure access credentials
You need to create an AccessKey for an Alibaba Cloud account or a RAM user and configure the AccessKey in the environment variables as follows.
After you complete the configuration, restart or refresh your compilation and runtime environment, including IDEs, command-line interfaces, other desktop applications, and background services, to ensure that the latest system environment variables are loaded successfully. For more information about configuring access credentials, see Configure access credentials.
Linux
Run the following commands in the command-line interface to append environment variable settings to the
~/.bashrcfile.echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrcRun the following command to allow the changes to take effect:
source ~/.bashrcRun the following commands to check whether the environment variables take effect:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
macOS
Run the following command in the terminal to check the default Shell type.
echo $SHELLPerform operations based on the default Shell type.
Zsh
Run the following commands to append environment variable settings to the
~/.zshrcfile.echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrcRun the following command to allow the changes to take effect:
source ~/.zshrcRun the following commands to check whether the environment variables take effect:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
Bash
Run the following commands to append environment variable settings to the
~/.bash_profilefile.echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profileRun the following command to allow the changes to take effect:
source ~/.bash_profileRun the following commands to check whether the environment variables take effect:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
Windows
CMD
Run the following commands in CMD to set environment variables.
setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID" setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET"After restarting CMD, run the following commands to check whether the environment variables take effect:
echo %TABLESTORE_ACCESS_KEY_ID% echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
Run the following command in PowerShell:
[Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User) [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", "YOUR_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)Run the following commands to check whether the environment variables take effect:
[Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User) [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)
Initialize a client
You must initialize a client and call the methods provided by the client to access Tablestore. Tablestore SDK for Python provides the OTSClient.
If you want to access Tablestore resources over HTTPS, we recommend that you use Tablestore SDK for Python V6.x.x and make sure that the version of OpenSSL is 0.9.8j or later. We recommend that you use OpenSSL 1.0.2d.
The release package for Tablestore SDK for Python contains the certifi package that you can directly install and use. To update the root certificate, download the latest root certificate from the official website of certifi.
Wide Column model
The following sample code provides an example on how to initialize a client, query the list of data tables in an instance, and display the list in the console:
# -*- coding: utf-8 -*-
import os
from tablestore import OTSClient
# Specify the name of the instance.
instance_name = "yourInstanceName"
# Specify the endpoint of the instance.
endpoint = "yourEndpoint"
# Obtain the AccessKey ID and AccessKey secret from the environment variables.
access_key_id = os.getenv("TABLESTORE_ACCESS_KEY_ID")
access_key_secret = os.getenv("TABLESTORE_ACCESS_KEY_SECRET")
# Initialize a Tablestore client
client = OTSClient(endpoint, access_key_id, access_key_secret, instance_name)
# Query the list of data tables in the instance and display the list in the console
resp = client.list_table()
for table_name in resp:
print(table_name)TimeSeries model
The following sample code provides an example on how to initialize a client, query the list of time series tables in an instance, and display the list in the console:
# -*- coding: utf-8 -*-
import os
from tablestore import OTSClient
# Specify the name of the instance.
instance_name = "yourInstanceName"
# Specify the endpoint of the instance.
endpoint = "yourEndpoint"
# Obtain the AccessKey ID and AccessKey secret from the environment variables.
access_key_id = os.getenv("TABLESTORE_ACCESS_KEY_ID")
access_key_secret = os.getenv("TABLESTORE_ACCESS_KEY_SECRET")
# Initialize a Tablestore client
client = OTSClient(endpoint, access_key_id, access_key_secret, instance_name)
# Query the list of time series tables in the instance and display the list in the console
response = client.list_timeseries_table()
for tableMeta in response:
print(tableMeta.timeseries_table_name)You can also use the Credentials tool to read access credentials.
FAQ
Appendix: Use the Credentials tool to read access credentials
Run the following command to install the alibabacloud_credentials package:
pip install alibabacloud_credentialsConfigure the environment variables.
Configure the
ALIBABA_CLOUD_ACCESS_KEY_IDenvironment variable for the AccessKey ID of your Alibaba Cloud account and theALIBABA_CLOUD_ACCESS_KEY_SECRETenvironment variable for the AccessKey secret of your Alibaba Cloud account.Read access credentials.
The following sample code provides an example on how to use the Credentials tool to read the access credentials from the environment variables.
# -*- coding: utf-8 -*- from alibabacloud_credentials.client import Client as CredClient # Use CredClient to obtain the AccessKey ID and AccessKey secret from the environment variables. cred = CredClient() access_key_id = cred.get_credential().access_key_id access_key_secret = cred.get_credential().access_key_secret