-
Notifications
You must be signed in to change notification settings - Fork 114
add incremental processing example #1101
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,64 @@ | ||
#!/usr/bin/env python | ||
""" | ||
File Generator Script using DataChain Delta | ||
|
||
This script demonstrates: | ||
1. Creating numbered text files in a 'test' directory | ||
2. Using DataChain's delta flag for incremental dataset processing | ||
|
||
Each execution: | ||
- Creates a new numbered file in the 'test' directory | ||
- Updates a DataChain dataset to track these files incrementally | ||
""" | ||
|
||
import re | ||
import time | ||
|
||
from utils import generate_next_file | ||
|
||
import datachain as dc | ||
from datachain import C, File | ||
|
||
|
||
def extract_file_number(file: File) -> int: | ||
"""Extract file number from the filename.""" | ||
match = re.search(r"file-(\d+)\.txt", file.name) | ||
if match: | ||
return int(match.group(1)) | ||
return -1 | ||
|
||
|
||
def process_files_with_delta(): | ||
""" | ||
Process files in the test directory using DataChain with delta mode. | ||
This demonstrates incremental processing - only new files are processed. | ||
""" | ||
chain = ( | ||
dc.read_storage("test/", update=True, delta=True, delta_on="file.path") | ||
.filter(C("file.path").glob("*.txt")) | ||
.map(file_number=extract_file_number) | ||
.map(content=lambda file: file.read_text()) | ||
.map(processed_at=lambda: time.strftime("%Y-%m-%d %H:%M:%S")) | ||
.save(name="test_files") | ||
) | ||
|
||
# Show information about the dataset | ||
print(f"\nProcessed files. Total records: {chain.count()}") | ||
print("\nDataset versions:") | ||
test_dataset = dc.datasets().filter(C("name") == "test_files") | ||
|
||
for version in test_dataset.collect("version"): | ||
print(f"- Version: {version}") | ||
|
||
# Show the last 3 records to demonstrate the incremental processing | ||
print("\nLatest files processed:") | ||
chain.order_by("file_number", descending=True).limit(3).show() | ||
|
||
|
||
if __name__ == "__main__": | ||
# Generate a new file | ||
new_file = generate_next_file() | ||
print(f"Created new file: {new_file}") | ||
|
||
# Process all new file with (delta update) | ||
process_files_with_delta() |
This file contains hidden or 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,41 @@ | ||
#!/usr/bin/env python | ||
""" | ||
File Generator Helper | ||
|
||
This helper creates numbered text files in a 'test' directory each time it runs. | ||
The files follow the naming pattern: file-0.txt, file-1.txt, file-2.txt, etc. | ||
|
||
Each execution, the script: | ||
|
||
1. Creates the 'test' directory if it doesn't exist | ||
2. Finds the highest numbered file currently present | ||
3. Creates a new file with the next number in sequence | ||
4. Adds timestamped content to the file | ||
""" | ||
|
||
import re | ||
import time | ||
from pathlib import Path | ||
|
||
|
||
def generate_next_file() -> Path: | ||
""" | ||
Generate (appends) a new numbered text file in the 'test' directory. | ||
""" | ||
test_dir = Path("test") | ||
test_dir.mkdir(exist_ok=True) | ||
|
||
max_num = -1 | ||
for file in test_dir.glob("file-*.txt"): | ||
if file.is_file(): | ||
match = re.search(r"file-(\d+)\.txt", file.name) | ||
if match: | ||
max_num = max(max_num, int(match.group(1))) | ||
|
||
next_num = max_num + 1 | ||
new_file_path = test_dir / f"file-{next_num}.txt" | ||
timestamp = time.strftime("%Y-%m-%d %H:%M:%S") | ||
content = f"This is file number {next_num}\nCreated at: {timestamp}\n" | ||
new_file_path.write_text(content) | ||
|
||
return new_file_path |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Note that without explicit
delta_compare
it will look for all fields in schema exceptfile.path
(since it's indelta_on
already) to say if file is changed or not. This means that two rows need to be identical (all fields the same) in order to not count them as "modified / changed". If it count's them as changed there is no performance gain in delta update. You usually want to set `delta_compare=["file.version", "file.etag"].There is the case though with non-versioned sources where
file.version
andfile.etag
are randomly set every time on re-index which causes the same thing to happen regardless as it will catch everything as modified. In this cases, and in every other case where user doesn't even want or can track changed rows, workaround is to putdelta_update
to be the same asdelta_on
but we need a better way.Options are:
delta_compare=None
to disable tracking changed rows instead to look into all fields. If we go with this path thenDataChain.compare()
andDataChain.diff()
needs to be changed as well to be consistent.delta_ignore_changed
.I'm leaning more on first option, although then user then needs to explicitly set all fields in some cases (we loose "shortcut" of default being all fields). I don't have strong opinion though.
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, I think we are fine in this particular example (?)
where did you experience 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.
@ilongin please let me know ^^
Uh oh!
There was an error while loading. Please reload this page.
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 thought I saw it in one of our
gs
buckets but now I checked and it seems like it's ok.version
andetag
should be set to empty string if they don't exist.Regarding your example, yea you don't need to put anything as non of the columns will be changed since you only append new files. If you would re-create files every time when calling
generate_next_file
then it would be a problem as for local filesetag
we putmtime
which would mean that delta would find all files being modified every time.