使用Mathematica将文档插入CouchDB中

问题描述 投票:3回答:1

我正在寻找一种使用Mathematica将文档插入CouchDB数据库的方法。基于this帖子,我使用以下代码进行了尝试:

InsertDocument[key_, value_] := 
With[{url = 
  "http://couchdburl/database/"},
Import[url, "XML", "RequestMethod" -> "POST", 
 "RequestParameters" -> {"key" -> key, "value" -> value}]]

但是,当我尝试像这样执行它时:

InsertDocument[110, 1]

我收到以下错误:

Import :: erropts:为选项RequestParameters指定的值{key-> 110,value-> 1}无效。 >>

编辑:

在kguler的注释之后,我将参数转换为字符串,并且先前的错误消失了。我还修复了代码示例中被遗忘的},并删除了带有key参数的url串联。现在我遇到了另一个错误:

Throw :: nocatch:未捕获的Throw [Null,UtilitiesURLToolsPrivateURLTOOLSException[UtilitiesURLToolsPrivateBADCONNECTION,http://couchdburl/database/]]返回顶部。 >>

我尝试通过使用虚拟文档使用curl更改到couchDb url来发出请求:

curl -X POST http://couchdburl/database/ -H "Content-Type: application/json" -d {}

和预期的响应:

{“ ok”:true,“ id”:“ 57291ccea74c455beb2d7a37fe001624”,“ rev”:“ 1-967a00dff5e02add41819138abb3284d”}]

我仍然缺少导入功能中应该使用的任何选项吗?也许有些选项可以将内容类型设置为application / json?

http wolfram-mathematica couchdb
1个回答
1
投票

我按照this SO问题中提出的相同想法设法做到这一点。

出现一些问题后,以下代码起作用了:

<< JLink`

client = JavaNew["org.apache.commons.httpclient.HttpClient"];

method = JavaNew["org.apache.commons.httpclient.methods.PostMethod", 
   "http://couchdburl/database/"];

method@setRequestHeader["Content-Type", "application/json"];

entity = 
 JavaNew["org.apache.commons.httpclient.methods.StringRequestEntity", 
  "{\"key\":\"10\",\"value\":\"0\"}", "application/json", Null]

method@setRequestEntity[entity]

client@executeMethod[method]
© www.soinside.com 2019 - 2024. All rights reserved.