| 1 |
Create Dask Bags |
| 2 |
================ |
| 3 |
|
| 4 |
There are several ways to create Dask bags around your data: |
| 5 |
|
| 6 |
``db.from_sequence`` |
| 7 |
-------------------- |
| 8 |
|
| 9 |
You can create a bag from an existing Python iterable: |
| 10 |
|
| 11 |
.. code-block:: python |
| 12 |
|
| 13 |
>>> import dask.bag as db |
| 14 |
>>> b = db.from_sequence([1, 2, 3, 4, 5, 6]) |
| 15 |
|
| 16 |
You can control the number of partitions into which this data is binned: |
| 17 |
|
| 18 |
.. code-block:: python |
| 19 |
|
| 20 |
>>> b = db.from_sequence([1, 2, 3, 4, 5, 6], npartitions=2) |
| 21 |
|
| 22 |
This controls the granularity of the parallelism that you expose. By default, |
| 23 |
Dask will try to partition your data into about 100 partitions. |
| 24 |
|
| 25 |
IMPORTANT: do not load your data into Python and then load that data into a |
| 26 |
Dask bag. Instead, use Dask Bag to load your data. This |
| 27 |
parallelizes the loading step and reduces inter-worker communication: |
| 28 |
|
| 29 |
.. code-block:: python |
| 30 |
|
| 31 |
>>> b = db.from_sequence(['1.dat', '2.dat', ...]).map(load_from_filename) |
| 32 |
|
| 33 |
|
| 34 |
``db.read_text`` |
| 35 |
---------------- |
| 36 |
|
| 37 |
Dask Bag can load data directly from text files. You can pass either a |
| 38 |
single file name, a list of file names, or a globstring. The resulting |
| 39 |
bag will have one item per line and one file per partition: |
| 40 |
|
| 41 |
.. code-block:: python |
| 42 |
|
| 43 |
>>> b = db.read_text('myfile.txt') |
| 44 |
>>> b = db.read_text(['myfile.1.txt', 'myfile.2.txt', ...]) |
| 45 |
>>> b = db.read_text('myfile.*.txt') |
| 46 |
|
| 47 |
This handles standard compression libraries like ``gzip``, ``bz2``, ``xz``, or |
| 48 |
any easily installed compression library that has a file-like object. |
| 49 |
Compression will be inferred by the file name extension, or by using the |
| 50 |
``compression='gzip'`` keyword: |
| 51 |
|
| 52 |
.. code-block:: python |
| 53 |
|
| 54 |
>>> b = db.read_text('myfile.*.txt.gz') |
| 55 |
|
| 56 |
The resulting items in the bag are strings. If you have encoded data like |
| 57 |
line-delimited JSON, then you may want to map a decoding or load function across |
| 58 |
the bag: |
| 59 |
|
| 60 |
.. code-block:: python |
| 61 |
|
| 62 |
>>> import json |
| 63 |
>>> b = db.read_text('myfile.*.json').map(json.loads) |
| 64 |
|
| 65 |
Or do string munging tasks. For convenience, there is a string namespace |
| 66 |
attached directly to bags with ``.str.methodname``: |
| 67 |
|
| 68 |
.. code-block:: python |
| 69 |
|
| 70 |
>>> b = db.read_text('myfile.*.csv').str.strip().str.split(',') |
| 71 |
|
| 72 |
|
| 73 |
``db.read_avro`` |
| 74 |
---------------- |
| 75 |
|
| 76 |
Dask Bag can read binary files in the `Avro`_ format if `fastavro`_ is installed. |
| 77 |
A bag can be made from one or more files, with optional chunking within files. |
| 78 |
The resulting bag will have one item per Avro record, which will be a dictionary |
| 79 |
of the form given by the Avro schema. There will be at least one partition per |
| 80 |
input file: |
| 81 |
|
| 82 |
.. code-block:: python |
| 83 |
|
| 84 |
>>> b = db.read_avro('datafile.avro') |
| 85 |
>>> b = db.read_avro('data.*.avro') |
| 86 |
|
| 87 |
.. _Avro: https://avro.apache.org/docs/1.8.2/ |
| 88 |
.. _fastavro: https://fastavro.readthedocs.io |
| 89 |
|
| 90 |
By default, Dask will split data files into chunks of approximately ``blocksize`` |
| 91 |
bytes in size. The actual blocks you would get depend on the internal blocking |
| 92 |
of the file. |
| 93 |
|
| 94 |
For files that are compressed after creation (this is not the same as the internal |
| 95 |
"codec" used by Avro), no chunking should be used, and there will be exactly one |
| 96 |
partition per file: |
| 97 |
|
| 98 |
.. code-block:: python |
| 99 |
|
| 100 |
> b = bd.read_avro('compressed.*.avro.gz', blocksize=None, compression='gzip') |
| 101 |
|
| 102 |
|
| 103 |
``db.from_delayed`` |
| 104 |
------------------- |
| 105 |
|
| 106 |
You can construct a Dask bag from :doc:`dask.delayed <delayed>` values using the |
| 107 |
``db.from_delayed`` function. For more information, see |
| 108 |
:doc:`documentation on using dask.delayed with collections <delayed-collections>`. |
| 109 |
|
| 110 |
|
| 111 |
Store Dask Bags |
| 112 |
=============== |
| 113 |
|
| 114 |
In Memory |
| 115 |
--------- |
| 116 |
|
| 117 |
You can convert a Dask bag to a list or Python iterable by calling ``compute()`` |
| 118 |
or by converting the object into a list: |
| 119 |
|
| 120 |
.. code-block:: python |
| 121 |
|
| 122 |
>>> result = b.compute() |
| 123 |
or |
| 124 |
>>> result = list(b) |
| 125 |
|
| 126 |
To Text Files |
| 127 |
------------- |
| 128 |
|
| 129 |
You can convert a Dask bag into a sequence of files on disk by calling the |
| 130 |
``.to_textfiles()`` method: |
| 131 |
|
| 132 |
.. autofunction:: dask.bag.core.to_textfiles |
| 133 |
|
| 134 |
To Avro |
| 135 |
------- |
| 136 |
|
| 137 |
Dask bags can be written directly to Avro binary format using `fastavro`_. One file |
| 138 |
will be written per bag partition. This requires the user to provide a fully-specified |
| 139 |
schema dictionary (see the docstring of the ``.to_avro()`` method). |
| 140 |
|
| 141 |
.. autofunction:: dask.bag.avro.to_avro |
| 142 |
|
| 143 |
To DataFrames |
| 144 |
------------- |
| 145 |
|
| 146 |
You can convert a Dask bag into a :doc:`Dask DataFrame<dataframe>` and use |
| 147 |
those storage solutions. |
| 148 |
|
| 149 |
.. automethod:: dask.bag.core.Bag.to_dataframe |
| 150 |
|
| 151 |
|
| 152 |
To Delayed Values |
| 153 |
----------------- |
| 154 |
|
| 155 |
You can convert a Dask bag into a list of :doc:`Dask delayed values<delayed>` |
| 156 |
and custom storage solutions from there. |
| 157 |
|
| 158 |
.. automethod:: dask.bag.core.Bag.to_delayed |