【问题标题】:How to access DVC-controlled files from Oracle?如何从 Oracle 访问 DVC 控制的文件?
【发布时间】:2021-04-02 21:45:23
【问题描述】:

我一直将我的大文件存储在 Oracle 中的 CLOB 中,但我正在考虑将我的大文件存储在共享驱动器中,然后在 Oracle 中有一列包含指向这些文件的指针。这将使用 DVC。

当我这样做时,

(a) 是指向我共享驱动器中文件的 Oracle 路径中的路径,如实际文件本身?

(b) 还是 Oracle 中的路径以某种方式指向 DVC 元文件?

任何见解都会帮助我!

谢谢 :) 贾斯汀


编辑以提供更多清晰度:

我检查了这里 (https://dvc.org/doc/api-reference/open),它有所帮助,但我还没有完全到那里......

我想使用 python(我已连接到 Oracle 数据库)从远程 dvc 存储库中提取文件。所以,如果我们能做到这一点,我想我会很好。但是,我很困惑。如果我在下面指定'remote',那么当远程文件都被编码时,我如何命名文件(例如,'activity.log')?

with dvc.api.open(
        'activity.log',
        repo='location/of/dvc/project',
        remote='my-s3-bucket'
        ) as fd:
    for line in fd:
        match = re.search(r'user=(\w+)', line)
        # ... Process users activity log

(注意:出于测试目的,我的“远程”DVC 目录只是我 MacBook 上的另一个文件夹。)

我觉得我错过了一个关于获取远程文件的关键概念......

我希望这会增加更多的清晰度。任何帮助确定远程文件访问的帮助表示赞赏! :)

贾斯汀


编辑以了解“rev”参数:

在我的问题之前,一些背景/我的设置: (a) 我的 MacBook 上有一个名为“basics”的存储库。 (b) 我将包含 501 个文件(称为“surface_files”)的目录复制到“basics”中,随后将其推送到名为“gss”的远程存储文件夹中。推送后,'gss' 包含 220 个哈希目录。

我以前到这里的步骤如下:

> cd ~/Desktop/Work/basics
> git init
> dvc init
> dvc add ~/Desktop/Work/basics/surface_files
> git add .gitignore surface_files.dvc
> git commit -m "Add raw data"
> dvc remote add -d remote_storage ~/Desktop/Work/gss
> git commit .dvc/config -m "Configure remote storage"
> dvc push
> rm -rf ./.dvc/cache
> rm -rf ./surface_files

接下来,我运行以下 Python 代码来获取我的一个名为 surface_100141.dat 的表面文件,并使用 dvc.api.get_url() 获取相应的远程存储文件名。然后我将这个远程存储文件复制到我的桌面,使用文件的原始名称,即surface_100141.dat

完成这一切的代码如下,但首先,我的问题 --- 当我运行如下所示的代码时,没有问题;但是当我取消注释'rev ='行时,它失败了。我不确定为什么会这样。我使用git logcat .git/refs/heads/master 来确保我得到了正确的哈希值。为什么会失败?这是我的问题。

(完全披露,我的 git 知识还不是太强。我已经到了那里,但它仍在进行中!:))

import dvc.api
import os.path
from os import path
import shutil

filename = 'surface_100141.dat' # This file name would be stored in my Oracle database
home_dir = os.path.expanduser('~')+'/' # This simply expanding '~' into '/Users/ricej/'

resource_url = dvc.api.get_url(
    path=f'surface_files/{filename}', # Works when 'surface_files.dvc' exists, even when 'surface_files' directory and .dvc/cache do not
    repo=f'{home_dir}Desktop/Work/basics',
    # rev='5c92710e68c045d75865fa24f1b56a0a486a8a45', # Commit hash, found using 'git log' or 'cat .git/refs/heads/master'
    remote='remote_storage')
resource_url = home_dir+resource_url
print(f'Remote file: {resource_url}')

new_dir = f'{home_dir}Desktop/' # Will copy fetched file to desktop, for demonstration
new_file = new_dir+filename
print(f'Remote file copy: {new_file}')

if path.exists(new_file):
    os.remove(new_file)
    
dest = shutil.copy(resource_url, new_file) # Check your desktop after this to see remote file copy

【问题讨论】:

  • 文件通常存储在BLOB 列而不是CLOB 列中,除非文件是纯文本,如果客户端碰巧请求与数据库不同的字符集。当您说您正在考虑存储指向文件的指针时,您是在谈论BFILE 列(它只是对文件系统路径的引用)吗?或者您是在谈论存储路径字符串的VARCHAR2 列?

标签: python oracle dvc


【解决方案1】:

我不能 100% 确定我理解了这个问题(最好根据您尝试使用此数据库解决的实际用例来扩展它),但我可以分享一些想法。

当我们谈论 DVC 时,我认为您需要指定一些东西来识别文件/目录:

  1. Git 提交 + 路径(实际路径如 data/data/xml)。需要提交(或者准确地说是任何 Git 修订版)来识别数据文件的版本。
  2. 或 DVC 存储中的路径(/mnt/shared/storage/00/198493ef2343ao ...) + actual name of this file. This way you would be saving info that .dvc` 文件有。

我会说第二种方式是推荐的,因为在某种程度上它是一个实现细节 - DVC 如何在内部存储文件。 DVC 组织数据存储的公共接口是它的存储库 URL + 提交 + 文件名。

编辑(示例):

with dvc.api.open(
        'activity.log',
        repo='location/of/dvc/project',
        remote='my-s3-bucket'
        ) as fd:
    for line in fd:
        match = re.search(r'user=(\w+)', line)
        # ... Process users activity log

location/of/dvc/project 此路径必须指向实际的 Git 存储库。这个 repo 应该有一个 .dvcdvc.lock 文件,其中包含 activity.log 名称 + 它在远程存储中的哈希:

outs:
  - md5: a304afb96060aad90176268345e10355
    path: activity.log

通过阅读这个 Git 存储库并分析假设 activity.log.dvc DVC 将能够创建正确的路径 s3://my-bucket/storage/a3/04afb96060aad90176268345e10355

remote='my-s3-bucket' 参数是可选的。默认情况下,它将使用在 repo 本身中定义的那个。

我们再举一个真实的例子:

with dvc.api.open(
        'get-started/data.xml',
        repo='https://github.com/iterative/dataset-registry'
        ) as fd:
    for line in fd:
        match = re.search(r'user=(\w+)', line)
        # ... Process users activity log

https://github.com/iterative/dataset-registry 中,您可以找到足以让DVC 通过分析其config 来创建文件路径的.dvc file

https://remote.dvc.org/dataset-registry/a3/04afb96060aad90176268345e10355

你可以在这个文件上运行wget来下载它

【讨论】:

  • 谢谢,@Shcheklein :) 请参阅我上面的编辑以提供更多详细信息 :)
  • @贾斯汀谢谢!我已经用一些细节更新了答案。如果还不清楚,请告诉我,您需要更多信息。
  • 再次感谢@Shcheklein :) 在玩弄了这个之后,我想我有一些有用的东西---有一个例外---我还没有成功使用@的rev=参数987654341@,所以我不知道如何指定正确的提交。如果可以的话,你能看看我对我原来帖子的最新编辑吗?我已经展示了我的所有步骤,希望您能帮助我了解为什么我没有成功使用rev= 参数。感谢您在这里的任何帮助/见解!你一直很有帮助! (P.S.,我也是 discord 上的 ricemcm 人。)
  • @Justin 我认为社区的 Saugat 已经处理了这个问题-it's a known issue with 2.0 version: https://github.com/iterative/dvc/issues/5590 The workaround for now is to use file:/// urls in the repo. That way, DVC will clone the repo to read the file. 如果有任何其他问题,请随时联系我们 :)
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-21
  • 1970-01-01
  • 2016-01-31
  • 2013-03-15
  • 2017-10-24
相关资源
最近更新 更多