-
Notifications
You must be signed in to change notification settings - Fork 709
Expand file tree
/
Copy path_dataset.py
More file actions
393 lines (339 loc) · 15.4 KB
/
_dataset.py
File metadata and controls
393 lines (339 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
from __future__ import annotations
import asyncio
import logging
from datetime import timedelta
from io import StringIO
from typing import TYPE_CHECKING, overload
from typing_extensions import override
from crawlee import service_locator
from crawlee._utils.docs import docs_group
from crawlee._utils.file import export_csv_to_stream, export_json_to_stream
from crawlee.errors import StorageWriteError
from ._base import Storage
from ._key_value_store import KeyValueStore
from ._utils import validate_storage_name
if TYPE_CHECKING:
from collections.abc import AsyncIterator
from typing import Any, Literal
from typing_extensions import Unpack
from crawlee._types import ExportDataCsvKwargs, ExportDataJsonKwargs
from crawlee.configuration import Configuration
from crawlee.storage_clients import StorageClient
from crawlee.storage_clients._base import DatasetClient
from crawlee.storage_clients.models import DatasetItemsListPage, DatasetMetadata
logger = logging.getLogger(__name__)
@docs_group('Storages')
class Dataset(Storage):
"""Dataset is a storage for managing structured tabular data.
The dataset class provides a high-level interface for storing and retrieving structured data
with consistent schema, similar to database tables or spreadsheets. It abstracts the underlying
storage implementation details, offering a consistent API regardless of where the data is
physically stored.
Dataset operates in an append-only mode, allowing new records to be added but not modified
or deleted after creation. This makes it particularly suitable for storing crawling results
and other data that should be immutable once collected.
The class provides methods for adding data, retrieving data with various filtering options,
and exporting data to different formats. You can create a dataset using the `open` class method,
specifying either a name or ID. The underlying storage implementation is determined by
the configured storage client.
### Usage
```python
from crawlee.storages import Dataset
# Open a dataset
dataset = await Dataset.open(name='my-dataset')
# Add data
await dataset.push_data({'title': 'Example Product', 'price': 99.99})
# Retrieve filtered data
results = await dataset.get_data(limit=10, desc=True)
# Export data
await dataset.export_to('results.json', content_type='json')
```
"""
def __init__(self, client: DatasetClient, id: str, name: str | None) -> None:
"""Initialize a new instance.
Preferably use the `Dataset.open` constructor to create a new instance.
Args:
client: An instance of a storage client.
id: The unique identifier of the storage.
name: The name of the storage, if available.
"""
validate_storage_name(name)
self._client = client
self._id = id
self._name = name
@property
@override
def id(self) -> str:
return self._id
@property
@override
def name(self) -> str | None:
return self._name
@override
async def get_metadata(self) -> DatasetMetadata:
return await self._client.get_metadata()
@override
@classmethod
async def open(
cls,
*,
id: str | None = None,
name: str | None = None,
alias: str | None = None,
configuration: Configuration | None = None,
storage_client: StorageClient | None = None,
) -> Dataset:
configuration = service_locator.get_configuration() if configuration is None else configuration
storage_client = service_locator.get_storage_client() if storage_client is None else storage_client
client_opener_coro = storage_client.create_dataset_client(
id=id, name=name, alias=alias, configuration=configuration
)
storage_client_cache_key = storage_client.get_storage_client_cache_key(configuration=configuration)
return await service_locator.storage_instance_manager.open_storage_instance(
cls,
id=id,
name=name,
alias=alias,
client_opener_coro=client_opener_coro,
storage_client_cache_key=storage_client_cache_key,
)
@override
async def drop(self) -> None:
storage_instance_manager = service_locator.storage_instance_manager
storage_instance_manager.remove_from_cache(self)
await self._client.drop()
@override
async def purge(self) -> None:
await self._client.purge()
async def push_data(
self,
data: list[dict[str, Any]] | dict[str, Any],
*,
max_attempts: int = 5,
wait_time_between_retries: timedelta = timedelta(seconds=1),
) -> None:
"""Store an object or an array of objects to the dataset.
The size of the data is limited by the receiving API and therefore `push_data()` will only
allow objects whose JSON representation is smaller than 9MB. When an array is passed,
none of the included objects may be larger than 9MB, but the array itself may be of any size.
Args:
data: A JSON serializable data structure to be stored in the dataset. The JSON representation
of each item must be smaller than 9MB.
max_attempts: The maximum number of attempts to push data in case of failure.
wait_time_between_retries: The time to wait between retries in case of failure.
"""
if max_attempts < 1:
raise ValueError('max_attempts must be at least 1')
wait_time_between_retries_seconds = wait_time_between_retries.total_seconds()
last_exception: StorageWriteError | None = None
for attempt in range(max_attempts):
try:
await self._client.push_data(data=data)
break
except StorageWriteError as e:
last_exception = e
if attempt < max_attempts - 1:
await asyncio.sleep(wait_time_between_retries_seconds)
else:
if last_exception:
logger.warning(f'Failed to push data after {max_attempts} attempts with error: {last_exception.cause}')
async def get_data(
self,
*,
offset: int = 0,
limit: int | None = 999_999_999_999,
clean: bool = False,
desc: bool = False,
fields: list[str] | None = None,
omit: list[str] | None = None,
unwind: list[str] | None = None,
skip_empty: bool = False,
skip_hidden: bool = False,
flatten: list[str] | None = None,
view: str | None = None,
) -> DatasetItemsListPage:
"""Retrieve a paginated list of items from a dataset based on various filtering parameters.
This method provides the flexibility to filter, sort, and modify the appearance of dataset items
when listed. Each parameter modifies the result set according to its purpose. The method also
supports pagination through 'offset' and 'limit' parameters.
Args:
offset: Skips the specified number of items at the start.
limit: The maximum number of items to retrieve. Unlimited if None.
clean: Return only non-empty items and excludes hidden fields. Shortcut for skip_hidden and skip_empty.
desc: Set to True to sort results in descending order.
fields: Fields to include in each item. Sorts fields as specified if provided.
omit: Fields to exclude from each item.
unwind: Unwinds items by a specified array field, turning each element into a separate item.
skip_empty: Excludes empty items from the results if True.
skip_hidden: Excludes fields starting with '#' if True.
flatten: Fields to be flattened in returned items.
view: Specifies the dataset view to be used.
Returns:
An object with filtered, sorted, and paginated dataset items plus pagination details.
"""
return await self._client.get_data(
offset=offset,
limit=limit,
clean=clean,
desc=desc,
fields=fields,
omit=omit,
unwind=unwind,
skip_empty=skip_empty,
skip_hidden=skip_hidden,
flatten=flatten,
view=view,
)
async def iterate_items(
self,
*,
offset: int = 0,
limit: int | None = 999_999_999_999,
clean: bool = False,
desc: bool = False,
fields: list[str] | None = None,
omit: list[str] | None = None,
unwind: list[str] | None = None,
skip_empty: bool = False,
skip_hidden: bool = False,
) -> AsyncIterator[dict[str, Any]]:
"""Iterate over items in the dataset according to specified filters and sorting.
This method allows for asynchronously iterating through dataset items while applying various filters such as
skipping empty items, hiding specific fields, and sorting. It supports pagination via `offset` and `limit`
parameters, and can modify the appearance of dataset items using `fields`, `omit`, `unwind`, `skip_empty`, and
`skip_hidden` parameters.
Args:
offset: Skips the specified number of items at the start.
limit: The maximum number of items to retrieve. Unlimited if None.
clean: Return only non-empty items and excludes hidden fields. Shortcut for skip_hidden and skip_empty.
desc: Set to True to sort results in descending order.
fields: Fields to include in each item. Sorts fields as specified if provided.
omit: Fields to exclude from each item.
unwind: Unwinds items by a specified array field, turning each element into a separate item.
skip_empty: Excludes empty items from the results if True.
skip_hidden: Excludes fields starting with '#' if True.
Yields:
An asynchronous iterator of dictionary objects, each representing a dataset item after applying
the specified filters and transformations.
"""
async for item in self._client.iterate_items(
offset=offset,
limit=limit,
clean=clean,
desc=desc,
fields=fields,
omit=omit,
unwind=unwind,
skip_empty=skip_empty,
skip_hidden=skip_hidden,
):
yield item
async def list_items(
self,
*,
offset: int = 0,
limit: int | None = 999_999_999_999,
clean: bool = False,
desc: bool = False,
fields: list[str] | None = None,
omit: list[str] | None = None,
unwind: list[str] | None = None,
skip_empty: bool = False,
skip_hidden: bool = False,
) -> list[dict[str, Any]]:
"""Retrieve a list of all items from the dataset according to specified filters and sorting.
This method collects all dataset items into a list while applying various filters such as
skipping empty items, hiding specific fields, and sorting. It supports pagination via `offset` and `limit`
parameters, and can modify the appearance of dataset items using `fields`, `omit`, `unwind`, `skip_empty`, and
`skip_hidden` parameters.
Args:
offset: Skips the specified number of items at the start.
limit: The maximum number of items to retrieve. Unlimited if None.
clean: Return only non-empty items and excludes hidden fields. Shortcut for skip_hidden and skip_empty.
desc: Set to True to sort results in descending order.
fields: Fields to include in each item. Sorts fields as specified if provided.
omit: Fields to exclude from each item.
unwind: Unwinds items by a specified array field, turning each element into a separate item.
skip_empty: Excludes empty items from the results if True.
skip_hidden: Excludes fields starting with '#' if True.
Returns:
A list of dictionary objects, each representing a dataset item after applying
the specified filters and transformations.
"""
return [
item
async for item in self.iterate_items(
offset=offset,
limit=limit,
clean=clean,
desc=desc,
fields=fields,
omit=omit,
unwind=unwind,
skip_empty=skip_empty,
skip_hidden=skip_hidden,
)
]
@overload
async def export_to(
self,
key: str,
content_type: Literal['json'],
to_kvs_id: str | None = None,
to_kvs_name: str | None = None,
to_kvs_storage_client: StorageClient | None = None,
to_kvs_configuration: Configuration | None = None,
**kwargs: Unpack[ExportDataJsonKwargs],
) -> None: ...
@overload
async def export_to(
self,
key: str,
content_type: Literal['csv'],
to_kvs_id: str | None = None,
to_kvs_name: str | None = None,
to_kvs_storage_client: StorageClient | None = None,
to_kvs_configuration: Configuration | None = None,
**kwargs: Unpack[ExportDataCsvKwargs],
) -> None: ...
async def export_to(
self,
key: str,
content_type: Literal['json', 'csv'] = 'json',
to_kvs_id: str | None = None,
to_kvs_name: str | None = None,
to_kvs_storage_client: StorageClient | None = None,
to_kvs_configuration: Configuration | None = None,
**kwargs: Any,
) -> None:
"""Export the entire dataset into a specified file stored under a key in a key-value store.
This method consolidates all entries from a specified dataset into one file, which is then saved under a
given key in a key-value store. The format of the exported file is determined by the `content_type` parameter.
Either the dataset's ID or name should be specified, and similarly, either the target key-value store's ID or
name should be used.
Args:
key: The key under which to save the data in the key-value store.
content_type: The format in which to export the data.
to_kvs_id: ID of the key-value store to save the exported file.
Specify only one of ID or name.
to_kvs_name: Name of the key-value store to save the exported file.
Specify only one of ID or name.
to_kvs_storage_client: Storage client to use for the key-value store.
to_kvs_configuration: Configuration for the key-value store.
kwargs: Additional parameters for the export operation, specific to the chosen content type.
"""
kvs = await KeyValueStore.open(
id=to_kvs_id,
name=to_kvs_name,
configuration=to_kvs_configuration,
storage_client=to_kvs_storage_client,
)
dst = StringIO()
if content_type == 'csv':
await export_csv_to_stream(self.iterate_items(), dst, **kwargs)
await kvs.set_value(key, dst.getvalue(), 'text/csv')
elif content_type == 'json':
await export_json_to_stream(self.iterate_items(), dst, **kwargs)
await kvs.set_value(key, dst.getvalue(), 'application/json')
else:
raise ValueError('Unsupported content type, expecting CSV or JSON')