Skip to content

Commit a12435e

Browse files
committed
sharding
1 parent 9a4e97c commit a12435e

File tree

1 file changed

+137
-15
lines changed

1 file changed

+137
-15
lines changed

intermediate/intro-to-zarr.ipynb

Lines changed: 137 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,147 @@
516516
"id": "a63ebdd7",
517517
"metadata": {},
518518
"source": [
519-
"### How to Examine and Modify the Chunk Shape\n",
519+
"#### Chunking\n",
520+
"Chunking is the process of dividing the data arrays into smaller pieces. This allows for parallel processing and efficient storage.\n",
520521
"\n",
521-
"If your data is sufficiently large, Zarr will chose a chunksize for you.\n",
522+
"One of the important parameters in Zarr is the chunk shape, which determines how the data is divided into smaller, manageable pieces. This is crucial for performance, especially when working with large datasets.\n",
522523
"\n",
524+
"To examine the chunk shape of a Zarr array, you can use the `chunks` attribute. This will show you the size of each chunk in each dimension."
525+
]
526+
},
527+
{
528+
"cell_type": "code",
529+
"execution_count": 44,
530+
"id": "cd5e7ec0",
531+
"metadata": {},
532+
"outputs": [
533+
{
534+
"data": {
535+
"text/plain": [
536+
"(10, 10)"
537+
]
538+
},
539+
"execution_count": 44,
540+
"metadata": {},
541+
"output_type": "execute_result"
542+
}
543+
],
544+
"source": [
545+
"z.chunks"
546+
]
547+
},
548+
{
549+
"cell_type": "markdown",
550+
"id": "a1f62ab3",
551+
"metadata": {},
552+
"source": [
553+
"When selecting chunk shapes, we need to keep in mind two constraints:\n",
554+
"\n",
555+
"- Concurrent writes are possible as long as different processes write to separate chunks, enabling highly parallel data writing. \n",
556+
"- When reading data, if any piece of the chunk is needed, the entire chunk has to be loaded. \n",
557+
"\n",
558+
"The optimal chunk shape will depend on how you want to access the data. E.g., for a 2-dimensional array, if you only ever take slices along the first dimension, then chunk across the second dimension.\n",
559+
"\n",
560+
"Here we will compare two different chunking strategies.\n"
561+
]
562+
},
563+
{
564+
"cell_type": "code",
565+
"execution_count": 46,
566+
"id": "b7929741",
567+
"metadata": {},
568+
"outputs": [],
569+
"source": [
570+
"c = zarr.create(shape=(200, 200, 200), chunks=(1, 200, 200), dtype='f8', store='c.zarr')\n",
571+
"c[:] = np.random.randn(*c.shape)"
572+
]
573+
},
574+
{
575+
"cell_type": "code",
576+
"execution_count": 47,
577+
"id": "68d6d671",
578+
"metadata": {},
579+
"outputs": [
580+
{
581+
"name": "stdout",
582+
"output_type": "stream",
583+
"text": [
584+
"CPU times: user 112 ms, sys: 55.4 ms, total: 167 ms\n",
585+
"Wall time: 67.5 ms\n"
586+
]
587+
}
588+
],
589+
"source": [
590+
"%time _ = c[:, 0, 0]\n"
591+
]
592+
},
593+
{
594+
"cell_type": "code",
595+
"execution_count": 48,
596+
"id": "9ad7e371",
597+
"metadata": {},
598+
"outputs": [],
599+
"source": [
600+
"d = zarr.create(shape=(200, 200, 200), chunks=(200, 200, 1), dtype='f8', store='d.zarr')\n",
601+
"d[:] = np.random.randn(*d.shape)"
602+
]
603+
},
604+
{
605+
"cell_type": "code",
606+
"execution_count": 49,
607+
"id": "51094774",
608+
"metadata": {},
609+
"outputs": [
610+
{
611+
"name": "stdout",
612+
"output_type": "stream",
613+
"text": [
614+
"CPU times: user 1.63 ms, sys: 1.3 ms, total: 2.93 ms\n",
615+
"Wall time: 2.14 ms\n"
616+
]
617+
}
618+
],
619+
"source": [
620+
"%time _ = d[:, 0, 0]\n"
621+
]
622+
},
623+
{
624+
"cell_type": "markdown",
625+
"id": "3fa2b41a",
626+
"metadata": {},
627+
"source": [
628+
"### Sharding\n",
629+
"When working with large arrays and small chunks, Zarr’s sharding feature can improve storage efficiency and performance. Instead of writing each chunk to a separate file—which can overwhelm file systems and cloud object stores—sharding groups multiple chunks into a single storage object.\n",
630+
"\n",
631+
"Why Use Sharding?\n",
632+
"\n",
633+
"- File systems struggle with too many small files.\n",
634+
"- Small files (e.g., 1 MB or less) may waste space due to filesystem block size.\n",
635+
"- Object storage systems (e.g., S3) can slow down with a high number of objects.\n",
636+
"With sharding, you choose:\n",
523637
"\n"
524638
]
525639
},
640+
{
641+
"cell_type": "code",
642+
"execution_count": null,
643+
"id": "4fec37ba",
644+
"metadata": {},
645+
"outputs": [],
646+
"source": [
647+
"import zarr\n",
648+
"\n",
649+
"z6 = zarr.create_array(\n",
650+
" store={},\n",
651+
" shape=(10000, 10000, 1000),\n",
652+
" chunks=(100, 100, 100),\n",
653+
" shards=(1000, 1000, 1000),\n",
654+
" dtype='uint8'\n",
655+
")\n",
656+
"\n",
657+
"z6.info"
658+
]
659+
},
526660
{
527661
"cell_type": "markdown",
528662
"id": "1e0d1a8e",
@@ -636,12 +770,6 @@
636770
"So far we have only been dealing in single array Zarr data stores. In this next example, we will create a zarr store with multiple arrays and then consolidate metadata. The speed up is significant when dealing in remote storage options, which we will see in the following example on accessing cloud storage."
637771
]
638772
},
639-
{
640-
"cell_type": "markdown",
641-
"id": "cdb3f822",
642-
"metadata": {},
643-
"source": []
644-
},
645773
{
646774
"cell_type": "code",
647775
"execution_count": null,
@@ -748,7 +876,7 @@
748876
"id": "c6454acf",
749877
"metadata": {},
750878
"source": [
751-
"## Object Storage as a Zarr Store\n",
879+
"### Object Storage as a Zarr Store\n",
752880
"\n",
753881
"Zarr’s layout (many files/chunks per array) maps perfectly onto object storage, such as Amazon S3, Google Cloud Storage, or Azure Blob Storage. Each chunk is stored as a separate object, enabling distributed reads/writes.\n",
754882
"\n"
@@ -2294,12 +2422,6 @@
22942422
"- [Cloud Optimized Geospatial Formats](https://guide.cloudnativegeo.org/zarr/zarr-in-practice.html)\n",
22952423
"- [Scalable and Computationally Reproducible Approaches to Arctic Research](https://learning.nceas.ucsb.edu/2025-04-arctic/sections/zarr.html)\n"
22962424
]
2297-
},
2298-
{
2299-
"cell_type": "markdown",
2300-
"id": "5ed9088a",
2301-
"metadata": {},
2302-
"source": []
23032425
}
23042426
],
23052427
"metadata": {

0 commit comments

Comments
 (0)