Overview Of Impex


Impex stands for Import and export

As the name suggests, Impex in hybris is used for importing data from CSV file / impex file to hybris system and exporting data from hybris system to CSV file.

Hybris has come up with an extension called “Impex” inside platform/ext folder which helps in Inserting,updating,deleting and exporting data.

Impex import means always data flows into Hybris system.

Impex export means always data flows out of Hybris system

Why do we need to import ?

We need to import data multiple times into Hybris system.

As we all know that any ecommerce site will have a data like products,customer,regions,countries,promotions etc…

All this data has to be inserted into Hybris system to display it on the site.

So to insert this kind of data into hybris system, we go for Impex Import.

We can insert data either at the run time or during initialization or during update process.

Why do we need to export?

Whenever we need to take the backup of data, we have to export the data into csv files so that later it can be imported into Hybris system.

If we are doing initialization,it is recommended to take data backup using Impex Export.

Once the initialization is done, we can import the data back to hybris system.

Impex jargons

1) Header

2) Value lines

3) Macros

4) Comments

Header

Header in impex will have below structure

  1. Mode itemType[modifier=value];attribute1[modifier=value];attribute2[modifier=value];attribute3[modifier=value]
Mode itemType[modifier=value];attribute1[modifier=value];attribute2[modifier=value];attribute3[modifier=value]


We can see below things in header


Mode

It specifies what operation we are going to perform on the values(Insert,update,remove)

There are 4 mode types possible in Header

1) INSERT

2) UPDATE

3) INSERT_UPDATE

4) REMOVE

1) INSERT :

This Mode is used to insert a new record into Database.
If the record already exists it will throw an exception.

2) UPDATE :

This Mode is used to update the existing record.
If the record does not exist then exception will be thrown.

3) INSERT_UPDATE :

This Mode is the most preferred one for insertion or update.

This header will insert the record if its new one and update the record if it is already exists.

So there will be no exception if we use this mode.

4) REMOVE :

This mode is used to remove the existing records.

We need to specify one unique attribute to remove the record.

Type

specifies the item type on which the Mode is applied (Example:Product,Category,Customer etc )

Basically it impacts the table associated with this Type.

Attributes

It defines the attributes of the item type which are defined in items.xml file.

Each attribute specified will get the values affected on corresponding columns in DB.

Modifier

It is used to specify the additional information for the attributes like specifying attribute uniqueness using unique=true.

We can specify multiple modifers in 2 ways

1) Comma separated modifiers within single bracket [] as below

  1. attribute[modifier1=value1,modifier2=value2,modifier3=value3];
attribute[modifier1=value1,modifier2=value2,modifier3=value3];

Single bracket has all the modifiers separated by comma.

2) each modifier with separate bracket [] as below

  1. ;attribute[modifier1=value1][modifier2=value2][modifier3=value3];
;attribute[modifier1=value1][modifier2=value2][modifier3=value3];

Separate bracket for each modifier

Example:

  1. INSERT_UPDATE category;code[unique=true];name[lang=de];name[lang=en];$supercategories;$thumbnail;description[lang=de];order
INSERT_UPDATE category;code[unique=true];name[lang=de];name[lang=en];$supercategories;$thumbnail;description[lang=de];order

In the above header line,

INSERT_UPDATE is the mode

Category is the item type

Code,name etc are the attributes

[unique=true] is the modifier

Note:

This header is applicable for all the value lines till the next header appears in the file.

One file can have any number of headers.

Value lines in Impex

There could be one or more value lines in the file.

Each value line represents one row of data in table.

Each field in the value line is separated by semicolon and represents the data into one column.

Example:

Field1;field2;field3

Space is also considered as value of the field.

Each line should have the same number of fields.

If field contains any line breaks or quotes, then enclose such fields in double quotes.

Macros

Macros are basically used to define the value at one place and use the macro in all the places.

It helps to eliminate writing same lines again and again.
Macros starts with “$” and they are referenced by $macroName

Example:

  1. $catalog=catalog(id)
$catalog=catalog(id)

Defined a macro called “catalog” and can be referenced as $catalog wherever required.

  1. $catalogVersion=catalogVersion($catalog,version)
$catalogVersion=catalogVersion($catalog,version)

Defined a macro called “catalogVersion” and can be referenced as $catalogVersion wherever required.

  1. INSERT Product; code; $catalogVersion
INSERT Product; code; $catalogVersion

Now the macros $catalogVersion and $catalog are replaced by their actual value as below

  1. catalogVersion(catalog(id),version)
catalogVersion(catalog(id),version)

If we don’t use macros then we have to write above line in all the places wherever required.

Comments

Comments in impex file starts with # symbol.

Example:

  1. #Inserting product details
  2. INSERT_UPDATE Product;code[unique=true]; varianttype(code);name[lang=en];
#Inserting product details
INSERT_UPDATE Product;code[unique=true]; varianttype(code);name[lang=en];

In the above lines, we have one comment line starts with # which will be ignored while importing.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on