Learn about basic functionality of dense arrays within TileDB, from ingestion to querying and beyond.
How to run this tutorial
You can run this tutorial in two ways:
Locally on your machine.
On TileDB Cloud.
However, since TileDB Cloud has a free tier, we strongly recommend that you sign up and run everything there, as that requires no installations or deployment.
This tutorial explains the most basic functionality of sparse arrays, by creating a small 2×2 dense array and reading it.
First, import the necessary libraries, set the array URI (that is, its path, which in this tutorial will be on local storage), and delete any previously created arrays with the same name.
# Create the two dimensionsd1 = tiledb.Dim(name="d1", domain=(0, 3), tile=2, dtype=np.int32)d2 = tiledb.Dim(name="d2", domain=(0, 3), tile=2, dtype=np.int32)# Create a domain using the two dimensionsdom = tiledb.Domain(d1, d2)# Order of the dimensions matters when slicing subarrays.# Remember to give priority to more selective dimensions to# maximize the pruning power during slicing.# Create an attributea = tiledb.Attr(name="a", dtype=np.int32)# Create the array schema with `sparse=True`.# Set `cell_order` to 'row-major' (default) or 'C', 'col-major' or 'F', or 'hilbert'.# Set `tile_order` to 'row-major' (default) or 'C', 'col-major' or 'F'.sch = tiledb.ArraySchema(domain=dom, sparse=True, attrs=[a])# Create the array on disk (it will initially be empty)tiledb.Array.create(array_uri, sch)
# Create the two dimensionsd1 <-tiledb_dim("d1", c(0L, 3L), 2L, "INT32")d2 <-tiledb_dim("d2", c(0L, 3L), 2L, "INT32")# Create a domain using the two dimensionsdom <-tiledb_domain(dims =c(d1, d2))# Create an attributea <-tiledb_attr("a", type ="INT32")# Create the array schema with `sparse = TRUE`sch <-tiledb_array_schema(dom, a, sparse =TRUE)# Create the array on disk (it will initially be empty)arr <-tiledb_array_create(sparse_array, sch)
Note that you can specify tiling for each dimension, whereas you can set the cell and tile orders in the array schema. Tiling, tile order, and cell order collectively define the data layout on storage, which plays an important role in performance. For more information, visit the following sections:
Populate the TileDB array with a set of 1D input arrays: one for the coordinates of each dimension, and one for the attribute values. TileDB sparse arrays expect the coordinate (COO) format.
# Prepare some data in numpy arraysd1_data = np.array([2, 0, 3, 2, 0, 1], dtype=np.int32)d2_data = np.array([0, 1, 1, 2, 3, 3], dtype=np.int32)a_data = np.array([4, 1, 6, 5, 2, 3], dtype=np.int32)# Open the array in write mode and write the data in COO formatwith tiledb.open(array_uri, "w") as A: A[d1_data, d2_data] = a_data
# Prepare some data in an arrayd1_data <-c(2L, 0L, 3L, 2L, 0L, 1L)d2_data <-c(0L, 1L, 1L, 2L, 3L, 3L)a_data <-c(4L, 1L, 6L, 5L, 2L, 3L)# Open the array for writing and write data to the arrayarr <-tiledb_array(uri = sparse_array,query_type ="WRITE",return_as ="data.frame")arr[d1_data, d2_data] <- a_data# Close the arrayinvisible(tiledb_array_close(arr))
The array is a folder in the path specified in array_uri. You can learn about the different contents of the array folder in other sections of the Academy.
# Open the array in read modeA = tiledb.open(array_uri, "r")# Return the non-empty domain of the arrayprint("Non-empty domain: ")print(A.nonempty_domain())print("\n")# Show the entire arrayprint("Entire array: ")print(A[:])print("\n")# Show the 'a' attribute of the arrayprint("Attribute 'a': ")print(A[:]["a"])print("\n")# Slice a portion of the array, which is useful# when the arrays are too big to fit in main memoryprint("Slice [0:2), [0:3): ")print(A[0:2, 0:3])print("\n")# Slice a multi-range subarray# note multi_index uses closed rangesprint("Multi-range, rows 0,1 and 3, and columns 0-2: ")print(A.multi_index[[slice(0, 1), 3], slice(0, 2)])# Remember to close the arrayA.close()
# Open the array in read modeinvisible(tiledb_array_open(arr, type ="READ"))# Show the entire arraycat("Entire array:\n")print(arr[])# Show the 'a' attribute of the arraycat("Attribute 'a':\n")print(arr[]["a"])# Slice a portion of the array, which is useful# when the arrays are too big to fit in main memorycat("Slice [0:2], [0:3]:\n")print(arr[0:2, 0:3]["a"])# Close the arrayinvisible(tiledb_array_close(arr))