FIT transformer class examples

Field input and output transformer (FIT) class examples.

The DataStax Developer Blog provides an introduction to DSE Field Transformers.

Here are examples of field input and output transformer (FIT) classes.

Input transformer example
package com.datastax.bdp.search.solr.functional;
     
    import java.io.IOException;
    
    import org.apache.commons.codec.binary.Hex;
    import org.apache.commons.lang.StringUtils;
    import org.apache.lucene.document.Document;
    import org.apache.solr.core.SolrCore;
    import org.apache.solr.schema.SchemaField;
    
    import com.datastax.bdp.search.solr.FieldOutputTransformer;
    import org.apache.solr.schema.IndexSchema;
    
    public class BinaryFieldInputTransformer extends FieldInputTransformer
    {
    @Override
    public boolean evaluate(String field)
    {
    return field.equals("binary");
    }
    
    @Override
    public void addFieldToDocument(SolrCore core,
    IndexSchema schema,
    String key,
    Document doc,
    SchemaField fieldInfo,
    String fieldValue,
    DocumentHelper helper)
    throws IOException
    {
    try
    {
    byte[] raw = Hex.decodeHex(fieldValue.toCharArray());
    byte[] decomp = DSP1493Test.decompress(raw);
    String str = new String(decomp, "UTF-8");
    String[] arr = StringUtils.split(str, ",");
    String binary_name = arr[0];
    String binary_type = arr[1];
    String binary_title = arr[2];
    
    SchemaField binaryNameField = core.getSchema().getFieldOrNull("binary_name");
    SchemaField binaryTypeField = core.getSchema().getFieldOrNull("binary_type");
    SchemaField binaryTitleField = core.getSchema().getFieldOrNull("binary_title");
    
    helper.addFieldToDocument(core, core.getSchema(), key, doc, binaryNameField, binary_name);
    helper.addFieldToDocument(core, core.getSchema(), key, doc, binaryTypeField, binary_type);
    helper.addFieldToDocument(core, core.getSchema(), key, doc, binaryTitleField, binary_title);
    }
    catch (Exception ex)
    {
    throw new RuntimeException(ex);
    }
    }
    }
Output transformer example
package com.datastax.bdp.search.solr.functional;
    
    import java.io.IOException;
    import org.apache.commons.lang.StringUtils;
    import org.apache.lucene.index.FieldInfo;
    import org.apache.lucene.index.StoredFieldVisitor;
    import com.datastax.bdp.search.solr.FieldOutputTransformer;
    
    public class BinaryFieldOutputTransformer extends FieldOutputTransformer
    {
    @Override
    public void binaryField(FieldInfo fieldInfo, byte[] value,
    StoredFieldVisitor visitor, DocumentHelper helper) throws IOException
    {
    byte[] bytes = DSP1493Test.decompress(value);
    String str = new String(bytes, "UTF-8");
    String[] arr = StringUtils.split(str, ",");
    String binary_name = arr[0];
    String binary_type = arr[1];
    String binary_title = arr[2];
    
    FieldInfo binary_name_fi = helper.getFieldInfo("binary_name");
    FieldInfo binary_type_fi = helper.getFieldInfo("binary_type");
    FieldInfo binary_title_fi = helper.getFieldInfo("binary_title");
    
    visitor.stringField(binary_name_fi, binary_name);
    visitor.stringField(binary_type_fi, binary_type);
    visitor.stringField(binary_title_fi, binary_title);
    }
    }