Writing in Dense Subarrays
This is applicable only to dense arrays (see Dense Writes for more details). You can write into a subarray of the array domain as follows:
#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
int data[] = {1, 2, 3, 4, 5, 6};
unsigned long long data_size = sizeof(data);
// Set the subarray to write into
tiledb_subarray_t* subarray;
tiledb_subarray_alloc(ctx, array, &subarray);
int subarrayData[] = {1, 2, 2, 4};
tiledb_subarray_set_subarray(subarray, subarrayData);
// Create the query
tiledb_query_t* query;
tiledb_query_alloc(ctx, array, TILEDB_WRITE, &query);
tiledb_query_set_layout(ctx, query, TILEDB_ROW_MAJOR);
tiledb_query_set_subarray_t(ctx, query, subarray);
tiledb_query_set_data_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_data_buffer.
// You could have set the query layout to TILEDB_COL_MAJOR,
// and set the data vector to {1, 4, 2, 5, 3, 6},
// and the result would be the same
// You could have set the query layout to TILEDB_GLOBAL_ORDER,
// and set the data vector to {INT_MIN, 1, INT_MIN, 4, 2, 3, 5, 6},
// and the result would be the same
// Submit query
tiledb_query_submit(ctx, query);
// Close array
tiledb_array_close(ctx, array);
// Clean up
tiledb_subarray_free(&subarray);
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<int> data = {1, 2, 3, 4, 5, 6};
// Open array for writing
Array array(ctx, array_name, TILEDB_WRITE);
// Set the subarray to write into
Subarray subarray(ctx, array);
subarray.set_subarray({1, 2, 2, 4});
// Create the query
Query query(ctx, array);
query.set_layout(TILEDB_ROW_MAJOR)
.set_data_buffer("a", data)
.set_subarray(subarray);
// NOTE: If the array has more than one attributes, you should
// set the a buffer for every attribute with set_data_buffer.
// You could have set the query layout to TILEDB_COL_MAJOR,
// and set the data vector to {1, 4, 2, 5, 3, 6},
// and the result would be the same
// You could have set the query layout to TILEDB_GLOBAL_ORDER,
// and set the data vector to {INT_MIN, 1, INT_MIN, 4, 2, 3, 5, 6},
// and the result would be the same
// Submit query
query.submit();
// Close the array
array.close();
import tiledb
import numpy as np
data = np.array([[1,2],[3,4],[5,6]])
with tiledb.open(array_name, 'w') as A:
A[1:3,2:5] = data
# Prepare a 4x3 dense array
# Contrary to Python, R by default stores arrays in col-major order
data <- array(c(1L, 4L, 2L, 5L, 3L, 6L), dim=c(2,3))
# Prepare the [1,2] x [2,4] subarray to write to
I <- 1:2
J <- 2:4
# Open the array and write the data to it
A <- tiledb_array(uri = array_name)
A[I, J] <- data
// 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 subarray = new NativeArray(ctx, new int[] {1, 2, 2, 4}, Integer.class);
NativeArray data = new NativeArray(ctx, new int[] {1, 2, 3, 4, 5, 6}, Integer.class);
// Create the query
query.setLayout(TILEDB_ROW_MAJOR)
.setSubarray(subarray)
.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_COL_MAJOR,
// and set the data vector to {1, 4, 2, 5, 3, 6},
// and the result would be the same
// You could have set the query layout to TILEDB_GLOBAL_ORDER,
// and set the data vector to {INT_MIN, 1, INT_MIN, 4, 2, 3, 5, 6},
// and the result would be the same
// Submit query
query.submit();
}
import "github.com/TileDB-Inc/TileDB-Go"
// Create context
ctx, _ := tiledb.NewContext(nil)
// Prepare some data for the array
data := []int32{1, 2, 3, 4, 5, 6}
// Set the subarray to write into
subarray := []int32{1, 2, 2, 4}
// 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_ROW_MAJOR)
query.SetBuffer("a", data)
query.SetSubArray(subarray)
// 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_COL_MAJOR,
// and set the data vector to {1, 4, 2, 5, 3, 6},
// and the result would be the same
// You could have set the query layout to TILEDB_GLOBAL_ORDER,
// and set the data vector to {INT_MIN, 1, INT_MIN, 4, 2, 3, 5, 6},
// and the result would be the same
// Submit query
query.Submit()
// Close the array
query.Finalize()
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
int[] data = new int[] { 1, 2, 3, 4, 5, 6 };
// Set the subarray to write into
using Subarray subarray = new Subarray(array);
subarray.SetSubarray(1, 2, 2, 4);
// Create the query
using Query query = new Query(ctx, array, QueryType.Write);
query.SetLayout(LayoutType.RowMajor);
query.SetSubarray(subarray);
query.SetDataBuffer("a", data);
// NOTE: If the array has more than one attributes, you should
// set the a buffer for every attribute with SetDataBuffer.
// You could have set the query layout to ColumnMajor,
// and set the data buffer to {1, 4, 2, 5, 3, 6},
// and the result would be the same.
// You could have set the query layout to GlobalOrder,
// and set the data buffer to {int.MinValue, 1, int.MinValue, 4, 2, 3, 5, 6},
// and the result would be the same.
// Submit query
query.Submit();
// Close array
array.Close();
The code snippets above create the dense fragment shown in the figure below, assuming a 2D array with domain [1, 4] x [1, 4]
, 2x2
space tiling, row-major tile and cell order, and a single int32
attribute called a
. Observe that you can select the layout in which you can write the cells into. If you switch to column-major or 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.
Last updated