-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from rifqi2320/main
Add parquet support
- Loading branch information
Showing
7 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import re | ||
import logging | ||
import pyarrow.parquet as pq | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def generator_wrapper(table, _={}) -> dict: | ||
# change column name | ||
def format_name(name=""): | ||
formatted_key = re.sub(r"[^\w\s]", "", name) | ||
# replace whitespace with underscores | ||
formatted_key = re.sub(r"\s+", "_", formatted_key) | ||
return formatted_key.lower() | ||
|
||
table = table.rename_columns([format_name(name) for name in table.column_names]) | ||
|
||
for row in table.to_pylist(): | ||
yield row | ||
|
||
|
||
def get_row_iterator(table_spec, file_handle): | ||
try: | ||
parquet_file = pq.ParquetFile(file_handle) | ||
except Exception as e: | ||
LOGGER.error("Unable to read the Parquet file: %s", e) | ||
raise e | ||
|
||
# Use batch to read the Parquet file | ||
for batch in parquet_file.iter_batches(): | ||
yield from generator_wrapper(batch, table_spec) |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import logging | ||
import unittest | ||
|
||
from tap_spreadsheets_anywhere import format_handler | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
TEST_TABLE_SPEC = { | ||
"tables": [ | ||
{ | ||
"path": "file://./tap_spreadsheets_anywhere/test", | ||
"name": "parquet-iris", | ||
"pattern": "iris\\-sample\\.parquet", | ||
"start_date": "2017-05-01T00:00:00Z", | ||
"key_properties": [], | ||
"format": "parquet", | ||
}, | ||
{ | ||
"path": "file://./tap_spreadsheets_anywhere/test", | ||
"name": "parquet-mt", | ||
"pattern": "mt\\-sample\\.parquet", | ||
"start_date": "2017-05-01T00:00:00Z", | ||
"key_properties": [], | ||
"format": "parquet", | ||
}, | ||
{ | ||
"path": "file://./tap_spreadsheets_anywhere/test", | ||
"name": "parquet-iris-detect", | ||
"pattern": "iris\\.parquet", | ||
"start_date": "2017-05-01T00:00:00Z", | ||
"key_properties": [], | ||
"format": "detect", | ||
}, | ||
{ | ||
"path": "file://./tap_spreadsheets_anywhere/test", | ||
"name": "parquet-mt-detect", | ||
"pattern": "mt\\.parquet", | ||
"start_date": "2017-05-01T00:00:00Z", | ||
"key_properties": [], | ||
"format": "detect", | ||
}, | ||
] | ||
} | ||
|
||
|
||
class TestParquet(unittest.TestCase): | ||
def test_iris(self): | ||
table_spec = TEST_TABLE_SPEC["tables"][0] | ||
uri = "./tap_spreadsheets_anywhere/test/iris-sample.parquet" | ||
iterator = format_handler.get_row_iterator(table_spec, uri) | ||
|
||
rows = list(iterator) | ||
self.assertEqual(len(rows), 150) | ||
self.assertEqual( | ||
rows[0], | ||
{ | ||
"sepallength": 5.1, | ||
"sepalwidth": 3.5, | ||
"petallength": 1.4, | ||
"petalwidth": 0.2, | ||
"variety": "Setosa", | ||
}, | ||
) | ||
|
||
def test_mt(self): | ||
table_spec = TEST_TABLE_SPEC["tables"][1] | ||
uri = "./tap_spreadsheets_anywhere/test/mt-sample.parquet" | ||
iterator = format_handler.get_row_iterator(table_spec, uri) | ||
|
||
rows = list(iterator) | ||
self.assertEqual(len(rows), 32) | ||
self.assertEqual( | ||
rows[0], | ||
{ | ||
"model": "Mazda RX4", | ||
"mpg": 21.0, | ||
"cyl": 6, | ||
"disp": 160.0, | ||
"hp": 110, | ||
"drat": 3.9, | ||
"wt": 2.62, | ||
"qsec": 16.46, | ||
"vs": 0, | ||
"am": 1, | ||
"gear": 4, | ||
"carb": 4, | ||
}, | ||
) | ||
|
||
def test_iris_detect(self): | ||
table_spec = TEST_TABLE_SPEC["tables"][2] | ||
uri = "./tap_spreadsheets_anywhere/test/iris-sample.parquet" | ||
iterator = format_handler.get_row_iterator(table_spec, uri) | ||
|
||
rows = list(iterator) | ||
self.assertEqual(len(rows), 150) | ||
self.assertEqual( | ||
rows[0], | ||
{ | ||
"sepallength": 5.1, | ||
"sepalwidth": 3.5, | ||
"petallength": 1.4, | ||
"petalwidth": 0.2, | ||
"variety": "Setosa", | ||
}, | ||
) | ||
|
||
def test_mt_detect(self): | ||
table_spec = TEST_TABLE_SPEC["tables"][3] | ||
uri = "./tap_spreadsheets_anywhere/test/mt-sample.parquet" | ||
iterator = format_handler.get_row_iterator(table_spec, uri) | ||
|
||
rows = list(iterator) | ||
self.assertEqual(len(rows), 32) | ||
self.assertEqual( | ||
rows[0], | ||
{ | ||
"model": "Mazda RX4", | ||
"mpg": 21.0, | ||
"cyl": 6, | ||
"disp": 160.0, | ||
"hp": 110, | ||
"drat": 3.9, | ||
"wt": 2.62, | ||
"qsec": 16.46, | ||
"vs": 0, | ||
"am": 1, | ||
"gear": 4, | ||
"carb": 4, | ||
}, | ||
) |