Updated on 2025-12-11 GMT+08:00

Performing Operations on Serverless Tables Using Python

This section describes how to perform operations on Serverless tables using Python.

Usage Notes

Serverless tables are only available to GeminiDB DynamoDB-Compatible API.

Prerequisites

  • You have created a VPC endpoint for GeminiDB Serverless.
  • For details about how to create an ECS, see Purchasing an ECS in Getting Started with Elastic Cloud Server.
  • Python has been installed on an ECS.

Connection Example

Replace the IP address (***.***.***.***) in the following code example with the IP address queried in the endpoint list.

Replace region in the following code example with the actual region.

Python code example:

#!/usr/bin/python
import boto3

url = 'http://***.***.***.***'
dynamodb = boto3.resource('dynamodb',
                          endpoint_url=url, 
                          aws_access_key_id='ak', 
                          aws_secret_access_key='sk', 
                          region_name="region")

Creating a Serverless Table

After Serverless is connected, use Python to create a Serverless table, for example:

#!/usr/bin/python
import boto3
TABLE_NAME = 'test_table'
table = dynamodb.create_table(
    AttributeDefinitions=[
        {
            'AttributeName': 'id',
            'AttributeType': 'S'
        },
    ],
    BillingMode='PROVISIONED',
    TableName=TABLE_NAME,
    KeySchema=[
        {
            'AttributeName': 'id',
            'KeyType': 'HASH'
        },
    ],
)

Writing Data to a Serverless Table

After a Serverless table is created, use Python to write data, for example:

#!/usr/bin/python
import boto3
for i in range(0, 100):
    dynamodb.batch_write_item(RequestItems={
        TABLE_NAME: [
            {
                'PutRequest': {
                    'Item': {
                        'id': str(i), 'x': {'hello': 'world'}
                    }
                },
            }
        ]
    })

Querying a Serverless Table

Use Python to query a Serverless table, for example:

#!/usr/bin/python
import boto3
for i in range(0, 100):
    print(dynamodb.batch_get_item(RequestItems={
        TABLE_NAME: {'Keys': [{'id': str(i)}]}
    }))