Skip to content
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

Merged
merged 12 commits into from Sep 18, 2020

Conversation

robertwb
Copy link
Contributor


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Choose reviewer(s) and mention them in a comment (R: @username).
  • Format the pull request title like [BEAM-XXX] Fixes bug in ApproximateQuantiles, where you replace BEAM-XXX with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

Post-Commit Tests Status (on master branch)

Lang SDK Dataflow Flink Samza Spark Twister2
Go Build Status --- Build Status --- Build Status ---
Java Build Status Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status Build Status
Build Status
Build Status
Build Status
Python Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
--- Build Status ---
XLang Build Status --- Build Status --- Build Status ---

Pre-Commit Tests Status (on master branch)

--- Java Python Go Website Whitespace Typescript
Non-portable Build Status Build Status
Build Status
Build Status
Build Status
Build Status Build Status Build Status Build Status
Portable --- Build Status --- --- --- ---

See .test-infra/jenkins/README for trigger phrase, status and link of all Jenkins jobs.

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests

See CI.md for more information about GitHub Actions CI.

@robertwb
Copy link
Contributor Author

R: @chamikaramj

@codecov
Copy link

codecov bot commented Sep 15, 2020

Codecov Report

Merging #12841 into master will increase coverage by 0.02%.
The diff coverage is 90.09%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
sdks/python/apache_beam/dataframe/io.py 89.62% <89.62%> (ø)
sdks/python/apache_beam/dataframe/frames.py 90.02% <100.00%> (+0.11%) ⬆️
sdks/python/apache_beam/io/fileio.py 94.07% <0.00%> (+0.34%) ⬆️
sdks/python/apache_beam/dataframe/expressions.py 90.44% <0.00%> (+0.73%) ⬆️
sdks/python/apache_beam/io/source_test_utils.py 89.63% <0.00%> (+1.35%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bbda220...5002508. Read the comment docs.

Copy link
Contributor

@chamikaramj chamikaramj left a 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):
Copy link
Contributor

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).

Copy link
Contributor Author

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)

Copy link
Contributor

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) ?

Copy link
Contributor Author

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)
Copy link
Contributor

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).

Copy link
Contributor Author

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):
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@robertwb
Copy link
Contributor Author

Thanks for the feedback, I'll revise based on ReadableFile and WriteToFiles (if they're flexible enough).

@robertwb
Copy link
Contributor Author

Updated to use the new fileio framework. (As an aside, I noticed that it's still marked experimental, should that be dropped.)

PTAL

Copy link
Contributor

@chamikaramj chamikaramj left a 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
Copy link
Contributor

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) ?

Copy link
Contributor Author

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):
Copy link
Contributor

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) ?

Copy link
Contributor Author

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:
Copy link
Contributor

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 ?

Copy link
Contributor Author

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.

@robertwb
Copy link
Contributor Author

Thanks. I'll merge once I verify all tests are still passing after the merge.

@robertwb robertwb merged commit c867591 into apache:master Sep 18, 2020
ibzib pushed a commit to ibzib/beam that referenced this pull request Sep 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants