将 json 文件导入沙发 db-

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/790757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:14:38  来源:igfitidea点击:

import json file to couch db-

jsoncouchdb

提问by

If I have a json file that looks something like this:

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

{"name":"bob","hi":"hello"}
{"name":"hello","hi":"bye"}

Is there an option to import this into couchdb?

是否可以将其导入到 couchdb 中?

回答by thelastshadow

Starting from @Millhouse answer but with multiple docs in my file I used

从@Millhouse 答案开始,但我使用的文件中有多个文档

cat myFile.json | lwp-request -m POST -sS "http://localhost/dbname/_bulk_docs" -c "application/json" 

POSTis an alias of lwp-requestbut POSTdoesn't seem to work on debian. If you use lwp-requestyou need to set the method with -mas above.

POST是 的别名,lwp-requestPOST似乎不适用于 debian。如果您使用lwp-request,则需要使用上述方法设置方法-m

The trailing _bulk_docsallows multiple documents to be uploaded at once.

尾随_bulk_docs允许一次上传多个文档。

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

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

回答by Millhouse

If you are on Linux, You could write a quick shell script to POST the contents of valid json files to Couch.

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

To test couch I did something like this:

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

cat myFile.json | POST -sS "http://myDB.couchone.com/testDB" -c "application/json"

myFile.json has the json contents I wanted to import into the database.

myFile.json 包含我想导入到数据库中的 json 内容。

Another alternative, if you don't like command line or aren't using Linux, and prefer a gui, you can use a tool like RESTClient

另一种选择,如果您不喜欢命令行或不使用 Linux,并且更喜欢 gui,则可以使用RESTClient 之类的工具

回答by user470370

Yes, this is not valid JSON ...

是的,这不是有效的 JSON ...

To import JSON-Objects I use curl (http://curl.haxx.se):

要导入 JSON 对象,我使用 curl (http://curl.haxx.se):

curl -X PUT -d @my.json http://admin:[email protected]:5984/db_name/doc_id

where my.json is a file the JSON-Object is in. Of course you can put your JSON-Object directly into couchdb (without a file) as well:

其中 my.json 是 JSON-Object 所在的文件。当然,您也可以将 JSON-Object 直接放入 couchdb(没有文件):

curl -X PUT -d '{"name":"bob","hi":"hello"}' http://admin:[email protected]:5984/db_name/doc_id

If you do not have a doc_id, you can ask couchdb for it:

如果您没有 doc_id,您可以向 couchdb 索取:

curl -X GET http://127.0.0.1:5984/_uuids?count=1

回答by Tanmay

Probably a bit late to answer. But If you can use Python than you can use the couchdbmodule to do so:

回答可能有点晚了。但是如果你可以使用 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)

I created the python script to do that(As I could not find one on Internet).

我创建了 python 脚本来做到这一点(因为我在互联网上找不到)。

The full script is here: :

完整的脚本在这里::

http://bitbucket.org/tdatta/tools/src/

http://bitbucket.org/tdatta/tools/src/

(name --> jsonDb_to_Couch.py)

(名称 --> jsonDb_to_Couch.py​​)

If you download the full repo and:

如果您下载完整的 repo 并且:

  1. Text replace all the "_id" in json files to "id"

  2. Run make load_dbs

  1. 文本将 json 文件中的所有“_id”替换为“id”

  2. 运行 make load_dbs

It would create 4 databases in your local couch installation

它将在您的本地沙发安装中创建 4 个数据库

Hope that helps newbies (like me)

希望对新手有帮助(比如我)

回答by Dorian Puerta

It's not my solution but I found this to solve my issue:

这不是我的解决方案,但我发现这可以解决我的问题:

A simple way of exporting a CouchDB database to a file, is by running the following Curl command in the terminal window:

将 CouchDB 数据库导出到文件的一种简单方法是在终端窗口中运行以下 Curl 命令:

curl -X GET http://127.0.0.1:5984/[mydatabase]/_all_docs\?include_docs\=true > /Users/[username]/Desktop/db.json

Next step is to modify the exported json file to look like something like the below (note the _id):

下一步是将导出的 json 文件修改为如下所示(注意 _id):

{
  "docs": [
      {"_id": "0", "integer": 0, "string": "0"},
      {"_id": "1", "integer": 1, "string": "1"},
      {"_id": "2", "integer": 2, "string": "2"}
  ]
}

Main bit you need to look at is adding the documents in the “docs” code block. Once this is done you can run the following Curl command to import the data to a CouchDB database:

您需要查看的主要部分是在“docs”代码块中添加文档。完成此操作后,您可以运行以下 Curl 命令将数据导入 CouchDB 数据库:

curl -d @db.json -H "Content-type: application/json" -X POST http://127.0.0.1:5984/[mydatabase]/_bulk_docs

Duplicating a database If you want to duplicate a database from one server to another. Run the following command:

复制数据库 如果要将数据库从一台服务器复制到另一台服务器。运行以下命令:

curl -H 'Content-Type: application/json' -X POST http://localhost:5984/_replicate -d ' {"source": "http://example.com:5984/dbname/", "target": "http://localhost@:5984/dbname/"}'

Original Post: http://www.greenacorn-websolutions.com/couchdb/export-import-a-database-with-couchdb.php

原帖:http: //www.greenacorn-websolutions.com/couchdb/export-import-a-database-with-couchdb.php

回答by Matthew Flaschen

That JSON object will not be accepted by CouchDB. To store all the data with a single server request use:

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

{
  "people": 
   [
      {
        "name":"bob",
        "hi":"hello"
      },
      { 
        "name":"hello",
        "hi":"bye"
      }
   ]
}

Alternatively, submit a different CouchDB request for each row.

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

Import the file into CouchDB from the command-line using cURL:

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

curl -vX POST https://user:[email protected]:1234/database \
  -d @- -# -o output -H "Content-Type: application/json" < file.json

回答by Jeremy Wall

http://github.com/zaphar/db-couchdb-schema/tree/master

http://github.com/zaphar/db-couchdb-schema/tree/master

My DB::CouchDB::Schema module has a script to help with loading a series of documents into a CouchDB Database. The couch_schema_tool.pl script accepts a file as an argument and loads all the documents in that file into the database. Just put each document into an array like so:

我的 DB::CouchDB::Schema 模块有一个脚本来帮助将一系列文档加载到 CouchDB 数据库中。couch_schema_tool.pl 脚本接受一个文件作为参数并将该文件中的所有文档加载到数据库中。只需将每个文档放入一个数组中,如下所示:

[ {"name":"bob","hi":"hello"}, {"name":"hello","hi":"bye"} ]

[ {"name":"bob","hi":"hello"}, {"name":"hello","hi":"bye"}]

It will load them into the database for you. Small caveat though I haven't tested my latest code against CouchDB's latest so if you use it and it breaks then let me know. I probably have to change something to fit the new API changes.

它将为您将它们加载到数据库中。尽管我还没有针对 CouchDB 的最新代码测试我的最新代码,但有一点需要注意,所以如果您使用它并且它损坏了,请告诉我。我可能必须更改一些内容以适应新的 API 更改。

Jeremy

杰里米