Insert a row
|
Tables with the Data API are currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms. |
Inserts a single row into a table.
This method can insert a row in an existing CQL table, but the Data API does not support all CQL data types or modifiers. For more information, see Data types in tables.
For general information about working with tables and rows, see About tables with the Data API.
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
-
Python
-
TypeScript
-
Java
-
curl
Inserts the specified row and returns a TableInsertOneResult object that includes the primary key of the inserted row as a dictionary and as a tuple.
If a row with the specified primary key already exists in the table, the row will be overwritten with the specified column values. Unspecified columns will remain unchanged.
If any of the inserted columns use the wrong datatype or improper encoding, then the entire insert fails.
Inserts the specified row and returns a promise that resolves to a TableInsertOneResult<PKey> object that includes the primary key of the inserted row.
The primary key type is inferred from the PKey of the table’s type. If it cannot be inferred from the PKey, it is instead inferred from Partial<Schema>.
If a row with the specified primary key already exists in the table, the row will be overwritten with the specified column values. Unspecified columns will remain unchanged.
If any of the inserted columns use the wrong datatype or improper encoding, then the entire insert fails.
Inserts the specified row and returns a TableInsertOneResult instance that includes the primary key of the inserted row and the schema of the primary key.
If a row with the specified primary key already exists in the table, the row will be overwritten with the specified column values. Unspecified columns will remain unchanged.
If any of the inserted columns use the wrong datatype or improper encoding, then the entire insert fails.
Inserts the specified row. In the JSON response, status.primaryKeySchema is an object that describes the table’s primary key definition, including column names and types. status.insertedIds is a nested array that contains the values inserted for each primary key column.
If a row with the specified primary key already exists in the table, the row will be overwritten with the specified column values. Unspecified columns will remain unchanged.
If any of the inserted columns use the wrong datatype or improper encoding, then the entire insert fails.
Example response for a single-column primary key:
{
"status": {
"primaryKeySchema": {
"email": {
"type": "ascii"
}
},
"insertedIds": [
[
"tal@example.com"
]
]
}
}
Example response for a multi-column primary key:
{
"status": {
"primaryKeySchema": {
"email": {
"type": "ascii"
},
"graduation_year": {
"type": "int"
}
},
"insertedIds": [
[
"tal@example.com",
2014
]
]
}
}
Parameters
-
Python
-
TypeScript
-
Java
-
curl
Use the insert_one method, which belongs to the astrapy.Table class.
Method signature
insert_one(
row: Dict[str, Any],
*,
general_method_timeout_ms: int,
request_timeout_ms: int,
timeout_ms: int,
) -> TableInsertOneResult:
| Name | Type | Summary |
|---|---|---|
|
|
A dictionary that defines the row to insert. All primary key values are required.
Any unspecified columns are set to The table definition determines the columns in the row, the type for each column, and the primary key. To get this information, see List table metadata. |
|
|
Optional. The maximum time, in milliseconds, that the client should wait for the underlying HTTP request. This parameter is aliased as Default: The default value for the table. This default is 30 seconds unless you specified a different default when you initialized the |
Use the insertOne method, which belongs to the Table class.
Method signature
async insertOne(
row: Schema,
options?: {
timeout?: number | TimeoutDescriptor,
},
): TableInsertOneResult<PKey>
| Name | Type | Summary |
|---|---|---|
|
|
An object that defines the row to insert. All primary key values are required.
Any unspecified columns are set to The table definition determines the columns in the row, the type for each column, and the primary key. To get this information, see List table metadata. |
|
|
Optional. A timeout to impose on the underlying API request. |
Use the insertOne method, which belongs to the com.datastax.astra.client.tables.Table class.
Method signature
TableInsertOneResult insertOne(T row)
TableInsertOneResult insertOne(
T row,
TableInsertOneOptions insertOneOptions
)
| Name | Type | Summary |
|---|---|---|
|
|
An object that defines the row to insert.
The All primary key values are required.
Any unspecified columns are set to The table definition determines the columns in the row, the type for each column, and the primary key. To get this information, see List table metadata. |
|
Optional. The options for this operation, including the timeout. |
Use the insertOne command.
Command signature
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": ROW
}
}'
| Name | Type | Summary |
|---|---|---|
|
|
An object that defines the row to insert. All primary key values are required.
Any unspecified columns are set to The table definition determines the columns in the row, the type for each column, and the primary key. To get this information, see List table metadata. |
Examples
The following examples demonstrate how to insert a row into a table.
Insert a row
When you insert a row, you must specify a non-null value for each primary key column.
Non-primary key columns are optional, and any unspecified non-primary key columns are set to null.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.data_types import (
DataAPISet,
DataAPIDate,
)
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")
# Insert a row into the table
result = table.insert_one(
{
"title": "Computed Wilderness",
"author": "Ryan Eau",
"number_of_pages": 432,
"due_date": DataAPIDate.from_string("2024-12-18"),
"genres": DataAPISet(["History", "Biography"]),
}
)
import {
DataAPIClient,
date,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_NAME",
});
// Insert a row into the table
(async function () {
const result = await table.insertOne({
title: "Computed Wilderness",
author: "Ryan Eau",
number_of_pages: 432,
due_date: date("2024-12-18"),
genres: new Set(["History", "Biography"]),
});
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.results.TableInsertOneResult;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;
public class Example {
public static void main(String[] args) {
// Get an existing table
DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
Table<Row> table = database.getTable("TABLE_NAME");
// Insert a row into the table
Calendar calendar = Calendar.getInstance();
calendar.set(2024, Calendar.DECEMBER, 18);
Date date = calendar.getTime();
Row row =
new Row()
.addText("title", "Computed Wilderness")
.addText("author", "Ryan Eau")
.addInt("number_of_pages", 432)
.addDate("due_date", date)
.addSet("genres", Set.of("History", "Biography"));
TableInsertOneResult result = table.insertOne(row);
System.out.println(result.getInsertedId());
}
}
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"title": "Computed Wilderness",
"author" :"Ryan Eau",
"number_of_pages": 432,
"due_date": "2024-12-18",
"genres": ["History", "Biography"]
}
}
}'
Insert a row with vector embeddings
You can only insert vector embeddings into vector columns.
To create a table with a vector column, see Create a table. To add a vector column to an existing table, see Alter a table.
All embeddings in the column should use the same provider, model, and dimensions. Mismatched embeddings can cause inaccurate vector searches.
-
Python
-
TypeScript
-
Java
-
curl
You can use the astrapy.data_types.DataAPIVector class to binary-encode your vector embeddings.
DataStax recommends that you always use a DataAPIVector object instead of a list of floats to improve performance.
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.data_types import (
DataAPIVector,
)
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")
# Insert a row into the table
result = table.insert_one(
{
"title": "Computed Wilderness",
"author": "Ryan Eau",
"summary_genres_vector": DataAPIVector([0.12, -0.46, 0.35, 0.52, -0.32]),
}
)
You can use the DataAPIVector class to binary-encode your vector embeddings.
DataStax recommends that you always use a DataAPIVector object instead of a list of floats to improve performance.
import {
DataAPIClient,
DataAPIVector,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_NAME",
});
// Insert a row into the table
(async function () {
const result = await table.insertOne({
title: "Computed Wilderness",
author: "Ryan Eau",
summary_genres_vector: new DataAPIVector([0.12, -0.46, 0.35, 0.52, -0.32]),
});
})();
You can use the DataAPIVector class to binary-encode your vector embeddings.
DataStax recommends that you always use a DataAPIVector object instead of a list of floats to improve performance.
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.vector.DataAPIVector;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.results.TableInsertOneResult;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
Table<Row> table = database.getTable("TABLE_NAME");
// Insert a row into the table
Row row =
new Row()
.addText("title", "Computed Wilderness")
.addText("author", "Ryan Eau")
.addVector(
"summary_genres_vector",
new DataAPIVector(new float[] {0.12f, -0.46f, 0.35f, 0.52f, -0.32f}));
TableInsertOneResult result = table.insertOne(row);
System.out.println(result.getInsertedId());
}
}
You can provide the vector embeddings as an array of floats, or you can use $binary to provide the vector embeddings as a Base64-encoded string.
$binary can be more performant.
Vector binary encodings specification
A d-dimensional vector is a list of d floating-point numbers that can be binary encoded.
To prepare for encoding, the list must be transformed into a sequence of bytes where each float is represented as four bytes in big-endian format.
Then, the byte sequence is Base64-encoded, with = padding, if needed.
For example, here are some vectors and their resulting Base64 encoded strings:
[0.1, -0.2, 0.3] = "PczMzb5MzM0+mZma" [0.1, 0.2] = "PczMzT5MzM0=" [10, 10.5, 100, -91.19] = "QSAAAEEoAABCyAAAwrZhSA=="
Once encoded, you use $binary to pass the Base64 string to the Data API:
{ "$binary": "BASE64_STRING" }
You can use a script to encode your vectors, for example:
python
import base64
import struct
input_vector = [0.1, -0.2, 0.3]
d = len(input_vector)
pack_format = ">" + "f" * d
binary_encode = base64.b64encode(struct.pack(pack_format, *input_vector)).decode()
-
Array of floats
-
$binary
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"title": "Computed Wilderness",
"author" :"Ryan Eau",
"summary_genres_vector": [0.12, -0.46, 0.35, 0.52, -0.32]
}
}
}'
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"title": "Computed Wilderness",
"author" :"Ryan Eau",
"summary_genres_vector": {"$binary": "PfXCjz8FHrg+o9cK"}
}
}
}'
Client reference
-
Python
-
TypeScript
-
Java
-
curl
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.
Client reference documentation is not applicable for HTTP.