Write data

更新时间:
复制 MD 格式

Tablestore lets you write one or more rows of data to a data table by using Tablestore SDK for Java, either as a single row or as a batch in one request.

To learn about the use cases of Tablestore, see Quick Start Guide for Tablestore: Getting Started and Hands-On Practice.

Write methods

Tablestore provides the following operations to write data: PutRow, UpdateRow, and BatchWriteRow.

Write method

Description

Use case

Insert a single row

The PutRow operation inserts a row. If a row with the same primary key already exists, Tablestore replaces the entire existing row, including all its columns and data versions, with the new one.

Scenarios that require writing a small amount of data.

Update a single row

The UpdateRow operation updates a row. You can add or delete attribute columns, delete a specific data version of an attribute column, or update the value of an existing attribute column. If the specified row does not exist, Tablestore inserts a new row.

Scenarios that require updating existing data, such as deleting an attribute column, deleting a data version, or modifying an attribute column value.

Batch write data

The BatchWriteRow operation performs multiple write operations on one or more data tables in a single request.

A BatchWriteRow operation consists of multiple PutRow, UpdateRow, and DeleteRow sub-operations. You construct these sub-operations the same way as their standalone counterparts.

Scenarios that require writing, deleting, or updating large amounts of data, or performing a combination of these operations simultaneously.

Prerequisites

  • A client is initialized.

  • A data table is created.

Insert a single row

Call the PutRow operation to insert a new row. If a row with the same primary key already exists, Tablestore first deletes the existing row, including all its columns and data versions, and then inserts the new one.

API

"""
Writes a single row and returns the consumed capacity units.
``table_name``: The name of the data table.
``row``: The row data, which includes the primary key and attribute columns.
``condition``: The condition that must be met for the operation to proceed. This is an instance of `tablestore.metadata.Condition`.
Tablestore supports two types of conditions: a row existence condition ('IGNORE', 'EXPECT_EXIST', or 'EXPECT_NOT_EXIST') and a column value condition.
``return_type``: The type of data to return. This is an instance of `tablestore.metadata.ReturnType`. Currently, only `PrimaryKey` is supported, which is typically used with an auto-increment primary key column.
Returns: The capacity units consumed and the requested row data.
consumed: An instance of the tablestore.metadata.CapacityUnit class that represents the consumed capacity units.
return_row: The returned row data, which may include the primary key and attribute columns.
Example:
    primary_key = [('gid',1), ('uid',101)]
    attribute_columns = [('name','Mary'), ('mobile',111****1111), ('address','China, City A'), ('age',20)]
    row = Row(primary_key, attribute_columns)
    condition = Condition('EXPECT_NOT_EXIST')
    consumed, return_row = client.put_row('myTable', row, condition)
"""
def put_row(self, table_name, row, condition = None, return_type = None, transaction_id = None):

Parameters

Parameter

Description

table_name

The name of the data table.

row

The row data, which includes the following parameters:

  • primary_key: The primary key of the row. A primary key consists of a column name, column type, and column value.

    Important
    • The number and data types of the primary key columns must match the schema of the data table.

    • If you use an auto-increment primary key column, set the value of that column to a placeholder. For more information, see Auto-increment primary key column.

  • attribute_columns: The attribute columns of the row. Specify each item in the following order: attribute name, attribute value (ColumnValue), attribute type (ColumnType, optional), and timestamp (optional).

    • For more information about column names and data types, see Naming conventions and data types.

      The attribute type can be INTEGER, STRING (UTF-8 encoded), BINARY, BOOLEAN, or DOUBLE, represented by ColumnType.INTEGER, ColumnType.STRING, ColumnType.BINARY, ColumnType.BOOLEAN, and ColumnType.DOUBLE. The type is required for BINARY but is optional for all other types.

    • The timestamp serves as the data version. For more information, see Data versions and TTL.

      You can have the system generate a data version automatically or specify a custom one. If you do not specify a version, the system generates one by default.

      • If the system generates the data version, it uses the current timestamp in milliseconds (the number of milliseconds that have elapsed since 00:00:00 UTC on January 1, 1970).

      • If you specify a custom data version, it must be a 64-bit integer that represents a timestamp in milliseconds and must be within the valid version range.

condition

Specifies a condition for the update. You can set a condition based on row existence or the value of a specific column. For more information, see Conditional update.

Note
  • RowExistenceExpectation.IGNORE: Inserts the new data regardless of whether the row exists. If the row already exists, it is overwritten.

  • RowExistenceExpectation.EXPECT_EXIST: Inserts the new data only if the row already exists. The existing data is overwritten.

  • RowExistenceExpectation.EXPECT_NOT_EXIST: Inserts the new data only if the row does not exist.

return_type

The type of data to return. This is an instance of the tablestore.metadata.ReturnType class. Currently, only PrimaryKey is supported, which is typically used with an auto-increment primary key column.

transaction_id

The ID of the local transaction. This parameter is required when you use the local transaction feature.

Example

Insert a row of data.

Note

In the following example, the version of the age attribute column is 1498184687000, which corresponds to June 23, 2017. The max_time_deviation parameter, specified during table creation, defines the maximum allowed difference between the server's current time and a row's timestamp. The PutRow operation is rejected if the provided timestamp is older than current_time - max_time_deviation.

# Set the data table name.
table_name = '<TABLE_NAME>'

# The primary key consists of two columns: 'gid' with an integer value of 1, 
# and 'uid' with an integer value of 101.
primary_key = [('gid',1), ('uid',101)]
# Define five attribute columns:
# - 'name': A string 'J***'. The version is not specified, so the system uses the current timestamp as the version.
# - 'mobile': An integer 139********. The version is not specified.
# - 'address': A binary value 'China'. The version is not specified.
# - 'female': A boolean value False. The version is not specified.
# - 'age': A double value 29.7. The version is explicitly set to 1498184687000.
attribute_columns = [('name','J***'), ('mobile',139********),('address', bytearray('China', encoding='utf8')),('female', False), ('age', 29.7, 1498184687000)]
# Construct a Row object from the primary key and attribute columns.
row = Row(primary_key, attribute_columns)

# Set a conditional update. The row existence condition is set to expect that the row does not exist.
# If the row exists, a 'Condition Update Failed' error occurs.
condition = Condition(RowExistenceExpectation.EXPECT_NOT_EXIST)
try:
    # Call the put_row method. If ReturnType is not specified, return_row is None.
    consumed, return_row = client.put_row(table_name, row, condition)

    # Print the consumed write capacity units for this request.
    print('put row succeed, consume %s write cu.' % consumed.write)
# Client exception: typically caused by a parameter error or network issue.
except OTSClientError as e:
    print("put row failed, http_status:%d, error_message:%s" % (e.get_http_status(), e.get_error_message()))
# Server exception: typically caused by a parameter error or throttling.
except OTSServiceError as e:
    print("put row failed, http_status:%d, error_code:%s, error_message:%s, request_id:%s" % (e.get_http_status(), e.get_error_code(), e.get_error_message(), e.get_request_id()))                    

For the complete code, see PutRow@GitHub.

Update a single row

Call the UpdateRow operation to update a row. You can add or delete attribute columns, delete a specific data version of an attribute column, or update the value of an existing attribute column. If the row to be updated does not exist, Tablestore inserts a new row.

API

"""
Description: Updates a single row.
``table_name``: The name of the data table.
``row``: The row data to be updated, which includes the primary key columns (list) and attribute columns (dict).
``condition``: The condition that must be met for the operation to proceed. This is an instance of the tablestore.metadata.Condition class.
Tablestore supports two types of conditions: a row existence condition ('IGNORE', 'EXPECT_EXIST', or 'EXPECT_NOT_EXIST') and a column value condition.
``return_type``: The type of data to return. This is an instance of the tablestore.metadata.ReturnType class. Currently, only PrimaryKey is supported, which is typically used with an auto-increment primary key column.
Returns: The capacity units consumed and the requested row data (return_row).
consumed: An instance of the tablestore.metadata.CapacityUnit class that represents the consumed capacity units.
return_row: The requested row data.
Example:
    primary_key = [('gid',1), ('uid',101)]
    update_of_attribute_columns = {
        'put' : [('name','Jack'), ('address','China, City B')],
        'delete' : [('mobile', None, 1493725896147)],
        'delete_all' : [('age')],
        'increment' : [('counter', 1)]
    }
    row = Row(primary_key, update_of_attribute_columns)
    condition = Condition('EXPECT_EXIST')
    consumed, return_row = client.update_row('myTable', row, condition)
"""
def update_row(self, table_name, row, condition, return_type = None, transaction_id = None):

Parameters

Parameter

Description

table_name

The name of the data table.

row

The row data. This includes the following parameters:

  • primary_key: The primary key of the row. A primary key consists of a column name, column type, and column value.

    Important
    • The number and data types of the primary key columns must match the schema of the data table.

    • If you use an auto-increment primary key column, set the value of that column to a placeholder. For more information, see Auto-increment primary key column.

  • update_of_attribute_columns: The attribute columns to update.

    • To add or update data, you need to specify the attribute name, attribute value, attribute type (optional), and timestamp (optional).

      For more information about attribute column names and data types, see Naming conventions and data types.

      The timestamp serves as the data version. You can have the system generate a version number automatically or specify a custom one. For more information, see Data versions and TTL.

      • If the system generates the data version, it uses the current timestamp in milliseconds (the number of milliseconds that have elapsed since 00:00:00 UTC on January 1, 1970).

      • If you specify a custom data version, it must be a 64-bit integer representing a timestamp in milliseconds and must be within the valid version range.

    • To delete a specific version of an attribute column, you only need to specify the attribute name and timestamp.

      The timestamp must be a 64-bit integer in milliseconds that identifies the specific version of the data.

    • To delete an entire attribute column and all its versions, you only need to specify the attribute name.

      Note

      Deleting all attribute columns of a row is not the same as deleting the row itself. To delete a row, use the DeleteRow operation.

condition

A conditional update allows you to set a condition based on row existence or the value of a specific column.

return_type

The type of data to return. This is an instance of the tablestore.metadata.ReturnType class. Currently, only PrimaryKey is supported, which is typically used with an auto-increment primary key column.

transaction_id

The ID of the local transaction. This parameter is required when you use the local transaction feature.

Example

Update a row of data.

# Set the data table name.
table_name = '<TABLE_NAME>'

# The primary key consists of two columns: 'gid' with an integer value of 1,
# and 'uid' with an integer value of 101.
primary_key = [('gid',1), ('uid',101)]
# The update consists of 'PUT', 'DELETE', and 'DELETE_ALL' operations.
# 'PUT': Adds or updates column values. This example adds two columns: 'name' with the value 'David' and 'address' with the value 'Hongkong'.
# 'DELETE': Deletes a specific version of a column. This example deletes the version '1488436949003' of the 'address' column.
# 'DELETE_ALL': Deletes all versions of a column. This example deletes the 'mobile' and 'age' columns entirely.
update_of_attribute_columns = {
    'PUT' : [('name','David'), ('address','Hongkong')],
    'DELETE' : [('address', None, 1488436949003)],
    'DELETE_ALL' : [('mobile'), ('age')],
}
row = Row(primary_key, update_of_attribute_columns)

# The row existence condition is set to IGNORE, so the update occurs regardless of whether the row exists.
# A column condition is also set to update the row only if the 'age' column has a value of 20.
condition = Condition(RowExistenceExpectation.IGNORE, SingleColumnCondition("age", 20, ComparatorType.EQUAL)) # update row only when age is 20
try:
    consumed, return_row = client.update_row(table_name, row, condition)
    
    # Print the consumed write capacity units for this request.
    print('update row succeed, consume %s write cu.' % consumed.write)
# Client exception: typically caused by a parameter error or network issue.
except OTSClientError as e:
    print("update row failed, http_status:%d, error_message:%s" % (e.get_http_status(), e.get_error_message()))
# Server exception: typically caused by a parameter error or throttling.
except OTSServiceError as e:
    print("update row failed, http_status:%d, error_code:%s, error_message:%s, request_id:%s" % (e.get_http_status(), e.get_error_code(), e.get_error_message(), e.get_request_id()))

For the complete code, see UpdateRow@GitHub.

Batch write data

Call the BatchWriteRow operation to perform multiple write operations on one or more data tables in a single request.

Each sub-operation in a BatchWriteRow request runs independently. Tablestore returns the execution result for each sub-operation.

Usage notes

  • When you call the BatchWriteRow operation, some sub-operations may fail. The operation does not throw an exception for such partial failures. Instead, the BatchWriteRowResponse object reports the status of each sub-operation. Therefore, you must always check the response to identify any failures.

    If the server detects a parameter error in any sub-operation, the BatchWriteRow operation may throw an exception. In this case, none of the sub-operations in the request are executed.

  • If you call add multiple times for the same data table in a request, only the final add call takes effect.

API

"""
Description: Modifies multiple rows of data in batches.
request = BatchWriteRowRequest()
request.add(TableInBatchWriteRowItem(table0, row_items))
request.add(TableInBatchWriteRowItem(table1, row_items))
response = client.batch_write_row(request)
``response`` is the returned result, of type tablestore.metadata.BatchWriteRowResponse.
"""
def batch_write_row(self, request):                   

Example

Write data in batches.

put_row_items = []
# Add rows for the PutRow operation.
for i in range(0, 20):
    primary_key = [('gid',i), ('uid',i+1)]
    attribute_columns = [('name','somebody'+str(i)), ('address','somewhere'+str(i)), ('age',i)]
    row = Row(primary_key, attribute_columns)
    condition = Condition(RowExistenceExpectation.IGNORE)
    item = PutRowItem(row, condition)
    put_row_items.append(item)

# Add rows for the UpdateRow operation.
for i in range(0, 10):
    primary_key = [('gid',i), ('uid',i+1)]
    attribute_columns = {'PUT': [('name','somebody'+str(i)), ('address','somewhere'+str(i)), ('age',i)]}
    row = Row(primary_key, attribute_columns)
    condition = Condition(RowExistenceExpectation.IGNORE, SingleColumnCondition("age", i, ComparatorType.EQUAL))
    item = UpdateRowItem(row, condition)
    put_row_items.append(item)

# Add rows for the DeleteRow operation.
delete_row_items = []
for i in range(10, 20):
    primary_key = [('gid',i), ('uid',i+1)]
    row = Row(primary_key)
    condition = Condition(RowExistenceExpectation.IGNORE)
    item = DeleteRowItem(row, condition)
    delete_row_items.append(item)

# Construct a batch write request.
request = BatchWriteRowRequest()

# If you call the add operation multiple times for the same table, only the last add request takes effect.
request.add(TableInBatchWriteRowItem('<TABLE_NAME>', put_row_items))
request.add(TableInBatchWriteRowItem('<DELETE_TABLE_NAME>', delete_row_items))

# Example of performing multiple operations on the same table.
# table_batch_items = TableInBatchWriteRowItem('<TABLE_NAME>', put_row_items)
# table_batch_items.row_items += delete_row_items
# request.add(table_batch_items)
# Call the batch_write_row method to perform the batch write.
# An exception is thrown for request parameter errors.
# For failures of individual rows, no exception is thrown; instead, the status is returned in the corresponding result item.
try:
    result = client.batch_write_row(request)
    print('Result status: %s'%(result.is_all_succeed()))

    # Check the results for Put rows.
    print('check first table\'s put results:')
    succ, fail = result.get_put()
    for item in succ:
        print('Put succeed, consume %s write cu.' % item.consumed.write)
    for item in fail:
        print('Put failed, error code: %s, error message: %s' % (item.error_code, item.error_message))

    # Check the results for Update rows.
    print('check first table\'s update results:')
    succ, fail = result.get_update()
    for item in succ:
        print('Update succeed, consume %s write cu.' % item.consumed.write)
    for item in fail:
        print('Update failed, error code: %s, error message: %s' % (item.error_code, item.error_message))

    # Check the results for Delete rows.
    print('check second table\'s delete results:')
    succ, fail = result.get_delete()
    for item in succ:
        print('Delete succeed, consume %s write cu.' % item.consumed.write)
    for item in fail:
        print('Delete failed, error code: %s, error message: %s' % (item.error_code, item.error_message)) 
# Client exception: typically caused by a parameter error or network issue.
except OTSClientError as e:
    print("batch write row failed, http_status:%d, error_message:%s" % (e.get_http_status(), e.get_error_message()))
# Server exception: typically caused by a parameter error or throttling.
except OTSServiceError as e:
    print("batch write row failed, http_status:%d, error_code:%s, error_message:%s, request_id:%s" % (e.get_http_status(), e.get_error_code(), e.get_error_message(), e.get_request_id()))

For the complete code, see BatchWriteRow@GitHub.

FAQ

Related topics

  • To update data based on specified conditions in a high-concurrency application, you can use a conditional update. For more information, see Conditional update.

  • To implement real-time statistics, such as tracking post page views (PVs), use an atomic counter. For more information, see Atomic counter.

  • To perform atomic write operations on one or more rows, use a local transaction. For more information, see Local transaction.

  • After writing data, you can read or delete it from the table. For more information, see Read data or Delete data.