| 1 |
Detailed description |
| 2 |
==================== |
| 3 |
|
| 4 |
We will demonstrate how this library can lead to parallel, cloud-friendly access of data with |
| 5 |
specific reference to netCDF4/HDF5 files, which were the motivating first case we attempted. |
| 6 |
HDF5 is one of the data formats that already readily supports direct reading from cloud storage, |
| 7 |
whether over HTTP or by passing ``fsspec`` instances. Nevertheless, the ability to extract |
| 8 |
metadata in a single scan, and to arrange multiple chunks from multiple files and |
| 9 |
read them in parallel and/or concurrently within a single indexible aggregate dataset - |
| 10 |
*that* is the power of this project. |
| 11 |
|
| 12 |
.. note:: |
| 13 |
|
| 14 |
"Parallel" means actions taking place in multiple threads, processes or machines, each |
| 15 |
completely independent of the others. After overheard for arranging such a system are |
| 16 |
taken into account, a speedup equal to the number of CPU cores leveraged is possible, |
| 17 |
for CPU-intensive workloads. |
| 18 |
|
| 19 |
"Concurrency", by contrast, refers to mutiple operations that are *waiting* on an external |
| 20 |
system. For the particular case of cloud storage, the latency to get the first byte of a read |
| 21 |
can be comparable or dominate the total time for a request. So if you can launch many |
| 22 |
requests, and only pay the overhead cost once (they all wait together), then a large speedup |
| 23 |
is possible. |
| 24 |
|
| 25 |
``kerchunk`` with ``dask`` and ``zarr`` allows us to make use of both these techniques |
| 26 |
simultaneously. |
| 27 |
|
| 28 |
Binary buffers |
| 29 |
-------------- |
| 30 |
|
| 31 |
Numerical arrays in python (and all other modern languages) are stored in binary buffers. |
| 32 |
For example, if the data type is int32, then 4 bytes are used per value, and an array of |
| 33 |
total N elements is stored in a single Nx4 block of bytes. This remains true for multiple |
| 34 |
dimensions. |
| 35 |
|
| 36 |
If you want to store such data to disk, you cannot be more efficient than to take the buffer |
| 37 |
representation, perhaps pass it through some codecs (e.g., compression) and write those bytes |
| 38 |
directly. HDF5 does this, and so do almost all other binary formats. An HDF5 is made up of |
| 39 |
binary chunks and metadata areas, describing the types and sizes of the binary chunks, how |
| 40 |
they relate to one-another and any other metadata/attributes that might be associated. |
| 41 |
|
| 42 |
.. image:: images/binary_buffer.png |
| 43 |
:width: 400 |
| 44 |
:alt: HDF5 files contain C buffers |
| 45 |
|
| 46 |
``fsspec`` virtual filesystem |
| 47 |
----------------------------- |
| 48 |
|
| 49 |
``fsspec`` was designed allow access to any storage backend and present it using the same filesystem-like |
| 50 |
interface, so that other libraries could ingest data from anywhere. The idea was originally |
| 51 |
motivated by the need for ``dask`` to be able to read data from cloud storage in parallel on a |
| 52 |
distributed cluster. |
| 53 |
|
| 54 |
Given this framework, we have created ``ReferenceFileSystem``, which allows arbitrary mapping of |
| 55 |
a pathname to: |
| 56 |
|
| 57 |
- a static binary value |
| 58 |
- a whole file at a different URL |
| 59 |
- part of a file at a different URL |
| 60 |
|
| 61 |
Thus, for each of the binary chunks in one (or more) HDF file, we can assign is a position within a |
| 62 |
directory tree as we see fit. |
| 63 |
|
| 64 |
``zarr`` reader |
| 65 |
--------------- |
| 66 |
|
| 67 |
The zarr array storage library reads data by first reading two small special JSON files, ``.zarray`` and |
| 68 |
``.zattrs`` in a given directory, which give information about the array shape, chunking, encoding and |
| 69 |
attributes. The chunks of data themselves are expected to live at paths like ``"var/x.y"``, where |
| 70 |
``var`` is the name of the variable and ``x, y`` are chunk indexes in two dimensions (there can be more!). |
| 71 |
|
| 72 |
In the example image, we demonstrate how zarr can access data across the chunks of many HDF5 files, if |
| 73 |
we construct a virtual filesystem in which each binary chunk is given the exact correct filename as |
| 74 |
expected by ``zarr``, in this case for the variable "time". |
| 75 |
|
| 76 |
|
| 77 |
.. image:: images/multi_refs.png |
| 78 |
:width: 400 |
| 79 |
:alt: Zarr can view many files as a single dataset |
| 80 |
|
| 81 |
.. raw:: html |
| 82 |
|
| 83 |
<script data-goatcounter="https://kerchunk.goatcounter.com/count" |
| 84 |
async src="//gc.zgo.at/count.js"></script> |