【问题标题】:Deleting all rows in dataset with SODA API使用 SODA API 删除数据集中的所有行
【发布时间】:2015-01-08 22:30:10
【问题描述】:

我正在尝试使用 SODA API 以编程方式删除 Socrata 数据集中的所有行。我不想删除数据集本身,因为那样我就有很多工作来重建它。对数千行执行此操作也很慢。

以前,我缓慢地遍历每一行,删除每一行,直到 someone on Twitter 建议做一个没有行的 Upsert。我相信我实现了这个并且它有效,但现在它没有。

以下是相关的代码:

headers = {'X-App-Token': config.app_token}
headers['Content-Type'] = 'application/json'

r = requests.put(config.dataset + '.json', data='[ ]', headers=headers, auth=config.auth)
print r.status_code, r.text

输出是:

200 {
  "By RowIdentifier" : 0,
  "Rows Updated" : 0,
  "Rows Deleted" : 0,
  "Rows Created" : 0,
  "Errors" : 0,
  "By SID" : 0
}

(所以我认为可以肯定地说问题与身份验证、授权等无关?我的其他更新行的功能工作正常,因此错误的数据集 URL 或任何其他问题也不是问题。)

我也查询了前后的行数,没有变化。仍然有数千行。

据我所知,我正在关注Replacing rows in bulk 的 API 文档。

我唯一能想到的是,由于一个错误,我有多个具有相同 rowid 的行。

编辑

这里有一些重复的行标识符:

rowid 肯定设置为行标识符:

现在考虑到行标识符应该是“essentially act the same way as primary keys”,我开始怀疑这是一个错误,还是出了什么可怕的问题?发布代码如下:

def publishDataset(rows):
  r = requests.post(config.dataset, data=simplejson.dumps(rows), headers = headers, auth=config.auth)
  j = r.json()
  print
  if r.status_code != 200:
    raise RuntimeError ( "%d Socrata error: %s" % (r.status_code, j['message']))
  return j

完整代码:https://github.com/stevage/meshlium-soda

【问题讨论】:

    标签: python json rest put socrata


    【解决方案1】:

    很抱歉延迟回复您。我认为文档中有一个错误,您应该在 API 端点上使用 DELETE:

    %> curl --verbose -X DELETE --header "X-App-Token: [REDACTED]" --user [REDACTED] https://soda.demo.socrata.com/resource/ppbu-8a96.json
    Enter host password for user '[REDACTED]':
    * Hostname was NOT found in DNS cache
    *   Trying 216.227.229.224...
    * Connected to soda.demo.socrata.com (216.227.229.224) port 443 (#0)
    * TLS 1.0 connection using TLS_DHE_RSA_WITH_AES_256_CBC_SHA
    * Server certificate: *.demo.socrata.com
    * Server certificate: AlphaSSL CA - SHA256 - G2
    * Server certificate: GlobalSign Root CA
    * Server auth using Basic with user '[REDACTED]'
    > DELETE /resource/ppbu-8a96.json HTTP/1.1
    > Authorization: Basic [REDACTED]
    > User-Agent: curl/7.37.1
    > Host: soda.demo.socrata.com
    > Accept: */*
    > X-App-Token: [REDACTED]
    >
    < HTTP/1.1 200 OK
    * Server nginx is not blacklisted
    < Server: nginx
    < Date: Fri, 09 Jan 2015 06:31:16 GMT
    < Content-Type: application/json; charset=utf-8
    < Transfer-Encoding: chunked
    < Connection: keep-alive
    < Access-Control-Allow-Origin: *
    < X-Socrata-Region: production
    < Age: 14
    <
    

    这应该可以解决问题。如果它也适合你,我会更新我的文档。很抱歉造成混乱!

    【讨论】:

    • 嗯,确实有效。但其含义令人困惑。 1.PUT documentation是完全错误的,没有办法真正替换数据集的内容? 2. 这种不带行标识符的 DELETE 使用没有记录。 3. 这种使用 DELETE 来删除资源的 内容 而不是 资源本身 可能不是标准的。 4. 为什么它之前有效? :)
    猜你喜欢
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    相关资源
    最近更新 更多