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

Add example for file streaming #1759

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions examples/data_types_and_io/data_types_and_io/file_streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from flytekit import task, workflow
from flytekit.types.file import FlyteFile
from flytekit.types.directory import FlyteDirectory
import pandas as pd
import os


@task()
def remove_some_rows(ff: FlyteFile) -> FlyteFile:
"""
Remove the rows that the value of city is 'Seattle'.
This is an example with streaming support.
"""
new_file = FlyteFile.new_remote_file("data_without_seattle.csv")
with ff.open("r") as r:
with new_file.open("w") as w:
df = pd.read_csv(r)
df = df[df["City"] != "Seattle"]
df.to_csv(w, index=False)
return new_file


@task
def process_folder(fd: FlyteDirectory) -> FlyteDirectory:
out_fd = FlyteDirectory.new_remote("folder-copy")
for base, x in fd.crawl():
src = str(os.path.join(base, x))
out_file = out_fd.new_file(x)
with FlyteFile(src).open("rb") as f:
with out_file.open("wb") as o:
o.write(f.read())
# The output path will be s3://my-s3-bucket/data/77/<execution-id>-<node-id>-0/folder-copy
return out_fd


@workflow()
def wf():
remove_some_rows(ff=FlyteFile("s3://custom-bucket/data.csv"))
process_folder(fd=FlyteDirectory("s3://my-s3-bucket/folder"))
return


if __name__ == "__main__":
print(f"Running wf() {wf()}")