A Tablestore client is a client for Tablestore and provides various methods that you can use to perform operations on tables and data in Tablestore. This topic describes how to use Tablestore SDK for Node.js to initialize a Tablestore client.
In the examples in this topic, the AccessKey pair of an Alibaba Cloud account is used. You can also use the AccessKey pair of a Resource Access Management (RAM) user or temporary access credentials obtained from Security Token Service (STS) to initialize a Tablestore client. For more information, see Use the AccessKey pair of a RAM user to access Tablestore and Use temporary access credentials obtained 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 ID of the China (Hangzhou) region is cn-hangzhou. For more information about regions, see Regions.
Instance name and endpoint: Each Tablestore instance has an endpoint. You must specify an endpoint before you can perform operations on the data and tables in Tablestore. You can perform the following steps to obtain the endpoint of an instance:
Log on to the Tablestore console.
In the top navigation bar, select a resource group and a region.
On the Overview page, click the name of the instance that you want to manage or click Manage Instance in the Actions column of the instance.
On the Instance Details tab, view the name and endpoints of the instance.
ImportantBy default, Internet-based access is disabled for a newly created instance. If you want to access resources in an instance over the Internet, you must enable Internet-based access for the instance. For more information, see Enable Internet-based access for an instance.
Install Tablestore SDK for Node.js
Use npm to run the following command to install Tablestore SDK for Node.js:
npm install tablestore
For information about how to install Tablestore SDK for Node.js, see Install Tablestore SDK for Node.js.
Configure access credentials
To use Tablestore SDK for Java to initiate a request to access Tablestore, you must configure access credentials. Alibaba Cloud verifies your identity and access permissions based on the access credentials.
After the configuration is complete, restart or refresh your compilation and runtime environment, including IDE, command-line interface, other desktop applications, and background services, to ensure that the latest system environment variables are successfully loaded. For more information about how to configure access credentials, see Configure access credentials.
Linux
Run the following commands in the command-line interface to append environment variable settings to the
~/.bashrc
file.echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc
Run the following command to allow the changes to take effect:
source ~/.bashrc
Run 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 $SHELL
Perform operations based on the default Shell type.
Zsh
Run the following commands to append environment variable settings to the
~/.zshrc
file.echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc
Run the following command to allow the changes to take effect:
source ~/.zshrc
Run 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_profile
file.echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profile
Run the following command to allow the changes to take effect:
source ~/.bash_profile
Run 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 Node.js provides a client for the Wide Column model.
Wide Column model
The following sample code provides an example on how to initialize a Tablestore client, query the list of data tables in an instance, and display the list in the Tablestore console:
// In this example, the path is a relative path. Modify the path as needed.
var TableStore = require('../index.js');
// Specify the name of the instance.
var instancename = 'yourInstanceName';
// Specify the endpoint of the instance.
var endpoint = 'yourEndpoint';
// Obtain the AccessKey ID and AccessKey secret from the environment variables.
var accessKeyId = process.env.TABLESTORE_ACCESS_KEY_ID;
var secretAccessKey = process.env.TABLESTORE_ACCESS_KEY_SECRET;
// Initialize a Tablestore client.
var client = new TableStore.Client({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
endpoint: endpoint,
instancename: instancename
});
// Query the list of data tables in the instance and display the list in the Tablestore console.
client.listTable({}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});