C3 AI Documentation Home

Import and Export Data

Use Export, a BatchJob provided by the C3 Agentic AI Platform, to export data to a file. Before exporting, you can perform some action on the Type instance that holds the data. You can export data as JSON, XML, or CSV. To load the exported files back into an application, use Import.

To use Export, start by specifying the details in an instance of BatchExportSpec. You can run the following examples on the windTurbine application included with the C3 Agentic AI Platform.

To use these examples as a tutorial, create an application with windTurbine as a dependency. Generate demo data by running WindTurbineDataGenerator.createDemoData(), then follow the examples as written.

Examples

The following examples show common uses of Export.

Export all the Type instance data

JavaScript
var exportSpec = BatchExportSpec.make({
   targetType: WindTurbine,
   numFiles: 100,
   fileUrlOrEncodedPathPrefix: "TestBatchExportWindTurbine"
});
var job = Export.startExport(exportSpec);

// Check job status
job.status();
// View File URLs when job is complete
job.get().fileList.urls

If job.get().fileList.urls returns an error or an empty array, verify that the exported Type has data. Then check the Export job run for errors.

To view the content in the files, use the following code example:

JavaScript
// Change index to view other files
var index = 0;
var urls = job.get().fileList.urls;
// Make a File, then read content from the file
// Second parameter in readString() is end character to limit content
// Otherwise, all text is read into memory (proceed with caution)
var content = FileSystem.makeFile(urls[index]).readString(0, 1000);

Export selected fields of a Type instance into a CSV file

When exporting data to a CSV, ensure that you provide contentType and csvHeader. If you do not provide csvHeader, the export job fails.

JavaScript
var exportSpec = BatchExportSpec.make({
   targetType: WindTurbine,
   numFiles: 100,
   fileUrlOrEncodedPathPrefix: "TestCsvExportWindTurbine",
   contentType: "csv",
   csvHeader: "id, location, manufacturer.id", // include fields to export
   filter: "contains(id, 'TURBINE')"
});
var job = Export.startExport(exportSpec);

// Check job status
job.status();
// View File URLs when job is complete
job.get().fileList.urls

For nested fields, de-nest them with dot notation in the csvHeader field. For example, to export certain fields from Meta such as created and createdBy, define the BatchExportSpec as shown:

JavaScript
var exportSpec = BatchExportSpec.make({
   targetType: "WindTurbine",
   numFiles: 100,
   fileUrlOrEncodedPathPrefix: "TestCsvExportWindTurbine",
   contentType: "csv",
   csvHeader: "id, meta.created, meta.createdBy", // include fields to export
   filter: "contains(id, 'TURBINE')"
});

If you only include meta in the csvHeader, the resulting field in the export is undefined.

Export results of evalMetrics as JSON

JavaScript
var expressions = ["GeneratorRotationSpeedDiff", "GearOilTemperatureDiff"];
var start = "2022-01-01";
var end = "2022-05-01";
var interval = "DAY";
var filter = "id == 'TURBINE-1'";
var evalSpec = EvalMetricsSpec.make({
   expressions: expressions,
   start: start,
   end: end,
   interval: interval
});
var actionParams = {"spec": evalSpec};
var exportSpec = BatchExportSpec.make({
   targetAction: PartiallyAppliedAction.make({typeName: "WindTurbine", actionName: "evalMetrics", args: actionParams}),
   filter: filter,
   numFiles: 100,
   contentType: "json",
   fileUrlOrEncodedPathPrefix: "TestBatchExportEvalMetrics"
});
var job = Export.startExport(exportSpec);

// Check job status
job.status();
// View File URLs when job is complete
job.get().fileList.urls

Specify BatchExportSpec for export

The following table describes a few key fields of BatchExportSpec for Export. For a full list of fields and their descriptions, run the command c3ShowType(BatchExportSpec) from Console.

Field in BatchExportSpecData typeDescription
targetTypeTypeType to export. If you specify the targetAction field, do not use this field.
targetActionPartiallyAppliedActionContains the target Type, the action name and the arguments for the action. The export serializes the output to the default filesystem. You cannot specify a filter in the args object. Provide the filter in BatchExportSpec.
fileUrlOrEncodedPathPrefixstringPrefix for exported files. You should provide either this field or the field fileList. If you provide fileList, then this field is overridden. If you do not provide fileList or this field, the job fails with an error. The field fileUrlOrEncodedPathPrefix is a unique identifier of where the files are persisted in the default filesystem. To delete any existing files at the fileUrlOrEncodedPathPrefix, set deleteExisting to true. For more details, see the "Specify fileUrlOrEncodedPathPrefix" section of this topic.
fileListFileListList of files to which the objects are exported. If you do not provide this field, the list of files is generated from fileUrlOrEncodedPathPrefix.
numFilesintNumber of files to export the data to. The numFiles is also the number of batch jobs to spawn because one batch job writes to one file. Specifying numFiles breaks up the data almost uniformly. For example, if you have 10 objects and specify numFiles as 10, the export does not guarantee 1 object per file. The export might write 1 object per file in most of the files, 2 objects in some, and 3 objects in another file.
numObjPerFileintNumber of IDs to process in one batch (which results in one file). You should provide either numFiles or numObjPerFile but not both, because each of the two parameters determines the other. If both are provided and the parameters are impossible to satisfy, the job throws an error.
contentTypestringSerialization format and content-type of the files to export (Default: "json").
contentEncodingstringContent-encoding to use while exporting files (used for compression).
deleteExistingbooleanIf set to "true", the export deletes the file with the same fileUrlOrEncodedPathPrefix. Defaults to true. Deprecated in 8.12: use failIfUrlNotEmpty instead.
filterstringFilter expression.
orderstringOrder expression.
limitintLimit the total number of objects to export.
csvHeaderstringHeader when exporting data as CSV. Required when contentType is "csv".
xmlIncludestringInclude spec when exporting data as XML.
jsonIncludestringInclude spec when exporting data as JSON.

Export job instance

The startExport() returns a job instance on which you can call a BatchJob method; for example, setMaxConcurrency(100):

JavaScript
var job =  Export.startExport(exportSpec);
job.setMaxConcurrency(100); // and other BatchJob functions

Import

Use Import to import data into the C3 Agentic AI Platform by reading files that Export wrote. The Import Type deserializes the files and then calls the callback action specified in the Import. Import is also a BatchJob.

To use Import, start by specifying the details in an instance of BatchImportSpec. See the following examples.

Import data of a Type instance

The following code works for any export file type (JSON, CSV, XML) that exists at the fileUrlOrEncodedPathPrefix specified.

JavaScript
var importSpec = BatchImportSpec.make({
   targetType: WindTurbine,
   fileUrlOrEncodedPathPrefix: "TestBatchExportWindTurbine"
});
var job = Import.startImport(importSpec);

// Check job status
job.status()

After the job completes, you can fetch on the targetType to ensure the data was imported correctly.

Specify BatchImportSpec for import

The following table describes a few key fields of BatchImportSpec for Import. For a full list of fields and their descriptions, run the command c3ShowType(BatchImportSpec) from Console.

Field in BatchImportSpecData typeDescription
targetTypeTypeType instance that receives the imported data. If you specify targetAction, do not use this field.
targetActionPartiallyAppliedActionAction that is called with the deserialized objects. The action is called just with the imported data and ignores the provided arguments. If you provide targetType, do not use this field.
fileUrlOrEncodedPathPrefixstringPath to use to determine the files to import. If the provided path is not a URL, then it is treated as a file prefix, and the import reads every file that matches the prefix. The example uses this field to import the files of one export. To import only specific files and the objects in them, provide fileList instead.
fileListFileListList of files from which the objects are imported.

Import job instance

The startImport() returns a job instance on which you can call a BatchJob method; for example, setMaxConcurrency(100):

JavaScript
var job =  Import.startImport(importSpec);
job.setMaxConcurrency(100); // and other BatchJob functions

Specify fileUrlOrEncodedPathPrefix

The parameter fileUrlOrEncodedPathPrefix controls the bucket and the URL to export and import data. The parameter accepts either a fully qualified URL or a path in the default file system.

If fileUrlOrEncodedPathPrefix is not a URL, Export and Import treat it as a prefix for a file. Export exports to, and Import imports from, the following path of the default file system:

JavaScript
// Root URL for default file system
var rootUrl = FileSystem.rootUrl();

// Format of file name
"<rootUrl>/<fileUrlOrEncodedPathPrefix>/<fileUrlOrEncodedPathPrefix>-<RANDOM_HASH>-<FILE_NUMBER>.<extension>"

The following are a few fileUrlOrEncodedPathPrefix examples with Amazon S3 as the default file system:

sampleTest

The path used to export to or import from is:

Text
s3://<defaultBucketName>/<ENV_NAME>/<APP_NAME>/fs/sampleTest/sampleTest-<RANDOM_HASH>-<FILE_NUMBER>.<extension>

For example, a sample file could be:

Text
s3://c3--platform/testenv/windturbine/fs/sampleTest/sampleTest-PGDMPB0CMS-0.json

sampleTest/somePrefix

The path used to export to or import from is:

Text
s3://<defaultBucketName>/<ENV_NAME>/<APP_NAME>/fs/sampleTest/somePrefix-<RANDOM_HASH>-<FILE_NUMBER>.<extension>

For example, a sample file could be:

Text
s3://c3--platform/testenv/windturbine/fs/sampleTest/somePrefix-PGDMPB0CMS-0.json

s3://c3--platform/testenv/windturbine/fs/sampleFolder/

This path puts all files in the folder it names, with no file name prefix. For example, a sample file could be:

Text
s3://c3--platform/testenv/windturbine/fs/sampleFolder/PGDMPB0CMS-0.json

If the path has no / separator at the end, Export and Import treat its last part as a file prefix, not a folder. See the following example.

s3://c3--platform/testenv/windturbine/fs/sampleFolder/samplePrefix

This path puts all the files in the folder sampleFolder, and all files have the prefix samplePrefix. For example, a sample file could be:

Text
s3://c3--platform/testenv/windturbine/fs/sampleFolder/samplePrefix-PGDMPB0CMS-0.json

See also

Was this page helpful?