New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BEAM-10894] Basic CSV reading and writing. #12841
Conversation
R: @chamikaramj |
Codecov Report
@@ Coverage Diff @@
## master #12841 +/- ##
==========================================
+ Coverage 82.31% 82.33% +0.02%
==========================================
Files 451 452 +1
Lines 53874 53985 +111
==========================================
+ Hits 44344 44449 +105
- Misses 9530 9536 +6
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
from apache_beam.dataframe import frame_base | ||
|
||
|
||
def read_csv(path, *args, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add pydocs for public API here (or add a TODO/JIRA for this).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here is to mirror the Pandas APIs. I suppose I should reference them at least.
|
||
def read_csv(path, *args, **kwargs): | ||
return _ReadFromPandas(pd.read_csv, path, args, kwargs) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to provide these as methods instead of transforms (similar to other IO connectors) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mirroring the Pandas APIs. The _ReadFromPandas will be widely shared.
|
||
pcoll = ( | ||
paths_pcoll | ||
| beam.FlatMap(expand_pattern) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably we can replace above file-handling related transforms with transforms available in fileio.
https://github.com/apache/beam/blob/master/sdks/python/apache_beam/io/fileio.py
For example,
fileio.MatchFiles(self.path) | ParDo(_ReadFromPandasFromReadableFileDoFn())
(ReadableFile.metadata.path gives the file path).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
self.binary)) | ||
|
||
|
||
class _WriteToPandasFileBasedSink(io.FileBasedSink): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of extending FileBasedSink here please implement a fileio.FileSink and use fileio.WriteToFiles.
https://github.com/apache/beam/blob/master/sdks/python/apache_beam/io/fileio.py#L76
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Thanks for the feedback, I'll revise based on ReadableFile and WriteToFiles (if they're flexible enough). |
Updated to use the new fileio framework. (As an aside, I noticed that it's still marked experimental, should that be dropped.) PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks LGTM.
Regarding, experiment tags, I believe we can remove those now since that fileio transforms have been available in multiple Beam releases. cc: @pabloem
|
||
|
||
def write_csv(df, path, *args, **kwargs): | ||
from apache_beam.dataframe import convert |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import at top (here and below) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was running into circular import issues. Will add a comment.
yield line.rstrip('\n') | ||
|
||
@unittest.skipIf(sys.version_info[0] < 3, 'unicode issues') | ||
def test_write_csv(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_read_csv (seems like this is testing read_csv) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tests tests both read and write.
def test_write_csv(self): | ||
input = self.temp_dir({'1.csv': 'a,b\n1,2\n', '2.csv': 'a,b\n3,4\n'}) | ||
output = self.temp_dir() | ||
with beam.Pipeline() as p: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test for write_cvs as well ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tests tests both read and write. Updated name.
Thanks. I'll merge once I verify all tests are still passing after the merge. |
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
R: @username
).[BEAM-XXX] Fixes bug in ApproximateQuantiles
, where you replaceBEAM-XXX
with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.CHANGES.md
with noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
Post-Commit Tests Status (on master branch)
Pre-Commit Tests Status (on master branch)
See .test-infra/jenkins/README for trigger phrase, status and link of all Jenkins jobs.
GitHub Actions Tests Status (on master branch)
See CI.md for more information about GitHub Actions CI.