{"name":"bob","hi":"hello"}
{"name":"hello","hi":"bye"}
有没有将这个导入CouchDB的选项?
从@Millhouse的回答开始,但我的文件中有多个文档,我使用了以下方法:
cat myFile.json | lwp-request -m POST -sS "http://localhost/dbname/_bulk_docs" -c "application/json"
POST
是lwp-request
的别名,但在debian系统上似乎不能使用POST
。如果您使用lwp-request
,需要像上面那样使用-m
设置方法。
末尾的_bulk_docs
允许一次上传多个文档。
cat myFile.json | POST -sS "http://myDB.couchone.com/testDB" -c "application/json"
我想将myFile.json中的json内容导入数据库。
如果您不喜欢命令行或不使用Linux,并且更喜欢图形用户界面,则可以使用像RESTClient这样的工具。
可能有点晚回答。但是如果您会使用Python,那么可以使用couchdb模块来实现:
import couchdb
import json
couch = couchdb.Server(<your server url>)
db = couch[<your db name>]
with open(<your file name>) as jsonfile:
for row in jsonfile:
db_entry = json.load(row)
db.save(db_entry)
couch = couchdb.server(<你的服务器URL>)
缺少URLwith open(<你的文件名>)
闭合括号而不是方括号db_entry = json.loads(row)
使用loads而不是load - Jared Beach是的,这不是有效的JSON格式...
我使用curl(http://curl.haxx.se)导入JSON对象:
curl -X PUT -d @my.json http://admin:secret@127.0.0.1:5984/db_name/doc_id
如果你的 JSON 对象在 my.json 文件中,可以将其导入。当然,你也可以直接将 JSON 对象存储到 CouchDB 中,而不需要使用文件。
curl -X PUT -d '{"name":"bob","hi":"hello"}' http://admin:secret@127.0.0.1:5984/db_name/doc_id
curl -X GET http://127.0.0.1:5984/_uuids?count=1
curl -X POST -d @data.json http://127.0.0.1:5984/database/_bulk_docs -H 'Content-Type: application/json'
- Eric Uldall那个JSON对象将不会被CouchDB接受。要使用单个服务器请求存储所有数据,请使用:
{
"people":
[
{
"name":"bob",
"hi":"hello"
},
{
"name":"hello",
"hi":"bye"
}
]
}
或者,为每一行提交不同的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
这不是我的解决方案,但我发现这个可以解决我的问题:
将CouchDB数据库导出到文件的简单方法是在终端窗口中运行以下Curl命令:
curl -X GET http://127.0.0.1:5984/[mydatabase]/_all_docs\?include_docs\=true > /Users/[username]/Desktop/db.json
{
"docs": [
{"_id": "0", "integer": 0, "string": "0"},
{"_id": "1", "integer": 1, "string": "1"},
{"_id": "2", "integer": 2, "string": "2"}
]
}
您需要关注的主要部分是在“docs”代码块中添加文档。完成后,您可以运行以下Curl命令将数据导入CouchDB数据库:
curl -d @db.json -H "Content-type: application/json" -X POST http://127.0.0.1:5984/[mydatabase]/_bulk_docs
复制数据库 如果您想将一个数据库从一个服务器复制到另一个服务器,请运行以下命令:
curl -H 'Content-Type: application/json' -X POST http://localhost:5984/_replicate -d ' {"source": "http://example.com:5984/dbname/", "target": "http://localhost@:5984/dbname/"}'
原始帖子: http://www.greenacorn-websolutions.com/couchdb/export-import-a-database-with-couchdb.php
这篇文章介绍了如何使用CouchDB导出和导入数据库。CouchDB是一个基于文档的NoSQL数据库,它使用JSON格式存储数据。在本文中,我们将学习如何使用CouchDB的命令行工具来导出和导入数据库。http://github.com/zaphar/db-couchdb-schema/tree/master
我的DB::CouchDB::Schema模块有一个脚本,可以帮助将一系列文档加载到CouchDB数据库中。couch_schema_tool.pl脚本接受文件作为参数,并将该文件中的所有文档加载到数据库中。只需将每个文档放入数组中,如下所示:
[ {"name":"bob","hi":"hello"}, {"name":"hello","hi":"bye"} ]
它会自动将它们加载到数据库中。但是有一个小问题,我还没有使用最新版本的CouchDB测试过我的代码,所以如果您使用它而出现问题,请告诉我。我可能需要更改一些内容以适应新的API更改。
Jeremy
{"docs": [documents]}
对象内部。 - Loïc Faure-Lacroix