0

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

以前,我在每一行上慢慢迭代,删除每一行,直到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 或其他任何东西也不是问题。)

我也查询了前后的行数,没有变化。还是几千行。

据我所知,我正在关注批量替换行的 API 文档。

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

编辑

以下是一些重复的行标识符:

在此处输入图像描述

rowid绝对设置为行标识符:

在此处输入图像描述

现在考虑到行标识符应该“基本上与主键的行为方式相同”,我开始怀疑这是一个错误,还是出现了可怕的错误?发布代码如下所示:

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

4

1 回答 1

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
<

这应该够了吧。如果它也适合你,我会更新我的文档。对困惑感到抱歉!

于 2015-01-09T06:33:25.773 回答