You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by Apache Wiki <wi...@apache.org> on 2008/07/05 05:31:16 UTC

[Solr Wiki] Update of "DataImportHandler" by NoblePaul

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Solr Wiki" for change notification.

The following page has been changed by NoblePaul:
http://wiki.apache.org/solr/DataImportHandler

------------------------------------------------------------------------------
  
  === A Simple TrimTransformer ===
  {{{
+ package foo;
  public class TrimTransformer	{
  	public Object transformRow(Map<String, Object> row)	{
  		String artist = row.get("artist");
@@ -523, +524 @@

  }}}
  No need to extend any interface. Just write any class which has a method named transformRow with the above signature and DataImportHandler will instantiate it and call the transformRow method using reflection. You will specify it in your data-config.xml as follows:
  {{{
- <entity name="artist" query="..." transformer="TrimTransformer">
+ <entity name="artist" query="..." transformer="foo.TrimTransformer">
  	<field column="artistName" />
  </entity>
  }}}
@@ -531, +532 @@

  === A General TrimTransformer ===
  Suppose you want to write a general !TrimTransformer without hardcoding the column on which it needs to operate. Now we'd need to have a flag on the field in data-config.xml to indicate that the !TrimTransformer should apply itself on this field.
  {{{
- <entity name="artist" query="..." transformer="TrimTransformer">
+ <entity name="artist" query="..." transformer="foo.TrimTransformer">
  	<field column="artistName" trim="true" />
  </entity>
  }}}
  Now you'll need to extend the [#transformer Transformer] interface and use the API methods in Context to get the list of fields in the entity and get attributes of the fields to detect if the flag is set.
  {{{
+ package foo;
- public class TrimTransformer extends Transformer	{
+ public class TrimTransformer implements Transformer	{
  
  	public Map<String, Object> transformRow(Map<String, Object> row, Context context) {
  		List<Map<String, String>> fields = context.getAllEntityFields();
@@ -545, +547 @@

  		for (Map<String, String> field : fields) {
  			// Check if this field has trim="true" specified in the data-config.xml
  			String trim = field.get("trim");
- 			if (Boolean.parseBoolean(trim))	{
+ 			if ("true".equals(trim))	{
  				// Apply trim on this field
  				String columnName = field.get("column");
  				// Get this field's value from the current row
@@ -561, +563 @@

  
  }
  }}}
- If the field is multi-valued, then it returns a List instead of a single object and would need to handled appropriately. You'll need to add the jar for DataImportHandler to your project as a dependency to use the Transformer and Context interfaces.
+ If the field is multi-valued, then the value returned is a List instead of a single object and would need to handl appropriately. You'll need to add the jar for !DataImportHandler to your project as a dependency to use the Transformer and Context interfaces.
  
  [[Anchor(entityprocessor)]]
  == EntityProcessor ==