Database Reference
In-Depth Information
Do be aware that running this after every write is unwise, because it'll force every write
to wait for the next journal sync. This means that all writes may take up to 100 ms to
return. So use this feature with care. 3
10.1.3
Data imports and exports
If you're migrating an existing system to MongoDB, or if you need to seed the data-
base with information from, say, a data warehouse, then you'll need an efficient
import method. You might also need a good export strategy, since you may have to
export data from MongoDB to external processing jobs. For example, exporting data
to Hadoop for batch processing has become a common practice. 4
There are two ways to import and export data with MongoDB. You can use the
included tools, mongoimport and mongoexport , or you can write a simple program
using one of the drivers. 5
MONGOIMPORT AND MONGOEXPORT
Bundled with MongoDB are two utilities for importing and exporting data:
mongoimport and mongoexport . You can use mongoimport to import JSON , CSV , and
TSV files. This is frequently useful for loading data from relational databases into
MongoDB:
$ mongoimport -d stocks -c values --type csv --headerline stocks.csv
In the example, you import a CSV file called stocks.csv into the values collection of
the stocks database. The --headerline flag indicates that the first line of the CSV
contains the field names. You can see all the import options by running mongoimport
--help .
Use mongoexport to export all of a collection's data to a JSON or CSV file:
$ mongoexport -d stocks -c values -o stocks.csv
This command exports data to the file stocks.csv. As with its counterpart, you can see
the rest of mongoexport 's command options by starting it with the --help flag.
C USTOM IMPORT AND EXPORT SCRIPTS
You're likely to use MongoDB's import and export tools when the data you're dealing
with is relatively flat; once you introduce sub-documents and arrays, the CSV format
becomes awkward because it's not designed to represent nested data. When you need
to export a rich document to CSV or import a CSV to a rich MongoDB document, it
may be easier to build a custom tool instead. You can do this using any of the drivers.
For example, MongoDB users commonly write scripts that connect to a relational
database and then combine the data from two tables into a single collection.
3
Future releases of MongoDB promise finer-grained control over journal syncing. Consult the latest release
notes for details.
4
For this particular use case, an officially supported MongoDB-Hadoop adapter is available at http://
github.com/mongodb/mongo-hadoop .
5
Note that importing and exporting data is distinct from backups, which are covered later in the chapter.
Search WWH ::




Custom Search