【问题标题】:Seeking the simplest method of updating CKAN linked resources regularly求最简单的定期更新CKAN链接资源的方法
【发布时间】:2018-12-17 15:22:10
【问题描述】:

我正在寻找更新链接资源时更新 CKAN 数据存储的最简单方法。在这种情况下,所有资源都已链接(不上传)。资源是 csv 并定期更新。当 csv 文件发生更新时,CKAN 的 DataStore 似乎不会自动获取这些更改。我尝试使用 ckanapi,但 update_resource 函数似乎只更新元数据。我无法让它持续更新 DataStore(因此 Data Explorer 视图包含过时的信息)。

除非有更简单的方法,否则我的偏好是找到一种以编程方式触发“上传到数据存储”按钮的方法,该按钮可以在给定资源的“数据存储”选项卡上找到。我已经进行了一些相当广泛的搜索,但还没有找到一种方法来做到这一点。任何建议表示赞赏。

CKAN 的当前版本是 CKAN 2.8.1,启用了 DataStore 和 DataPusher 扩展。

【问题讨论】:

    标签: datastore ckan


    【解决方案1】:

    您应该能够通过使用 CKAN API 的脚本来执行此操作,特别是 datapusher_submit(见下文)。

    这是我过去使用的示例 python 脚本。

    还有一个PR open 可以帮助更好地记录这一点,但尚未合并。

    #!/usr/bin/env python
    import urllib2
    import urllib
    import json
    import pprint
    
    
    # We'll use the package_search function to get all of the resources.
    # NOTE: there may be a limit on this in the future and would have to then make multiple calls to collect
    # all of the resources. Datasets has a hard-limit of 1000 but defaults to 10. So for now this works, but future issue maybe.
    resources_request = urllib2.Request(
        'http://ckan-site.com/api/3/action/resource_search?query=name:')
    
    # Make the HTTP request.
    resources_response = urllib2.urlopen(resources_request)
    # Make sure it worked
    assert resources_response.code == 200
    
    # Use the json module to load CKAN's response into a dictionary.
    resources_response_dict = json.loads(resources_response.read())
    assert resources_response_dict['success'] is True
    
    results = resources_response_dict['result']['results']
    
    for result in results:
      '''Loop over the resources and submit them to the datastore.
      '''
      try:
        request = urllib2.Request('http://ckan-site.com/api/3/action/datapusher_submit')
    
        data_dict = {
          "resource_id":result['id']
          }
    
        data_string = urllib.quote(json.dumps(data_dict))
    
        request.add_header('Authorization', 'your-token-here')
    
        response = urllib2.urlopen(request, data_string)
        assert json.loads(response.read())['success'] is True
      except:
        # Catch and print any issues and keep going.
        print "resource_id: "  + result['id']
        continue
    
    print "Complete. Datastore is now up to date."
    

    【讨论】:

    • 感谢 user3366016。您提供的解决方案在 python 2.7 中完美运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多