Array metadata is a powerful way to assign key-value information to an array. Learn how to work with array metadata in this tutorial.
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 covers the basics of writing arbitrary metadata to an array. The example focuses on a dense array, but it applies identically to sparse arrays as well.
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=(1, 4), tile=2, dtype=np.int32)d2 = tiledb.Dim(name="d2", domain=(1, 4), tile=2, dtype=np.int32)# Create a domain using the two dimensionsdom = tiledb.Domain(d1, d2)# Create an attributea = tiledb.Attr(name="a", dtype=np.int32)# Create the array schema, setting `sparse=False` to indicate a dense arraysch = tiledb.ArraySchema(domain=dom, sparse=False, 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(1L, 4L), 2L, "INT32")d2 <-tiledb_dim("d2", c(1L, 4L), 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, setting `sparse = FALSE` to indicate a dense arraysch <-tiledb_array_schema(dom, a, sparse =FALSE)# Create the array on disk (it will initially be empty)arr <-tiledb_array_create(array_uri, sch)
You can write arbitrary metadata to an array as follows.
# Open the array in write mode and write some metadatawith tiledb.open(array_uri, "w") as A: A.meta["aaa"] =1.1 A.meta["bbb"] =2 A.meta["ccc"] ="hello!"# multiple values of the same type# may be written as a tuple: A.meta["tuple_int"] = (1, 2, 3, 4)# or list: A.meta["list_float"] = [1.0, 2.1, 3.2, 4.3]
array <-tiledb_array( array_uri,query_type ="WRITE",keep_open =TRUE,return_as ="data.frame")tiledb_put_metadata(array, "aaa", 1.1)tiledb_put_metadata(array, "bbb", 2L)tiledb_put_metadata(array, "ccc", "hello!")# Multiple values of the same type# may be written as a vector:tiledb_put_metadata(array, "vector_float", c(1.0, 2.1, 3.2, 4.3))
You can also delete already written metadata as follows.
invisible(tiledb_array_open(array, type ="READ"))# Print all metadataprint(tiledb_get_all_metadata(array))# Read "aaa" and print its valueprint(paste("aaa:", tiledb_get_metadata(array, "aaa")))# Read the "vector_float" keyprint(paste("vector_float:", tiledb_get_metadata(array, "vector_float")))
# Open the array for readingwith tiledb.open(array_uri) as A:# print the keysprint("keys:", A.meta.keys())# Iterate over all key-value pairs:for key, value in A.meta.items():print(f"{key}: {value}")
meta <-tiledb_get_all_metadata(array)# Iterate over all key value pairsfor (key innames(meta)) { value <- meta[[key]]print(paste(key, ": ", value, sep =""))}