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);
}
}
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;
}
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());
}
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 });
}
}
Aggregations