Import data into openshift mongoDb

I created a java application in openshift using the mongoDb cartridge. My application works fine, both locally on jboss AS7, and in shutter mode. So far, so good. Now I would like to import csv to mongoDb in the openshift cloud. The command is pretty simple:

mongoimport -d dbName -c collectionName --type csv data.csv --headerline 

This works fine locally and I know how to connect to openhift-shell and remote mongo-db. But my question is: how can I use a locally saved file (data.csv) when running this command in ssh-shell.

I found this on the openshift forum, but I don't know what the tmp directory is and how to use it. I work on windows, so I use Cygwin as a shell replacement.

Thanks for any help

+4
source share
6 answers

The tmp directory is short for /tmp . On Linux, this is a directory that is cleaned up every time you restart your computer, so this is a good place for temporary files.

So you can do something like:

 $ rsync data.csv openshiftUsername@openshiftHostname :/tmp $ ssh openshiftUsername@openshiftHostname $ mongoimport -d dbName -c collectionName --type csv /tmp/data.csv --headerline 
+8
source

This is what I need in October 2014:

mongoimport --host $OPENSHIFT_MONGODB_DB_HOST --port $OPENSHIFT_MONGODB_DB_PORT -u admin -p 123456789 -d dbName -c users /tmp/db.json

Please note that I used json file instead of csv

+4
source

When using Openshift, you must use environment variables to ensure that your values ​​are always correct. Click here to learn more about Openshift Envrionment variables.

SSH to your openshift server, then run (do not forget to change the bold bits in the command according to your values) :

 mongoimport --headerline --type csv \ --host $OPENSHIFT_NOSQL_DB_HOST \ --port $OPENSHIFT_NOSQL_DB_PORT \ --db **your db name** \ --collection **your collection name** \ --username $OPENSHIFT_NOSQL_DB_USERNAME \ --password $OPENSHIFT_NOSQL_DB_PASSWORD \ --file ~/**your app name**/data/**your csv file name** 

Note When importing csv files using mongoimport, data is saved as strings and numbers only . It will not save arrays or objects. If you have arrays or an object to save, you must first convert your csv file to the correct json file, and then the mongoimport json file.

+3
source

I installed RockMongo in my openshift instance to control mongodb. This is a nice user interface, a bit like phpMyAdmin for mysql

+1
source

Users who want to use mongorestore have worked for me:

First, copy your dump using scp to the data directory in the forward mode:

 scp yourfile.bson yourhex@yourappname.rhcloud.com :app-root/data 

rhc ssh to your application and cd to the app-root/data folder.

 mongorestore --host $OPENSHIFT_MONGODB_DB_HOST --port $OPENSHIFT_MONGODB_DB_PORT --username $OPENSHIFT_MONGODB_DB_USERNAME --password $OPENSHIFT_MONGODB_DB_PASSWORD -d yourdb -c yourcollection yourfilename.bson --drop 
+1
source

As with Simon, but I imported .json into the database:

mongoimport --host $OPENSHIFT_MONGODB_DB_HOST -u admin -p 123456 --db dbname --collection grades < grades.json

0
source

Source: https://habr.com/ru/post/1402137/


All Articles