如果我有一个看起来像这样的 json 文件:
{"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
但POST
似乎不适用于 debian。如果你使用lwp-request
你需要-m
像上面那样设置方法。
尾随_bulk_docs
允许一次上传多个文档。
如果您使用的是 Linux,您可以编写一个快速的 shell 脚本来将有效 json 文件的内容发布到 Couch。
为了测试沙发,我做了这样的事情:
cat myFile.json | POST -sS "http://myDB.couchone.com/testDB" -c "application/json"
myFile.json 有我想要导入数据库的 json 内容。
另一种选择,如果您不喜欢命令行或不使用 Linux,并且更喜欢 gui,则可以使用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)
我创建了 python 脚本来做到这一点(因为我在 Internet 上找不到)。
完整的脚本在这里::
http://bitbucket.org/tdatta/tools/src/
(名称 --> jsonDb_to_Couch.py)
如果您下载完整的 repo 并且:
文本将 json 文件中的所有“_id”替换为“id”
运行 make load_dbs
它将在您的本地沙发安装中创建 4 个数据库
希望对新手有帮助(比如我)
是的,这不是有效的 JSON ...
要导入 JSON 对象,我使用 curl (http://curl.haxx.se):
curl -X PUT -d @my.json http://admin:secret@127.0.0.1:5984/db_name/doc_id
其中 my.json 是 JSON-Object 所在的文件。当然,您也可以将 JSON-Object 直接放入 couchdb(没有文件):
curl -X PUT -d '{"name":"bob","hi":"hello"}' http://admin:secret@127.0.0.1:5984/db_name/doc_id
如果您没有 doc_id,您可以向 couchdb 索取:
curl -X GET http://127.0.0.1:5984/_uuids?count=1
CouchDB 不会接受该 JSON 对象。要使用单个服务器请求存储所有数据,请使用:
{
"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
下一步是将导出的 json 文件修改为如下所示(注意 _id):
{
"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
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 更改。
杰里米