0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/tools/importJSON.cpp

117 lines
3.0 KiB
C++
Raw Normal View History

2009-01-29 19:01:45 +01:00
// importJSON.cpp
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include "client/dbclient.h"
#include "db/json.h"
2009-09-24 17:02:00 +02:00
#include "tool.h"
2009-01-29 19:01:45 +01:00
#include <fstream>
#include <iostream>
#include <boost/program_options.hpp>
using namespace mongo;
namespace po = boost::program_options;
class ImportJSON : public Tool {
public:
ImportJSON() : Tool( "importjson" ){
add_options()
2009-06-25 22:45:48 +02:00
("file",po::value<string>() , "file to import from; if not specified stdin is used" )
2009-01-29 19:01:45 +01:00
("drop", "drop collection first " )
;
addPositionArg( "file" , 1 );
}
2009-02-11 19:52:28 +01:00
2009-01-29 19:01:45 +01:00
int run(){
string filename = getParam( "file" );
long long fileSize = -1;
2009-02-11 19:52:28 +01:00
2009-01-29 19:51:42 +01:00
istream * in = &cin;
2009-01-29 19:01:45 +01:00
ifstream file( filename.c_str() , ios_base::in | ios_base::binary);
2009-02-11 19:52:28 +01:00
2009-06-25 22:45:48 +02:00
if ( filename.size() > 0 && filename != "-" ){
2009-01-29 19:51:42 +01:00
in = &file;
fileSize = file_size( filename );
2009-01-29 19:51:42 +01:00
}
2009-02-11 19:52:28 +01:00
string ns;
try {
ns = getNS();
} catch (...) {
printHelp(cerr);
return -1;
}
2009-08-12 22:31:22 +02:00
auth();
2009-01-29 19:01:45 +01:00
if ( hasParam( "drop" ) ){
cout << "dropping: " << ns << endl;
conn().dropCollection( ns.c_str() );
2009-01-29 19:01:45 +01:00
}
2009-02-11 19:52:28 +01:00
2009-01-29 19:51:42 +01:00
int num = 0;
time_t start = time(0);
2009-02-11 19:52:28 +01:00
ProgressMeter pm( fileSize );
2009-08-26 18:22:48 +02:00
const int BUF_SIZE = 1024 * 1024 * 4;
char line[ (1024 * 1024 * 4) + 128];
2009-01-29 19:51:42 +01:00
while ( *in ){
in->getline( line , BUF_SIZE );
char * buf = line;
while( isspace( buf[0] ) ) buf++;
2009-01-29 19:51:42 +01:00
int len = strlen( buf );
2009-01-29 19:51:42 +01:00
if ( ! len )
continue;
2009-08-26 18:22:48 +02:00
if ( in->rdstate() == ios_base::eofbit )
break;
2009-08-26 18:22:48 +02:00
assert( in->rdstate() == 0 );
2009-01-29 19:51:42 +01:00
2009-01-29 19:01:45 +01:00
try {
BSONObj o = fromjson( buf );
conn().insert( ns.c_str() , o );
2009-01-29 19:01:45 +01:00
}
catch ( MsgAssertionException& ma ){
2009-01-29 19:01:45 +01:00
cout << "exception:" << ma.toString() << endl;
cout << buf << endl;
2009-01-29 19:51:42 +01:00
}
2009-02-11 19:52:28 +01:00
num++;
if ( pm.hit( len + 1 ) ){
cout << "\t\t\t" << num << "\t" << ( num / ( time(0) - start ) ) << "/second" << endl;
2009-01-29 19:01:45 +01:00
}
}
2009-02-11 19:52:28 +01:00
2009-01-29 19:01:45 +01:00
return 0;
}
};
int main( int argc , char ** argv ) {
ImportJSON import;
return import.main( argc , argv );
}