import json file to couch db-

前端 未结 7 1843
猫巷女王i
猫巷女王i 2020-12-14 04:27

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

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

Is there an o

相关标签:
7条回答
  • 2020-12-14 04:35

    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:

    [ {"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.

    Jeremy

    0 讨论(0)
  • 2020-12-14 04:38

    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:

    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):

    {
      "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:

    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

    0 讨论(0)
  • 2020-12-14 04:46

    If you are on Linux, You could write a quick shell script to POST the contents of valid json files to 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.

    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

    0 讨论(0)
  • 2020-12-14 04:46

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

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

    Alternatively, submit a different CouchDB request for each row.

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

    curl -vX POST https://user:pass@127.0.0.1:1234/database \
      -d @- -# -o output -H "Content-Type: application/json" < file.json
    
    0 讨论(0)
  • 2020-12-14 04:49

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

    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).

    The full script is here: :

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

    (name --> jsonDb_to_Couch.py)

    If you download the full repo and:

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

    2. Run make load_dbs

    It would create 4 databases in your local couch installation

    Hope that helps newbies (like me)

    0 讨论(0)
  • 2020-12-14 04:57

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

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

    POST is an alias of lwp-request but POST doesn't seem to work on debian. If you use lwp-request you need to set the method with -m as above.

    The trailing _bulk_docs allows multiple documents to be uploaded at once.

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

    0 讨论(0)
提交回复
热议问题