Julia Moska
commited on
Commit
•
0ae3740
1
Parent(s):
179e7ee
added tryout script
Browse files- test_parquet.py +56 -0
test_parquet.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import datasets
|
3 |
+
import pyarrow as pa
|
4 |
+
import pyarrow.parquet as pq
|
5 |
+
logger = datasets.utils.logging.get_logger(__name__)
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
_URLS = { "train": "https://huggingface.co/datasets/moska/test_parquet/resolve/main/data/example.parquet" }
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
class ParquetDataset(datasets.ArrowBasedBuilder):
|
14 |
+
BUILDER_CONFIGS = [
|
15 |
+
datasets.BuilderConfig(
|
16 |
+
version=datasets.Version(version="0.0.1"),
|
17 |
+
description=f"test_parquet dataset.",
|
18 |
+
)
|
19 |
+
|
20 |
+
]
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
def _info(self):
|
25 |
+
return datasets.DatasetInfo(
|
26 |
+
# This is the description that will appear on the datasets page.
|
27 |
+
description="reading parquet format.",
|
28 |
+
# This defines the different columns of the dataset and their types
|
29 |
+
features=datasets.Features(
|
30 |
+
{ "pop_est": datasets.Value(dtype="float64"),
|
31 |
+
"continent": datasets.Value(dtype="string"),
|
32 |
+
"name": datasets.Value(dtype="string"),
|
33 |
+
"iso_a3": datasets.Value(dtype="string"),
|
34 |
+
"gdp_md_est": datasets.Value(dtype="int64"),
|
35 |
+
"geometry": datasets.Value("binary"),
|
36 |
+
# These are the features of your dataset like images, labels ...
|
37 |
+
}
|
38 |
+
),
|
39 |
+
)
|
40 |
+
|
41 |
+
|
42 |
+
def _split_generators(self, dl_manager: datasets.download.DownloadManager):
|
43 |
+
files = _URLS[self.config.name]
|
44 |
+
downloaded_files = dl_manager.download(files)
|
45 |
+
return [
|
46 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'filepath': downloaded_files['train']})
|
47 |
+
]
|
48 |
+
|
49 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
50 |
+
def _generate_tables(self, filepaths: list[str]):
|
51 |
+
for file_idx, filepath in enumerate(filepaths):
|
52 |
+
with open(filepath, mode="rb") as f:
|
53 |
+
parquet_file = pq.ParquetFile(source=filepath)
|
54 |
+
for batch_idx, record_batch in enumerate(parquet_file.iter_batches()):
|
55 |
+
pa_table = pa.Table.from_batches([record_batch])
|
56 |
+
yield f"{file_idx}_{batch_idx}", pa_table
|