Writing Sparse Cells

This is applicable to both dense and sparse arrays (see Sparse Writes for more details). You can write sparse cells in an array as follows:

C
C++
Python
R
Java
Go
C
#include <tiledb/tiledb.h>
// Create TileDB context
tiledb_ctx_t* ctx;
tiledb_ctx_alloc(NULL, &ctx);
// Open array for writing
tiledb_array_t* array;
tiledb_array_alloc(ctx, array_name, &array);
tiledb_array_open(ctx, array, TILEDB_WRITE);
// Prepare some data for the array
long long coords[] = {3, 3, 4, 4, 1, 2, 2, 1};
unsigned long long coords_size = sizeof(coords);
int data[] = {3, 4, 1, 2};
unsigned long long data_size = sizeof(data);
// Create the query
tiledb_query_t* query;
tiledb_query_alloc(ctx, array, TILEDB_WRITE, &query);
tiledb_query_set_layout(ctx, query, TILEDB_UNORDERED);
tiledb_query_set_buffer(ctx, query, TILEDB_COORDS, coords, &coords_size);
tiledb_query_set_buffer(ctx, query, "a", data, &data_size);
// NOTE: If the array has more than one attributes, you should
// set the a buffer for every attribute with tiledb_query_set_buffer.
// You could have set the query layout to TILEDB_GLOBAL_ORDER,
// and set the coords vector to {1, 2, 2, 1, 3, 3, 4, 4} and
// the data vector to {1, 2, 3, 4}, and the result would be the same
// Submit query
tiledb_query_submit(ctx, query);
// Close array
tiledb_array_close(ctx, array);
// Clean up
tiledb_array_free(&array);
tiledb_query_free(&query);
tiledb_ctx_free(&ctx);
C++
#include <tiledb/tiledb>
// Create context
Context ctx;
// Prepare some data for the array
std::vector<int64_t> coords = {3, 3, 4, 4, 1, 2, 2, 1};
std::vector<int> data = {3, 4, 1, 2};
// Open array for writing
Array array(ctx, array_name, TILEDB_WRITE);
// Create the query
Query query(ctx, array);
query.set_layout(TILEDB_UNORDERED)
.set_coords(coords)
.set_buffer("a", data);
// NOTE: If the array has more than one attributes, you should
// set the a buffer for every attribute with set_buffer.
// You could have set the query layout to TILEDB_GLOBAL_ORDER,
// and set the coords vector to {1, 2, 2, 1, 3, 3, 4, 4} and
// the data vector to {1, 2, 3, 4}, and the result would be the same
// Submit query
query.submit();
// Close the array
array.close();
Python
import tiledb
import numpy as np
c1 = np.array([3,4,1,2])
c2 = np.array([3,4,2,1])
data = np.array([3,4,1,2])
with tiledb.SparseArray(array_name, 'w') as A:
A[c1, c2] = data
R
# Prepare some data
data <- c(3L, 4L, 1L, 2L)
I <- c(3, 4, 1, 2)
J <- c(3, 4, 2, 1)
# Open the array and write the data to it
A <- tiledb_sparse(uri = array_name)
A[I, J] <- data
Java
// Create context, array and query
try(Context ctx = new Context(),
Array array = new Array(ctx, array_name, TILEDB_WRITE),
Query query = new Query(ctx, array);) {
// Prepare some data for the array
NativeArray coords = new NativeArray(ctx, new long[] {3l, 3l, 4l, 4l, 1l, 2l, 2l, 1l}, Long.class);
NativeArray data = new NativeArray(ctx, new int[] {3, 4, 1, 2}, Integer.class);
// Create the query
query.setLayout(TILEDB_ROW_MAJOR)
.setCoords(coords)
.setBuffer("a", data);
// NOTE: If the array has more than one attributes, you should
// set the a buffer for every attribute with set_buffer.
// You could have set the query layout to TILEDB_GLOBAL_ORDER,
// and set the coords vector to {1, 2, 2, 1, 3, 3, 4, 4} and
// the data vector to {1, 2, 3, 4}, and the result would be the same
// Submit query
query.submit();
}
Go
import tiledb "github.com/TileDB-Inc/TileDB-Go"
// Create context
Context ctx;
// Prepare some data for the array
coords := []int64{3, 3, 4, 4, 1, 2, 2, 1}
data := []int32{3, 4, 1, 2}
// Open array for writing
array, _ := tiledb.NewArray(ctx, array_name)
array.Open(tiledb.TILEDB_WRITE)
// Create the query
query, _ := tiledb.NewQuery(ctx, array)
query.SetLayout(tiledb.TILEDB_UNORDERED)
query.SetCoordinates(coords)
query.SetBuffer("a", data)
// NOTE: If the array has more than one attributes, you should
// set the a buffer for every attribute with set_buffer.
// You could have set the query layout to TILEDB_GLOBAL_ORDER,
// and set the coords vector to {1, 2, 2, 1, 3, 3, 4, 4} and
// the data vector to {1, 2, 3, 4}, and the result would be the same
// Submit query
query.Submit()
// Close the array
array.Close();

The code snippets above write the sparse cells shown in the figure below, assuming a 2D array with a int64 domain [1, 4] x [1, 4] , 2x2 space tiling, row-major tile and cell order, and a single int32 attribute called a. The unordered layout means that you do no have to worry about the order of the values when writing them. Since you must explicitly set the coordinates, TileDB knows how to properly sort on the global order before writing them cells to persistent storage. If you switch to global order, make sure to organize your cells properly before writing (see figure below).

Currently, if your array consists of more than one attributes, TileDB requires you to provide values for all the attributes in each write operation.