Importing Data to Backendless
|Top Previous Next|
Importing data into a Backendless backend can be done either with the Backendless console or using a REST API1. The import feature can process the following types of data:
The import feature is available in Backendless console under Manage > Import. There are three sections, each accepting a file with data (the Data Service section accepts more than one file):
The App Settings section accepts a file in the JSON format. The best way to become familiar with the file format is to run Export for an application and see the structure of the settings.json file.
This section accepts one or more files in the JSON or the CSV formats. Each file corresponds to a single table. If the table does not exist, it will be created with the same name as the name of the file. A file may either contain only schema definition (columns and their types) or both schema definition and the objects. Consider a file called Product.csv with the following data:
To import the data into the application click the add file button next to Data Service and locate the file. Notice that the first line in the file contains the names of the columns and there is no additional information about the column types. As a result, when you click the Import button, Backendless console shows the following screen for each CSV file:
The user interface lets you select the data type for each column. If more than one file is imported, a similar form is displayed for each file. Once the types have been selected, click the Finalize Import button. Backendless processes data import as an independent, asynchronous task. Depending on the size of the imported data, it may take some time for the import to complete. Once it is finished, Backendless sends an email with the import status/log to the email address associated with the application.
For the file shown above, the imported data in Backendless will appear in the Data section of the console:
Notice that Backendless added the objectId column with unique values assigned to each record and also added the created and updated columns. It is also possible for your objects/records to use custom IDs. The column name must be objectId and it must contain unique values. For example:
Backendless supports a special format for the header row with column names and data types. The format for defining the data type for a column in the header row is:
where type can be any of the following values:
For example, the following header row declares columns and pre-assigns data types to them:
"objectId(STRING_ID)", "name(STRING)", "price(INT)", "desc(TEXT)","date(DATETIME)","discounted(BOOLEAN)"
When the types are specified, Backendless still shows the second step of the data import - the screen where the developer can adjust the data types for the columns.
Backendless can import related objects and reconstruct relations between them. The system uses a special naming convention for the columns representing relations. There are two types of relations supported by the system: one-to-one and one-to-many.
For one-to-one relations. the column must be defined in the "parent" file - the file containing objects/records which reference other related entities. For instance, suppose there are two files in the import - Order and Customer. The "order" records contain a column referencing a "customer" record. In this case, Order.csv would contain a column with references related objects from Customer.csv. The value of a reference must be objectId of the related record.
The format of the column name defining a relation is significant. It includes column name, name of the file containing related entities and a special marker designating the column as a relation. The general format of the column name is:
Using the same example of Order and Customer, the files would look as shown below:
When importing files with related records, Backendless recognizes relations and displays them accordingly on the second step of the data import - the screen where the column types can be set or adjusted. Notice how the relation displays for the sample data shown above:
Once the import is complete, the Data browser automatically displays all relations as links:
One-to-many relations differ from one-to-one in a way that the relationship column is defined at the child level. For example, consider a scenario where an Order object contains a collection of OrderItem objects. In this case, the relation column must be defined in the OrderItem.csv file. Each OrderItem record contains a value of objectId of the Order record it belongs to.
The format of the column name defining a one-to-many relation consists of column name (in the parent object), name of the file containing parent entities and a special marker designating the column as a relation. The general format of the column name is:
Extending the example of Order and Customer shown above with the OrderItem.csv file:
When importing the files, notice how Backendless recognizes and confirms the relations in the second step of data import:
The Geo Service section of the Import page allows importing the geo points (in .csv file) or geo fences (in .json file). In the import file containing geo points, each row defines one geo point. A single row must have the following structure:
latitude, longitude, list of categories, metadata
Consider the following example of a CSV file:
Once this data is imported to Backendless, the GeoLocation pagevisualizes them as:
The Import user interface provides a way to select a single ZIP archive which contains files for app settings, data service and geo service. The ZIP archive could be created either by the Backendless Export feature or by an export from a Parse application.