导入json文件到沙发db-

10 json couchdb

如果我有一个看起来像这样的json文件:

{"name":"bob","hi":"hello"}
{"name":"hello","hi":"bye"}
Run Code Online (Sandbox Code Playgroud)

有没有选项将其导入couchdb?

the*_*dow 6

从@Millhouse回答开始,但我使用的文件中包含多个文档

cat myFile.json | lwp-request -m POST -sS "http://localhost/dbname/_bulk_docs" -c "application/json" 
Run Code Online (Sandbox Code Playgroud)

POST是别名,lwp-requestPOST似乎没有在debian上工作.如果使用lwp-request,则需要-m按上述方法设置方法.

尾随_bulk_docs允许一次上载多个文档.

http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API


Mil*_*use 5

如果您使用的是Linux,则可以编写一个快速shell脚本,将有效json文件的内容POST到Couch.

为了测试沙发我做了这样的事情:

cat myFile.json | POST -sS "http://myDB.couchone.com/testDB" -c "application/json"
Run Code Online (Sandbox Code Playgroud)

myFile.json有我想导入数据库的json内容.

另一种选择,如果你不喜欢命令行或者不使用Linux,并且更喜欢gui,你可以使用像RESTClient这样的工具


Mat*_*hen 2

CouchDB 不会接受该 JSON 对象。要通过单个服务器请求存储所有数据,请使用:

{
  "people": 
   [
      {
        "name":"bob",
        "hi":"hello"
      },
      { 
        "name":"hello",
        "hi":"bye"
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

或者,为每行提交不同的 CouchDB 请求。

使用 cURL 从命令行将文件导入 CouchDB:

curl -vX POST https://user:pass@127.0.0.1:1234/database \
  -d @- -# -o output -H "Content-Type: application/json" < file.json
Run Code Online (Sandbox Code Playgroud)