Search in sources :

Example 1 with Field

use of org.apache.gora.elasticsearch.mapping.Field in project gora by apache.

the class ElasticsearchStore method createSchema.

@Override
public void createSchema() throws GoraException {
    CreateIndexRequest request = new CreateIndexRequest(elasticsearchMapping.getIndexName());
    Map<String, Object> properties = new HashMap<>();
    for (Map.Entry<String, Field> entry : elasticsearchMapping.getFields().entrySet()) {
        Map<String, Object> fieldType = new HashMap<>();
        fieldType.put("type", entry.getValue().getDataType().getType().name().toLowerCase(Locale.ROOT));
        if (entry.getValue().getDataType().getType() == Field.DataType.SCALED_FLOAT) {
            fieldType.put("scaling_factor", entry.getValue().getDataType().getScalingFactor());
        }
        properties.put(entry.getKey(), fieldType);
    }
    // Special field for range query
    properties.put("gora_id", new HashMap<String, Object>() {

        {
            put("type", "keyword");
        }
    });
    Map<String, Object> mapping = new HashMap<>();
    mapping.put("properties", properties);
    request.mapping(mapping);
    try {
        if (!client.indices().exists(new GetIndexRequest(elasticsearchMapping.getIndexName()), RequestOptions.DEFAULT)) {
            client.indices().create(request, RequestOptions.DEFAULT);
        }
    } catch (IOException ex) {
        throw new GoraException(ex);
    }
}
Also used : Field(org.apache.gora.elasticsearch.mapping.Field) GoraException(org.apache.gora.util.GoraException) HashMap(java.util.HashMap) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with Field

use of org.apache.gora.elasticsearch.mapping.Field in project gora by apache.

the class ElasticsearchStore method fromElasticsearchRecord.

/**
 * Deserialize an Elasticsearch Record to an Avro Object as used in Gora generated classes
 * that can safely be written into Avro persistent object.
 *
 * @param avroFieldSchema     schema for the persistent Avro class field
 * @param elasticsearchRecord Elasticsearch Record value to be deserialized
 * @return deserialized Avro Object from the given Elasticsearch Record value
 * @throws GoraException when one of the underlying values cannot be deserialized
 */
private Object fromElasticsearchRecord(Schema avroFieldSchema, Map<String, Object> elasticsearchRecord) throws GoraException {
    Class<?> clazz;
    try {
        clazz = ClassLoadingUtils.loadClass(avroFieldSchema.getFullName());
    } catch (ClassNotFoundException ex) {
        throw new GoraException(ex);
    }
    PersistentBase record = (PersistentBase) new BeanFactoryImpl(keyClass, clazz).newPersistent();
    for (Schema.Field recField : avroFieldSchema.getFields()) {
        Schema innerSchema = recField.schema();
        Field innerDocField = elasticsearchMapping.getFields().getOrDefault(recField.name(), new Field(recField.name(), null));
        record.put(recField.pos(), deserializeFieldValue(recField, innerSchema, elasticsearchRecord.get(innerDocField.getName())));
    }
    return record;
}
Also used : Field(org.apache.gora.elasticsearch.mapping.Field) GoraException(org.apache.gora.util.GoraException) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) Schema(org.apache.avro.Schema) BeanFactoryImpl(org.apache.gora.persistency.impl.BeanFactoryImpl)

Example 3 with Field

use of org.apache.gora.elasticsearch.mapping.Field in project gora by apache.

the class TestElasticsearchStore method testInitialize.

@Test
public void testInitialize() throws GoraException {
    log.info("test method: testInitialize");
    ElasticsearchMapping mapping = ((ElasticsearchStore) employeeStore).getMapping();
    Map<String, Field> fields = new HashMap<String, Field>() {

        {
            put("name", new Field("name", new Field.FieldType(Field.DataType.TEXT)));
            put("dateOfBirth", new Field("dateOfBirth", new Field.FieldType(Field.DataType.LONG)));
            put("ssn", new Field("ssn", new Field.FieldType(Field.DataType.TEXT)));
            put("value", new Field("value", new Field.FieldType(Field.DataType.TEXT)));
            put("salary", new Field("salary", new Field.FieldType(Field.DataType.INTEGER)));
            put("boss", new Field("boss", new Field.FieldType(Field.DataType.OBJECT)));
            put("webpage", new Field("webpage", new Field.FieldType(Field.DataType.OBJECT)));
        }
    };
    Assert.assertEquals("frontier", employeeStore.getSchemaName());
    Assert.assertEquals("frontier", mapping.getIndexName());
    Assert.assertEquals(fields, mapping.getFields());
}
Also used : Field(org.apache.gora.elasticsearch.mapping.Field) ElasticsearchMapping(org.apache.gora.elasticsearch.mapping.ElasticsearchMapping) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with Field

use of org.apache.gora.elasticsearch.mapping.Field in project gora by apache.

the class ElasticsearchStore method put.

@Override
public void put(K key, T obj) throws GoraException {
    if (obj.isDirty()) {
        Schema schemaObj = obj.getSchema();
        List<Schema.Field> fields = schemaObj.getFields();
        Map<String, Object> jsonMap = new HashMap<>();
        for (Schema.Field field : fields) {
            Field mappedField = elasticsearchMapping.getFields().get(field.name());
            if (mappedField != null) {
                Object fieldValue = obj.get(field.pos());
                if (fieldValue != null) {
                    Schema fieldSchema = field.schema();
                    Object serializedObj = serializeFieldValue(fieldSchema, fieldValue);
                    jsonMap.put(mappedField.getName(), serializedObj);
                }
            }
        }
        // Special field for range query
        jsonMap.put("gora_id", key);
        // Prepare the Elasticsearch request
        IndexRequest request = new IndexRequest(elasticsearchMapping.getIndexName()).id((String) key).source(jsonMap);
        try {
            client.index(request, RequestOptions.DEFAULT);
        } catch (IOException ex) {
            throw new GoraException(ex);
        }
    } else {
        LOG.info("Ignored putting object {} in the store as it is neither " + "new, neither dirty.", new Object[] { obj });
    }
}
Also used : Field(org.apache.gora.elasticsearch.mapping.Field) GoraException(org.apache.gora.util.GoraException) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) IOException(java.io.IOException) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest)

Aggregations

Field (org.apache.gora.elasticsearch.mapping.Field)4 HashMap (java.util.HashMap)3 GoraException (org.apache.gora.util.GoraException)3 IOException (java.io.IOException)2 Schema (org.apache.avro.Schema)2 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)2 GetIndexRequest (org.elasticsearch.client.indices.GetIndexRequest)2 Map (java.util.Map)1 ElasticsearchMapping (org.apache.gora.elasticsearch.mapping.ElasticsearchMapping)1 BeanFactoryImpl (org.apache.gora.persistency.impl.BeanFactoryImpl)1 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)1 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)1 IndexRequest (org.elasticsearch.action.index.IndexRequest)1 Test (org.junit.Test)1