Synchronize from CSV to LDAP directory

This tutorial explains you can synchronize a CSV file to an LDAP directory. The idea is to use a database source service based on a HSQLDB instance (database on files or in memory).

This HSQLDB instance will be previously populated with data contained in the CSV file.

To run a sample CSV to LDAP connector out of the box, you can follow the quickstart.

Another way to synchronize from CSV to LDAP is to use Executable plugin and Perl wrappers for CSV.

Step 1: Populate the HSQLDB database from CSV

CSV file

First, get a CSV file, or create one for this tutorial. This file will be called tutorial.csv and contains the following sample data:

jdoe;Doe;John;John Doe;

HSQLDB script

LSC provides a wrapper to manage the packaged HSQLDB database, see HSQLDB presentation to get more details.


Start HSQLB with the following command:

$ bin/hsqldb --start

You can then check the status:

$ bin/hsqldb --status
HSQLDB Server is running (PID 7020)

Import CSV data

Load the data with this command:

$ bin/hsqldb --import tutorial.csv 
1 lines imported into table csvdata

Check imported data:

$ bin/hsqldb --show
----  ---  ---------  --------  ----------------
jdoe  Doe  John       John Doe

Step 2: Configure LSC

We just provide here the specific configuration items for this tutorial. For a complete overview of LSC configuration, please read the full documentation.

HSQLDB source connector

First of all, configure your database connection:


Then configure your service by referencing the previous connection (attribute reference of the connection node):

  <connection reference="src-jdbc"/>

We now need to use IBatis to get data from HSQLDB. First, create or update the SQL map configuration:

$ vi etc/sql-map-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
    PUBLIC "-// SQL Map Config 2.0//EN"
  <transactionManager type="JDBC">
    <dataSource type="SIMPLE">
      <property value="${driver}" name="JDBC.Driver" />
      <property value="${url}" name="JDBC.ConnectionURL" />
      <property value="${username}" name="JDBC.Username"/>
      <property value="${password}" name="JDBC.Password"/>
      <property value="15" name="Pool.MaximumActiveConnections"/>
      <property value="15" name="Pool.MaximumIdleConnections"/>
      <property value="1000" name="Pool.MaximumWait"/>
  <sqlMap url="file://${lsc.config}/sql-map-config.d/InetOrgPerson.xml"/>

Then declare SQL queries:

$ vi etc/sql-map-config.d/InetOrgPerson.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMap PUBLIC "-// SQL Map 2.0//EN" "">
<sqlMap namespace="InetOrgPerson">
        <select id="getInetOrgPerson" resultClass="java.util.HashMap" parameterClass="java.util.Map">
                pers."UID" AS uid,
                pers."SN" AS sn,
                pers."GIVENNAME" AS givenname,
                pers."CN" AS cn,
                pers."MAIL" AS mail
                FROM csvdata pers
                WHERE pers."UID" LIKE #uid#
        <select id="getInetOrgPersonList" resultClass="java.util.HashMap">
                SELECT pers."UID" AS uid
                FROM csvdata pers
        <select id="getInetOrgPersonClean" resultClass="java.util.HashMap" parameterClass="java.util.Map">
                pers."UID" AS UID
                FROM csvdata pers
                WHERE pers."UID" LIKE #uid#

LDAP destination connector

We suppose you have a running LDAP server on localhost. If not, you can use the sample LDAP directory from the quickstart.

First of all, configure your ldap connection:

      <username>cn=Directory Manager</username>

Then configure LDAP destination service:

        <connection reference="dst-ldap"/>

Set synchronization rules

As usual, define also how the synchronized objects are going to be identified and how you want to force or leave current attributes:

  <mainIdentifier>"uid=" + srcBean.getDatasetFirstValueById("uid") + ",ou=Sample,dc=lsc-project,dc=org"</mainIdentifier>

Here the rules are quite simple:

  • Create the objectClass attribute (class inetOrgPerson)
  • Create the password with the default value “changethis”
  • Copy all other attributes from source to destination (uid, cn, sn, givenname and mail)

Check configuration

Check your configuration:

$ bin/lsc -v

Launch synchronization

Finally launch the synchronization:

$ bin/lsc -s all -c all

You should see the following result:

avr. 23 22:38:35 - DEBUG - Loading XML configuration from: /home/clement/tmp/lsc-2.0-SNAPSHOT/bin/../etc/lsc.xml
avr. 23 22:38:35 - INFO  - Logging configuration successfully loaded from /home/clement/tmp/lsc-2.0-SNAPSHOT/bin/../etc/logback.xml 
avr. 23 22:38:35 - INFO  - LSC configuration successfully loaded from /home/clement/tmp/lsc-2.0-SNAPSHOT/bin/../etc/
avr. 23 22:38:35 - INFO  - Connecting to LDAP server ldap://localhost:33389/dc=lsc-project,dc=org as cn=Directory Manager
avr. 23 22:38:36 - INFO  - Starting sync for user
avr. 23 22:38:36 - INFO  - # Adding new object uid=jdoe,ou=Sample,dc=lsc-project,dc=org for user
dn: uid=jdoe,ou=Sample,dc=lsc-project,dc=org
changetype: add
uid: jdoe
sn: Doe
cn: John Doe
userPassword: changethis
objectClass: organizationalPerson
objectClass: person
objectClass: inetOrgPerson
objectClass: top

avr. 23 22:38:36 - INFO  - All entries: 1, to modify entries: 1, modified entries: 1, errors: 0
avr. 23 22:38:36 - INFO  - Starting clean for user
avr. 23 22:38:36 - INFO  - All entries: 1, to modify entries: 0, successfully modified entries: 0, errors: 0

For further synchronizations, you will need to reimport fresh data from a CSV file into HSQLDB and launch the connector again.