Toggle menu

importData

This legacy importer uses ColdFusion scripts. It has been replaced by new functionality in the API Server. See Data Import from CSV for the new documentation.

importData(ctx, tablename, columns, batchsize, csvfile, label, rowfilter, mappings, appendTo, allowAppend, progresscallback, progressrowcount, beforepublishcallback)

Imports data from a CSV file into a data import table. If you are importing data into a table that already exists, it is added to the table as additional rows, with the DI_version number incremented by one. Old versions can be deleted using deleteOldTableData.

It is possible to append data to an existing import. The existing data must have been imported with allowAppend: true. Subsequent imports should set the appendTo parameter, which will set the DI_version to that of the existing data. The latest version is not recorded in the DI_META_tables_v1 table. If allowAppend: false (the default) the latest version number is recorded and attempts to append data will return an error.

When data is imported, a corresponding row is added to the DI_META_versions_v1 table. This table records the table name, version, label, import stat time and import complete time of each data import. The version number increases each time (non appended) data is imported into a table. The importcompleted column in the versions table will be NULL if allowAppend: true.

Parameters

NameTypeDescription
ctxStruct, requiredDatabase details, see above
tablenameString, requiredThe table to import the data into
columnsArray of objects, requiredEach item in the array is an object representing a column. This follows the same format as makeTable
batchsizeInteger, optionalThe number of rows to insert in each database query. Defaults to 50
csvfileString, requiredThe fully qualified path to the CSV file
labelString, optionalA label for this import. Defaults to the csvfile value if not set
rowfilterFunction, optionalThe name of a callback function which decides whether rows should be imported into the table. The function is called before each row is processed. It must return true for the row to be imported. See below
mappingsArray of objects, requiredAn array of mappings. These set which data is imported into which field
mappings[n].csvColInteger, optionalThe index number for the column in the source CSV file. Either csvCol or a formatter must be supplied
mappings[n].tableColString, requiredThe name of the column to insert this data into
mappings[n].nullvalue[any], optionalIf the formatted data value matches this value, then a null will be inserted into the database table
mappings[n].trimBoolean, optionalTrim leading and trailing whitespace from the value. Defaults to true
mappings[n].formattersArray of functions, optionalAn array of callback functions that format a value before it is inserted into the database table. The functions are called in order. Each function must return a formatted version of the data value. See below. Either csvCol or a formatter must be supplied
appendToInteger, optionalThe version number of an open import to append this data to. Open imports must have previously been created with allowAppend set as true
allowAppendBoolean, optionalIf true the imported data will not be published and may have data appended to it in the future (ie future imports will have the same version number as this import). Defaults to false
progresscallbackFunction, optionalA callback function which is told how many rows have been processed. The function must not return a value. It takes two augments: nRead (Integer) the number of rows that have been read from the CSV file; nWritten (Integer) the number of rows that have been written to the database
progressrowcountInteger, optionalThe number of rows after which the progresscallback function is called. Defaults to 250
beforepublishcallbackFunction, optionalA callback function which is called after the data import, but before this version of the import is marked as complete. The function must not return a value. It takes two augments: tablename (String) the name of the table that the data has been imported into; version (Integer) the version number of the import which has just occurred

Row Filters

Row filters give you control over the CSV rows that are imported.

This first example skips the first row in the file (useful if the first row is a header).

<cffunction name="skipheader" returntype="Boolean">
    <cfargument name="row" type="Array" required="yes">
    <cfargument name="rowcount" type="Numeric" required="yes">
    <!--- skip the first row --->
    <cfif ARGUMENTS.rowcount EQ 1>
        <cfreturn false>
    </cfif>
    <cfreturn true>
</cffunction>

This filter skips the first row, then checks the values of NAME and MAGNITUDE, which are variables that correspond to columns in the csv file.

<cffunction name="brightstar" returntype="Boolean">
    <cfargument name="row" type="Array" required="yes">
    <cfargument name="rowcount" type="Numeric" required="yes">
    <!--- skip the first row --->
    <cfif ARGUMENTS.rowcount EQ 1>
        <cfreturn false>
    </cfif>
    <!--- do not import a row if the star is nameless and dim (the higher the number the dimmer the star) --->
    <cfif len(trim(ARGUMENTS.row[NAME])) EQ 0 AND ARGUMENTS.row[MAGNITUDE] GT 8.0>
        <cfreturn false>
    </cfif>
    <cfreturn true>
</cffunction>

Mapping Value Formatters

The formatters included in your mappings manipulate values in a column as they are imported.

This example checks for the value "Tim". If it's found it will be changed to "Tom". If not, the original value is returned.

<cffunction name="myformatter" returntype="Any">
    <cfargument name="row" type="Array" required="yes">
    <cfargument name="val" type="Any" required="yes">
     <cfif ARGUMENTS.val EQ "Tim">
        <cfreturn "Tom">
     </cfif>
    <cfreturn ARGUMENTS.val>
</cffunction>

This example is used on a CSV column called FIRST_NAME. It's value is mapped to a table column called FirstName. The formatter maps the first letter of the FIRST_NAME column to a new table column called Initial.

<cfset csv2table=[
    {"csvCol":FIRST_NAME, "tableCol":"FirstName"},
    {"csvCol":FIRST_NAME, "tableCol":"Initial", "formatters":[getinitial]},
    {"csvCol":LAST_NAME, "tableCol":"LastName"},
    {"csvCol":ADDRESS, "tableCol":"Address"},
    {"csvCol":CITY, "tableCol":"City"},
    {"csvCol":COUNTY, "tableCol":"County"},
    {"csvCol":POSTCODE, "tableCol":"PostCode"}
]>

<cffunction name="getinitial" returntype="Any">
    <cfargument name="row" type="Array" required="yes">
    <cfargument name="val" type="Any" required="yes">
     <cfif len(ARGUMENTS.val) GT 1>
        <cfreturn Left(ARGUMENTS.val,1)>
     </cfif>
    <cfreturn "">
</cffunction>

Returns

NameTypeDescription
versionStringThe version number of the import
labelStringThe label of the import
nReadIntegerThe number of lines read from the CSV
nWrittenintegerThe number of rows written to the database table

Example

This simplified example assumes the database table already exists.

<cfset ds=APPLICATION.datasource>
<cfset dt=APPLICATION.databasetype>
<cfset tablename="timstable">
<cfset csvfile=getDirectoryFromPath(getCurrentTemplatePath()) & "EXAMPLES/dataimport_names.csv">
<!--- get a data importer --->
<cfmodule template="/icm/admin/dataimport/importer_v1.cfm" name="di" datasource=#ds# databasetype=#dt#>
<!--- set some readable names for the csv columns to use later --->
<cfset FIRST_NAME = 1>
<cfset LAST_NAME = 2>
<cfset ADDRESS = 4>
<cfset CITY = 5>
<cfset COUNTY = 6>
<cfset POSTCODE = 7>
<!--- the database table columns --->
<cfset tableColumns=[
    {"name":"FirstName", "type":"VARCHAR", "size":60},
    {"name":"LastName", "type":"VARCHAR", "size":60},
    {"name":"Address", "type":"VARCHAR", "size":240},
    {"name":"City", "type":"VARCHAR", "size":60},
    {"name":"County", "type":"VARCHAR", "size":60},
    {"name":"PostCode", "type":"VARCHAR", "size":12}
]>
<!--- csv to database table mappings --->
<cfset csv2table=[
    {"csvCol":FIRST_NAME, "tableCol":"FirstName"},
    {"csvCol":LAST_NAME, "tableCol":"LastName"},
    {"csvCol":ADDRESS, "tableCol":"Address"},
    {"csvCol":CITY, "tableCol":"City"},
    {"csvCol":COUNTY, "tableCol":"County"},
    {"csvCol":POSTCODE, "tableCol":"PostCode"}
]>
<!--- import some data --->
<cfset imported = di.importData(ctx=#di#,
        tablename=#tablename#,
        columns=#tableColumns#,
        csvfile=#csvfile#,
        mappings=#csv2table#
)>
<cfoutput>...Done. Version:[#imported.version#]</cfoutput><cfflush>

Last modified on September 30, 2022

Share this page

Facebook icon Twitter icon email icon

Print

print icon