import
Description
The import
command imports the contents of a JSON or CSV file, or a
directory of JSON or CSV files, into a Fauna collection. If you
import a directory of source files, each file is imported into a
separate collection.
JSON source files must contain valid JSON, and each JSON object in the file becomes a document in the target collection.
CSV source files must be comma-delimited, and each line in the CSV file becomes a document in the target collection. A CSV file must have a header line containing the field names to be used in the target collection.
If your CSV file has rows which contain fewer fields than the number of
fields in the header line, you can use the --allow-short-rows
option
to permit the import to proceed. Otherwise the import fails with an
error. If you use the --allow-short-rows
option, the documents
imported from short rows do not contain the missing fields.
If your CSV files has empty columns, you can use the
treat-empty-csv-cells-as
option to specify:
-
empty
: The field exists in the imported document as an empty string. -
null
(default): The field does not exist in the imported document.
The target collection can be either an existing Fauna collection or a
new one. If the target collection exists and is not empty, you must use
the --append
option, and the new documents are added to the existing
collection. If you don’t specify a target collection, Fauna Shell
uses the filename of the source file as the name of the target
collection.
The --path
option specifies the path to the source file or directory
of source files, and it is required. If you don’t specify any other
options, Fauna Shell uses the parameters specified in your
fauna-shell
configuration
file.
Floating-point values that end in .0 are converted, by
JavaScript, into integers.
|
Billing considerations
Importing data with the import
command involves billable write and
compute operations. One write operation accrues for each 1kb of data
written, with a minimum of 1kb per imported record, so if you import a file
of 5,000 small records, you accrue 5,000 write operations. If the records
are larger, for example, 5kb each, you accrue 25,000 write operations
(5 each for 5,000 records).
Additionally, importing data into collections with covering indexes causes index updates which incur write operations.
Compute operations accrue at a rate of approximately 1 compute operation for every 20 write operations. The rate may vary depending on several factors, including record size and retries for recoverable errors.
See Billing for more information.
Recommended setup
To ensure the integrity of your data import, it is recommended that each record of your source file should include a unique identifying field. You can create a unique index on that field to ensure that no records are imported more than once, and you can query against the unique field to verify the completeness of your import.
Options
Option | Description | ||
---|---|---|---|
|
Optional - Allow CSV files in which some rows have fewer fields than
the number of fields in the header row. Note that if |
||
|
Optional - Append documents to an existing collection. |
||
|
Optional - Specifies the target collection to be created. Defaults to the filename of the source file. |
||
|
Optional - Specifies a child database in which to create a new target
collection or append to an existing target collection. The parent
database is always the database associated with the |
||
|
Optional - Performs all import operations, including record processing, type conversions, etc., except creating document in Fauna. This allows you to detect issues that might impact an import before writing documents to your collections. |
||
|
Required - Specifies the path to a source file or directory of source files. Note that directories may only contain files, not additional directories.
|
||
|
Optional - The Fauna server domain, that is, the hostname where
Fauna is running. Defaults to |
||
|
Optional - The name of the endpoint to use for the command. |
||
|
Optional - The connection port. Defaults to 8443. |
||
|
Optional - The connection scheme. Must be one of |
||
|
Optional - The secret to use. A secret authenticates your connection to Fauna, and connects you to a specific database. |
||
|
Optional - The connection timeout, an integer number of milliseconds.
When the specified period has elapsed, The default is zero, which means that |
||
|
Optional - Specifies how empty fields in a record should be handled:
|
||
|
Optional - Specify a data type for a column.
You can specify a column in your source file to be imported as one of the following data types:
|
Examples
Import a JSON file
A file named zipcodes.json
contains the following:
{ "zipcode" : "01001", "city" : "AGAWAM", "pop" : 15338, "state" : "MA" }
{ "zipcode" : "01002", "city" : "CUSHMAN", "pop" : 36963, "state" : "MA" }
{ "zipcode" : "01005", "city" : "BARRE", "pop" : 4546, "state" : "MA" }
{ "zipcode" : "01007", "city" : "BELCHERTOWN", "pop" : 10579, "state" : "MA" }
{ "zipcode" : "01008", "city" : "BLANDFORD", "pop" : 1240, "state" : "MA" }
The following terminal command imports zipcodes.json
:
fauna import --path=./zipcodes.json
Database connection established
Starting Import!
› Success: Import from ./zipcodes.json to zipcodes completed. 5 rows/object imported.
In the above command, no --collection
option is specified, so
Fauna Shell creates a new collection called zipcodes
.
Import a JSON file and append to an existing collection
A file named zipcodes2.json
contains the following:
{ "zipcode" : "01010", "city" : "BRIMFIELD", "pop" : 3706, "state" : "MA" }
{ "zipcode" : "01011", "city" : "CHESTER", "pop" : 1688, "state" : "MA" }
{ "zipcode" : "01012", "city" : "CHESTERFIELD", "pop" : 177, "state" : "MA" }
The following terminal command imports zipcodes2.json
and appends the
documents to the existing collection zipcodes
:
fauna import --path=./zipcodes2.json --collection=zipcodes --append
Database connection established
Starting Import!
› Success: Import from ./zipcodes2.json to zipcodes completed. 3 rows/object imported.
Import a JSON file with configuration options
The following terminal command overrides any configuration options in the
fauna-shell
configuration file:
fauna import --path=./zipcodes.json --scheme=https --domain=db.us.fauna.com --port=443 --secret=secret
Database connection established
Starting Import!
› Success: Import from ./zipcodes.json to zipcodes completed. 5 rows/object imported.
Import a CSV file with type conversions
The following CSV document contains three string values:
myDate,myBool,myNumber
"May 3, 2021",true,15338
To convert those string values to other types, you can use the --type
option.
fauna import --path=./myFile.json --type=myDate::date --type=myBool::bool --type=myNumber::number
Database connection established
Starting Import!
Warning: the string 'May 3, 2021' is not valid ISO-8601 nor RFC_2822 date. Making a best-effort translation to 'Mon May 03 2021 00:00:00 GMT-0700 (Pacific Daylight Saving Time)'
› Success: Import from ./myFile.csv to myFile completed. 1 rows/object imported.
The above JSON object becomes the following Fauna document:
{
ref: Ref(Collection("myFile"), "328119865601688064"),
ts: 1649178338530000,
data: {
myDate: Time("2021-05-03T07:00:00Z"),
myBool: true,
myNumber: 15338
}
}
Import a directory of source files
A directory named source_files
contains the following files:
myJSONfile1.json
myJSONfile2.json
myCSVfile1.csv
myCSVfile2.csv
The following command imports four files and creates four new collections:
fauna import --path=./source_files
Database connection established
Starting Import!
› Success: Import from ./source_files/myCSVfile1.csv to myCSVfile1 completed. 8 rows/object imported.
Starting Import!
› Success: Import from ./source_files/myCSVfile2.csv to myCSVfile2 completed. 14 rows/object imported.
Starting Import!
› Success: Import from ./source_files/myJSONfile1.json to myJSONfile1 completed. 10 rows/object imported.
Starting Import!
› Success: Import from ./source_files/myJSONfile2.json to myJSONfile2 completed. 10 rows/object imported.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!