跳转到主要内容

未提供项目描述

项目描述

Tests

ckanext-federated-index

一种轻量级的解决方案,用于在本地存储和搜索远程数据集,并在打开数据集详情页面时重定向到原始门户网站。

当前扩展类似于ckanext-harvest。主要区别在于

  • ckanext-harvest是一种通用的数据采集解决方案,可以从任何类型的源中采集数据。ckanext-federated-index仅与CKAN实例一起工作
  • ckanext-harvest使用后台进程进行数据采集。它更复杂,可自定义,更灵活,但同时也更复杂。ckanext-federated-index仅依赖于CKAN API,可以通过HTTP请求、CLI命令或cron任务触发,而不需要额外的复杂性。
  • ckanext-harvest在本地创建远程数据集的副本。如果您想创建对这些数据集的引用、编辑它们或修改本地副本,则这更合适。ckanext-federated-index将数据集添加到搜索索引中,但不创建实际的本地数据集。因此,您可以搜索这些数据集,但不能在本地打开它们。相反,您可以使用数据集的原始URL并将用户重定向到原始门户网站。

因此,ckanext-federated-index在您构建从多个门户获取数据的轻量级聚合器时表现最佳,但不提供任何查看或编辑功能。ckanext-harvest更适合其他任何情况,因为它基本上允许您对远程数据集执行任何操作。

要求

与核心CKAN版本兼容性

CKAN版本 兼容?
2.9 不兼容
2.10 兼容
2.11 兼容

安装

要安装ckanext-federated-index

  1. 通过pip安装

    pip install ckanext-federated-index
    
  2. federated-index添加到您的CKAN配置文件中的ckan.plugins设置。

用法

要索引远程数据集,您需要配置一个或多个联盟配置文件。每个配置文件描述远程门户,并定义如何获取和存储其数据。

每个配置文件必须有一个唯一的名称,其配置选项定义如下ckanext.federated_index.profile.<PROFILE_NAME>.<OPTION>。例如,如果您决定索引demo.ckan.org并希望使用名称demo,您必须将以下选项添加到配置文件中

ckanext.federated_index.profile.demo.url = https://demo.ckan.org

如果,除了URL之外,您还想为请求指定API令牌

ckanext.federated_index.profile.demo.url = https://demo.ckan.org
ckanext.federated_index.profile.demo.api_key = 123-abc

所有可用配置选项均在上面的配置设置部分中提及。

配置配置文件后,您需要做的唯一一件事是刷新门户数据。如果您的本地和远程门户具有类似的元数据模式,则无需额外努力即可正常工作。如果模式不同,请检查高级用法部分。

ckanapi action federated_index_profile_refresh profile=demo index=true

高级用法

对齐元数据模式

通常,当远程门户高度定制并定义大量自定义元数据字段时,最简单的选项是丢弃所有未在本地元数据模式中定义的字段。可以通过配置选项完成此操作

ckanext.federated_index.align_with_local_schema = true

如果这还不够,您可以在索引过程之前钩入并修改数据集字典,然后将其发送到搜索索引。为此,您可以使用IFederatedIndex接口

import ckan.plugins as p
from ckanext.federated_index.interfaces import IFederatedIndex
from ckanext.federated_index.shared import Profile

class CustomFederatedIndexPlugin(p.SingletonPlugin):
    p.implements(interfaces.IFederatedIndex, inherit=True)

    def federated_index_before_index(
        self,
        pkg_dict: dict[str, Any],
        profile: Profile,
    ) -> dict[str, Any]:

        # modify data. For example, remove all tags with vocabulary_id,
        # because local instance usually does not have same vocabulary IDs
        pkg_dict["tags"] = [
            t for t in pkg_dict.setdefault("tags", []) if "vocabulary_id" not in t
        ]

        return pkg_dict

获取比本地索引中最新数据集更新的数据集

在初始同步期间,您通常需要从远程门户拉取所有数据集。但是在此之后,您只对具有大于同步数据集中最新metadata_modified值的metadata_modified值的数据集感兴趣。基本上,您只想获取更新的数据集以加快过程。为此,请将since_last_refresh标志添加到刷新索引的操作中

ckanapi action federated_index_profile_refresh profile=demo index=true since_last_refresh=true

配置远程数据获取过程

ckanext-federated-index使用默认参数通过package_search API操作从远程门户获取数据。如果您想通过单个请求获取更多包,或使用q/fq过滤某些数据集,您可以通过设置添加搜索配置到配置文件

ckanext.federated_index.profile.demo.extras = {"search_payload": {"rows": 100, "q": "test"}}

配置文件中的extras选项包含一个有效的JSON对象,其中包含配置文件的附加设置。search_payload指定用于package_search的默认参数。

除此之外,如果您只想使用自定义搜索有效载荷一次,可以将search_payload传递给刷新操作

ckanapi action federated_index_profile_refresh profile=demo index=true search_payload='{"q": "test"}'

配置远程数据的存储

默认情况下,远程数据存储在单独的数据库表中。这允许您一次性拉取远程数据,然后多次重建远程包的索引,而无需向远程门户发出额外请求。数据库表被选为默认值,因为它可以有效地使用空间,允许快速访问数据,并且CKAN实例上都有,因为CKAN没有数据库就无法运行。

但是,还有其他存储类型,每个联盟配置文件都可以通过extras选项配置为使用不同的存储类型

ckanext.federated_index.profile.demo.extras = {"storage": {"type": "redis"}}

type键是storage对象的一个必需成员。根据存储类型,还可以支持其他键。例如,文件系统存储允许您指定数据存储的路径

ckanext.federated_index.profile.demo.extras = {"storage": {"type": "fs", "path": "/tmp/demo_profile"}}

存储类型

  • db:默认存储。在迁移过程中创建的自定义表中保留数据
  • fs:将数据作为单独的JSON文件存储在文件系统中。默认情况下,文件存储在ckan.storage_path/federated_index/PROFILENAME下。路径可以通过path选项进行更改。
  • redis:在Redis中保存数据。
  • sqlite:为每个配置文件单独存储SQLite数据库。默认情况下,数据库创建在ckan.storage_path/federated_index/PROFILENAME.sqlite3.db。路径可以通过url选项进行更改。

配置设置

# Remove from dataset any field that is not defined in the local dataset
# schema.
# (optional, default: false)
ckanext.federated_index.align_with_local_schema = false

# Redirect user to the original dataset URL when user opens federated dataset
# that is not recorded in local DB.
# (optional, default: true)
ckanext.federated_index.redirect_missing_federated_datasets = true

# Endpoints that are affected by `redirect_missing_federated_datasets` config
# option.
# (optional, default: dataset.read)
ckanext.federated_index.dataset_read_endpoints = dataset.read dataset.edit

# Name of the dataset extra field that holds original URL of the federated
# dataset.
# (optional, default: federated_index_remote_url)
ckanext.federated_index.index_url_field = federated_index_remote_url

# Name of the dataset extra field that holds federation profile name.
# (optional, default: federated_index_profile)
ckanext.federated_index.index_profile_field = federated_index_profile

# URL of the federation profile.
ckanext.federated_index.profile.<profile>.url = https://demo.ckan.org

# API Token for the federation profile.
ckanext.federated_index.profile.<profile>.api_key = 123-abc

# Extra configuration for federation profile. Must be a valid JSON object
# with the following keys:
#  * search_payload: payload sent to remote portal with
#    `package_search` API action when profile is refreshed
#  * storage: storage configuration for remote data. Requires `type`
#    parameter with one of the following values: redis, db, sqlite, fs.
ckanext.federated_index.profile.<profile>.extras = {"search_payload": {"rows": 100}, "storage": {"type": "fs"}}

# Request timeout for remote portal requests.
ckanext.federated_index.profile.<profile>.timeout = 5

开发者安装

要为开发安装ckanext-federated-index,请激活您的CKAN虚拟环境,然后执行:

git clone https://github.com/DataShades/ckanext-federated-index.git
cd ckanext-federated-index
pip install -e.

测试

要运行测试,请执行:

pytest

许可协议

AGPL

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

ckanext_federated_index-0.1.1.tar.gz (35.0 kB 查看散列)

上传时间

构建分发

ckanext_federated_index-0.1.1-py3-none-any.whl (37.9 kB 查看散列)

上传时间 Python 3

由以下支持