Ask or search…
K
Links

Writing Sparse Cells

This is applicable to 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 d1[] = {3, 4, 1, 2};
unsigned long long d1_size = sizeof(d1);
long long d2[] = {3, 4, 2, 1};
unsigned long long d2_size = sizeof(d2);
int a[] = {3, 4, 1, 2};
unsigned long long a_size = sizeof(a);
// 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_data_buffer(ctx, query, "d1", d1, &d1_size);
tiledb_query_set_data_buffer(ctx, query, "d2", d2, &d2_size);
tiledb_query_set_data_buffer(ctx, query, "a", a, &a_size);
// 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);
#include <tiledb/tiledb>
using namespace tiledb;
// Create context
Context ctx;
// Prepare some data for the array
std::vector<int64_t> d1 = {3, 4, 1, 2};
std::vector<int64_t> d2 = {3, 4, 2, 1};
std::vector<int> a = {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_buffer("d1", d1)
.set_buffer("d2", d2)
.set_buffer("a", a);
// Submit query
query.submit();
// Close the array
array.close();
import tiledb
import numpy as np
d1 = np.array([3,4,1,2])
d2 = np.array([3,4,2,1])
a = np.array([3,4,1,2])
with tiledb.open(array_name, 'w') as A:
A[d1, d2] = a
# Prepare some data
a <- c(3L, 4L, 1L, 2L)
d1 <- c(3, 4, 1, 2)
d2 <- c(3, 4, 2, 1)
# Open the array and write the data to it
A <- tiledb_array(uri = array_name, is.sparse = TRUE)
A[d1, d2] <- a
// 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 d1 = new NativeArray(ctx, new long[] {3l, 4l, 1l, 2l}, Long.class);
NativeArray d2 = new NativeArray(ctx, new long[] {3l, 4l, 2l, 1l}, Long.class);
NativeArray a = new NativeArray(ctx, new int[] {3, 4, 1, 2}, Integer.class);
// Create the query
query.setLayout(TILEDB_ROW_MAJOR)
.setBuffer("d1", d1)
.setBuffer("d2", d2)
.setBuffer("a", a);
// Submit query
query.submit();
}
import tiledb "github.com/TileDB-Inc/TileDB-Go"
// Create context
Context ctx;
// Prepare some data for the array
d1 := []int64{3, 4, 1, 2}
d2 := []int64{3, 4, 2, 1}
a := []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)
_, err = query.SetBuffer("d1", d1)
_, err = query.SetBuffer("d2", d2)
_, err = query.SetBuffer("a", a)
// Submit query
query.Submit()
// Close the array
array.Close();
using TileDB.CSharp;
// Create TileDB context
using Context ctx = new Context();
// Open array for writing
using Array array = new Array(ctx, array_name);
array.Open(QueryType.Write);
// Prepare some data for the array
long[] d1 = new long[] { 3, 1, 4, 2 };
long[] d2 = new long[] { 3, 4, 2, 1 };
int[] a = new int[] { 3, 4, 1, 2 };
// Create the query
using Query query = new Query(ctx, array, QueryType.Write);
query.SetLayout(LayoutType.Unordered);
query.SetDataBuffer("d1", d1);
query.SetDataBuffer("d2", d2);
query.SetDataBuffer("a", a);
// Submit query
query.Submit();
// Close 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.