8

Is there a way to create dynamic column (as a key/value) names using the values returned from the query throw DIH (DataImportHandler)?

For example:

<entity name="foo" dataSource="my_database" query="select key,value from foo where id=${item.id}">
   <field column="${foo.key}" value="${foo.value}" name="${foo.key}_s"/>
</entity>

??

Ehab Al-Hakawati
  • 982
  • 4
  • 11
  • 32

1 Answers1

11

Use ScriptTransformer -

Example -

Data Config - Add custom field -

<script><![CDATA[
        function addfield(row){
            var fieldName = row.get('key') + "_s"
            row.put(fieldName, row.get('value'));
            return row;
        }
]]></script>

Entity mapping -

<entity name="foo" dataSource="my_database" transformer="script:addfield" query="select key,value from foo where id=${item.id}">
    ......
</entity>
Jayendra
  • 52,349
  • 4
  • 80
  • 90
  • 1
    1 thing to watch out for. If you got fields in your schema named `key` or `value` then alias them in the `foo` entity's query and accordingly change the transformer function to use the aliased names. (I unfortunately had a multi-valued field with the same name as `key` and the values from this query got added to my multi-valued field.) – arun Oct 22 '13 at 22:45