Lompat ke konten

Pelajaran 8 ยท Remote Storage

Import and Reuse Data from Other DVC Projects

Reuse datasets across projects with dvc list, get, import and update, track external URLs with import-url, and read versioned files with the DVC Python API.

Good data gets reused. One team prepares a clean dataset and five other projects need it. Copying files by email creates untraceable duplicates. DVC lets a project fetch data from another Git repository that uses DVC, and remember exactly where it came from. A repository set up mainly to share datasets and models is often called a data registry.

Browse and download

Terminal
# Read-only: look inside a repository, then download one file once
dvc list https://git.example.com/team/data-registry datasets/
dvc get https://git.example.com/team/data-registry datasets/stores.csv

dvc list shows files tracked by Git and by DVC, so you can find paths. dvc get downloads a file or folder and keeps no link to the source. It is handy for a one-off copy. You need access to the Git repository and to its data storage.

Terminal
# Read-only: import and keep the connection
dvc import https://git.example.com/team/data-registry datasets/stores.csv \
  -o data/stores.csv --rev v2.1
git add data/stores.csv.dvc data/.gitignore
git commit -m "Import stores dataset v2.1"

dvc import downloads the data and writes a .dvc file that records the source repository, the path and the revision. An illustrative example:

YAML
frozen: true
deps:
- path: datasets/stores.csv
  repo:
    url: https://git.example.com/team/data-registry
    rev: v2.1
    rev_lock: 7c2f0e1a9d4b8c6e5f3a2b1c0d9e8f7a6b5c4d3e
outs:
- md5: 1b2e3d4c5a697887a6b5c4d3e2f10987
  size: 90112
  hash: md5
  path: stores.csv

--rev pins the import to a tag, branch or commit. Tags and branches can move to new content; a commit hash never changes.

Update an import

Terminal
# Read-only: bring in newer data
dvc update data/stores.csv.dvc
dvc update --rev v2.2 data/stores.csv.dvc

A plain dvc update does nothing for an import pinned to a fixed commit. Use --rev to move to another revision. Review the changes (row counts, columns, license) before you commit the new pointer, because a "minor" data update can shift model results.

Other sources

  • dvc import-url <url> <path> tracks data from a URL or storage location that is not a DVC project, such as an object in a bucket or an HTTP file. dvc update refreshes it. Options include --no-download (record without downloading) and --to-remote (send directly to your remote).
  • dvc get-url downloads from a URL without tracking.
  • dvc import-db saves a snapshot of a table or SQL query result as a CSV or JSON file. It needs a database connection set up in the DVC configuration; check the command reference for the setup.
  • Models and other files declared as artifacts in a dvc.yaml file can be fetched with dvc artifacts get.

An artifact entry is a small description inside dvc.yaml with an ID, a path, and optional type, description and labels:

YAML
artifacts:
  review-model:
    path: models/model.pkl
    type: model
    desc: Sentiment model for product reviews

Read data from Python

The dvc.api module reads versioned files without a manual checkout. This is useful in applications that need one specific version.

Python
# Read-only: stream a file from a tagged version
import dvc.api

with dvc.api.open("datasets/stores.csv",
                  repo="https://git.example.com/team/data-registry",
                  rev="v2.1") as f:
    header = f.readline()

rev accepts a branch, tag, commit hash or experiment name. remote selects which DVC remote to read from. dvc.api.read() returns the whole content in one call, and functions such as params_show and metrics_show read parameters and metrics. See the API reference for full signatures.

Try the idea

This stand-in shows why pinning matters. A branch that moves changes what you get on update; a pinned commit does not.

Python Stand-in: pinned revision versus moving branch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The rev_lock field is the exact commit that was used. Even when you follow a branch, your data does not change until you run dvc update.

Run a tidy registry

  • One folder per dataset, each with a short README describing source, license, owner and meaning of columns.
  • Tag releases (stores-v2.1) and write what changed.
  • Validate new data before tagging it.
  • Control access in Git and in storage, because both are needed.
  • Record whether the data holds personal information.

Common mistake: importing without checking the license or permission. DVC makes copying easy, but it does not tell you whether you may use the data.

A consumer's checklist

Before your project depends on someone else's data, answer a few questions and write the answers down:

  • Who owns it? You need a person or team to contact when something looks wrong.
  • What are you allowed to do? Check the license, contract or internal policy, especially for redistribution and model training.
  • How are versions named? Tags such as stores-v2.1 are easier to reason about than a moving branch.
  • What can change? Columns, units, categories and row meaning should be documented, and breaking changes announced.
  • How will you notice a bad update? Run your own validation after dvc update, before committing.

When an import is the wrong choice

Import suits stable datasets that you want to refresh deliberately. It is less suitable for data that changes every minute, such as a live event stream. For live data, take scheduled snapshots with a job that writes a file, then version each snapshot. For a tiny reference list that you edit by hand, committing the file in your own repository may be simpler than creating a dependency on another project.

Also remember that your import depends on the source repository and its storage staying available. If the source team moves or deletes data, your pull can fail. Pushing the imported data to your own remote and agreeing on retention with the owner reduces that risk. Check your policy before you keep extra copies.

Reviewing an update

When an update arrives, treat it like a dependency upgrade in code. Create a branch, run dvc update, then compare row counts, column names and a few summary numbers with the previous version. Run your pipeline and compare metrics. If something looks wrong, discard the branch, and your main branch keeps the old pinned data. Finally, tell the owner what you found, so other consumers do not repeat your investigation.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command downloads data but keeps no connection to the source?

    dvc get only downloads. dvc import also records where the data came from.
  2. 2. An import is pinned to a specific commit hash. What does a plain dvc update do?

    A commit never changes. Use --rev to choose another one.
  3. 3. What does dvc.api.open() let you do?

    It streams a tracked file from a project, local or remote, at the revision you ask for.
  4. 4. Which command is meant for a URL that is not a DVC project?

    import-url tracks external data and can be refreshed with dvc update.

Summary

  • dvc list browses, dvc get copies once, dvc import copies and remembers.
  • Pin revisions and use dvc update --rev to move deliberately.
  • dvc import-url tracks data from non-DVC sources.
  • dvc.api reads versioned files directly from Python.
  • Document and license-check data in a shared registry.

Next: Build Your First DVC Pipeline

Progres Anda disimpan di browser ini. Tidak perlu akun.