FITトランスフォーマー・クラスの例

フィールド入力/出力トランスフォーマー(FIT)クラスの例。

DataStax開発者ブログの「An introduction to DSE Field Transformers」を参照してください。

フィールド入力および出力トランスフォーマー(FIT)クラスの例を以下に示します。

入力トランスフォーマーの例
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);
    }
    }
    }
出力トランスフォーマーの例
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);
    }
    }