Writing and reading a dataset using Lance

In this example, we will write a simple lance dataset to disk. Then we will read it and print out some basic properties like the schema and sizes for each record batch in the dataset. The example uses only one record batch, however it should work for larger datasets (multiple record batches) as well.

Writing the raw dataset

 1// Writes sample dataset to the given path
 2async fn write_dataset(data_path: &str) {
 3    // Define new schema
 4    let schema = Arc::new(Schema::new(vec![
 5        Field::new("key", DataType::UInt32, false),
 6        Field::new("value", DataType::UInt32, false),
 7    ]));
 8
 9    // Create new record batches
10    let batch = RecordBatch::try_new(
11        schema.clone(),
12        vec![
13            Arc::new(UInt32Array::from(vec![1, 2, 3, 4, 5, 6])),
14            Arc::new(UInt32Array::from(vec![6, 7, 8, 9, 10, 11])),
15        ],
16    )
17    .unwrap();
18
19    let batches = RecordBatchIterator::new([Ok(batch)], schema.clone());
20
21    // Define write parameters (e.g. overwrite dataset)
22    let write_params = WriteParams {
23        mode: WriteMode::Overwrite,
24        ..Default::default()
25    };
26
27    Dataset::write(batches, data_path, Some(write_params))
28        .await
29        .unwrap();
30} // End write dataset

First we define a schema for our dataset, and create a record batch from that schema. Next we iterate over the record batches (only one in this case) and write them to disk. We also define the write parameters (set to overwrite) and then write the dataset to disk.

Reading a Lance dataset

Now that we have written the dataset to a new directory, we can read it back and print out some basic properties.

 1// Reads dataset from the given path and prints batch size, schema for all record batches. Also extracts and prints a slice from the first batch
 2async fn read_dataset(data_path: &str) {
 3    let dataset = Dataset::open(data_path).await.unwrap();
 4    let scanner = dataset.scan();
 5
 6    let mut batch_stream = scanner.try_into_stream().await.unwrap().map(|b| b.unwrap());
 7
 8    while let Some(batch) = batch_stream.next().await {
 9        println!("Batch size: {}, {}", batch.num_rows(), batch.num_columns()); // print size of batch
10        println!("Schema: {:?}", batch.schema()); // print schema of recordbatch
11
12        println!("Batch: {:?}", batch); // print the entire recordbatch (schema and data)
13    }
14} // End read dataset

First we open the dataset, and create a scanner object. We use it to create a batch_stream that will let us access each record batch in the dataset. Then we iterate over the record batches and print out the size and schema of each one.