Debian Codegraph @

docs/source/bag.rst

raw ◀ ▶
1 Debian patch touch this file
1 Bag
2 ===
3
4 .. toctree::
5 :maxdepth: 1
6 :hidden:
7
8 bag-creation.rst
9 bag-api.rst
10
11 Dask Bag implements operations like ``map``, ``filter``, ``fold``, and
12 ``groupby`` on collections of generic Python objects. It does this in parallel with a
13 small memory footprint using Python iterators. It is similar to a parallel
14 version of PyToolz_ or a Pythonic version of the `PySpark RDD`_.
15
16 .. _PyToolz: https://toolz.readthedocs.io/en/latest/
17 .. _`PySpark RDD`: https://spark.apache.org/docs/latest/api/python
18
19 Examples
20 --------
21
22 Visit https://examples.dask.org/bag.html to see and run examples using Dask Bag.
23
24 Design
25 ------
26
27 Dask bags coordinate many Python lists or Iterators, each of which forms a
28 partition of a larger collection.
29
30 .. raw:: html
31
32 <iframe width="560"
33 height="315"
•34 src="https://www.youtube-nocookie.com/embed/-qIiJ1XtSv0"
35 style="margin: 0 auto 20px auto; display: block;"
36 frameborder="0"
37 allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
38 allowfullscreen></iframe>
39
40 Common Uses
41 -----------
42
43 Dask bags are often used to parallelize simple computations on unstructured or
44 semi-structured data like text data, log files, JSON records, or user defined
45 Python objects.
46
47 Execution
48 ---------
49
50 Execution on bags provide two benefits:
51
52 1. Parallel: data is split up, allowing multiple cores or machines to execute
53 in parallel
54 2. Iterating: data processes lazily, allowing smooth execution of
55 larger-than-memory data, even on a single machine within a single partition
56
57
58 Default scheduler
59 ~~~~~~~~~~~~~~~~~
60
61 By default, ``dask.bag`` uses ``dask.multiprocessing`` for computation. As a
62 benefit, Dask bypasses the GIL_ and uses multiple cores on pure Python objects.
63 As a drawback, Dask Bag doesn't perform well on computations that include a
64 great deal of inter-worker communication. For common operations this is rarely
65 an issue as most Dask Bag workflows are embarrassingly parallel or result in
66 reductions with little data moving between workers.
67
68 .. _GIL: https://docs.python.org/3/glossary.html#term-gil
69
70
71 Shuffle
72 ~~~~~~~
73
74 Some operations, like ``groupby``, require substantial inter-worker
75 communication. On a single machine, Dask uses partd_ to perform efficient,
76 parallel, spill-to-disk shuffles. When working in a cluster, Dask uses a task
77 based shuffle.
78
79 These shuffle operations are expensive and better handled by projects like
80 ``dask.dataframe``. It is best to use ``dask.bag`` to clean and process data,
81 then transform it into an array or DataFrame before embarking on the more
82 complex operations that require shuffle steps.
83
84 .. _partd: https://github.com/mrocklin/partd
85
86
87 Known Limitations
88 -----------------
89
90 Bags provide very general computation (any Python function). This generality
91 comes at cost. Bags have the following known limitations:
92
93 1. By default, they rely on the multiprocessing scheduler, which has its own
94 set of known limitations (see :doc:`shared`)
95 2. Bags are immutable and so you can not change individual elements
96 3. Bag operations tend to be slower than array/DataFrame computations in the
97 same way that standard Python containers tend to be slower than NumPy
98 arrays and Pandas DataFrames
99 4. Bag's ``groupby`` is slow. You should try to use Bag's ``foldby`` if possible.
100 Using ``foldby`` requires more thought though
101
102
103 Name
104 ----
105
106 *Bag* is the mathematical name for an unordered collection allowing repeats. It
107 is a friendly synonym to multiset_. A bag, or a multiset, is a generalization of
108 the concept of a set that, unlike a set, allows multiple instances of the
109 multiset's elements:
110
111 * ``list``: *ordered* collection *with repeats*, ``[1, 2, 3, 2]``
112 * ``set``: *unordered* collection *without repeats*, ``{1, 2, 3}``
113 * ``bag``: *unordered* collection *with repeats*, ``{1, 2, 2, 3}``
114
115 So, a bag is like a list, but it doesn't guarantee an ordering among elements.
116 There can be repeated elements but you can't ask for the ith element.
117
118 .. _multiset: https://en.wikipedia.org/wiki/Bag_(mathematics)