Search in sources :

Example 46 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class ElasticsearchStore method initialize.

@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException {
    try {
        LOG.debug("Initializing Elasticsearch store");
        ElasticsearchParameters parameters = ElasticsearchParameters.load(properties, getConf());
        super.initialize(keyClass, persistentClass, properties);
        ElasticsearchMappingBuilder<K, T> builder = new ElasticsearchMappingBuilder<>(this);
        InputStream mappingStream;
        if (properties.containsKey(XML_MAPPING_DEFINITION)) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("{} = {}", XML_MAPPING_DEFINITION, properties.getProperty(XML_MAPPING_DEFINITION));
            }
            mappingStream = org.apache.commons.io.IOUtils.toInputStream(properties.getProperty(XML_MAPPING_DEFINITION), (Charset) null);
        } else {
            mappingStream = getClass().getClassLoader().getResourceAsStream(properties.getProperty(PARSE_MAPPING_FILE_KEY, DEFAULT_MAPPING_FILE));
        }
        String xsdValidation = properties.getProperty(XSD_VALIDATION, "false");
        builder.readMappingFile(mappingStream, Boolean.parseBoolean(xsdValidation));
        elasticsearchMapping = builder.getElasticsearchMapping();
        client = createClient(parameters);
        LOG.info("Elasticsearch store was successfully initialized.");
    } catch (Exception ex) {
        LOG.error("Error while initializing Elasticsearch store", ex);
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) ElasticsearchParameters(org.apache.gora.elasticsearch.utils.ElasticsearchParameters) ElasticsearchMappingBuilder(org.apache.gora.elasticsearch.mapping.ElasticsearchMappingBuilder) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 47 with GoraException

use of org.apache.gora.util.GoraException 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 48 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class ElasticsearchStore method get.

@Override
public T get(K key, String[] fields) throws GoraException {
    String[] requestedFields = getFieldsToQuery(fields);
    List<String> documentFields = new ArrayList<>();
    for (String requestedField : requestedFields) {
        documentFields.add(elasticsearchMapping.getFields().get(requestedField).getName());
    }
    try {
        // Prepare the Elasticsearch request
        GetRequest getRequest = new GetRequest(elasticsearchMapping.getIndexName(), (String) key);
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        if (getResponse.isExists()) {
            Map<String, Object> sourceMap = getResponse.getSourceAsMap();
            // Map of field's name and its value from the Document
            Map<String, Object> fieldsAndValues = new HashMap<>();
            for (String field : documentFields) {
                fieldsAndValues.put(field, sourceMap.get(field));
            }
            // Build the corresponding persistent
            return newInstance(fieldsAndValues, requestedFields);
        } else {
            return null;
        }
    } catch (IOException ex) {
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) HashMap(java.util.HashMap) GetRequest(org.elasticsearch.action.get.GetRequest) ArrayList(java.util.ArrayList) IOException(java.io.IOException) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 49 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class ElasticsearchStoreMetadataAnalyzer method getTablesNames.

@Override
public List<String> getTablesNames() throws GoraException {
    GetIndexRequest request = new GetIndexRequest("*");
    GetIndexResponse response;
    try {
        response = elasticsearchClient.indices().get(request, RequestOptions.DEFAULT);
    } catch (IOException ex) {
        throw new GoraException(ex);
    }
    if (response == null) {
        LOG.error("Could not find indices.");
        throw new GoraException("Could not find indices.");
    }
    return Arrays.asList(response.getIndices());
}
Also used : GoraException(org.apache.gora.util.GoraException) GetIndexResponse(org.elasticsearch.client.indices.GetIndexResponse) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) IOException(java.io.IOException)

Example 50 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class ElasticsearchStoreMetadataAnalyzer method getTableInfo.

@Override
public ElasticsearchStoreCollectionMetadata getTableInfo(String tableName) throws GoraException {
    GetIndexRequest request = new GetIndexRequest(tableName);
    GetIndexResponse getIndexResponse;
    try {
        getIndexResponse = elasticsearchClient.indices().get(request, RequestOptions.DEFAULT);
    } catch (IOException ex) {
        throw new GoraException(ex);
    }
    MappingMetadata indexMappings = getIndexResponse.getMappings().get(tableName);
    Map<String, Object> indexKeysAndTypes = (Map<String, Object>) indexMappings.getSourceAsMap().get("properties");
    List<String> documentTypes = new ArrayList<>();
    List<String> documentKeys = new ArrayList<>();
    for (Map.Entry<String, Object> entry : indexKeysAndTypes.entrySet()) {
        Map<String, Object> subEntry = (Map<String, Object>) entry.getValue();
        documentTypes.add((String) subEntry.get("type"));
        documentKeys.add(entry.getKey());
    }
    ElasticsearchStoreCollectionMetadata collectionMetadata = new ElasticsearchStoreCollectionMetadata();
    collectionMetadata.setDocumentKeys(documentKeys);
    collectionMetadata.setDocumentTypes(documentTypes);
    return collectionMetadata;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) GoraException(org.apache.gora.util.GoraException) GetIndexResponse(org.elasticsearch.client.indices.GetIndexResponse) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) MappingMetadata(org.elasticsearch.cluster.metadata.MappingMetadata) Map(java.util.Map)

Aggregations

GoraException (org.apache.gora.util.GoraException)174 IOException (java.io.IOException)119 ArrayList (java.util.ArrayList)30 Schema (org.apache.avro.Schema)25 SQLException (java.sql.SQLException)17 HashMap (java.util.HashMap)17 InvocationTargetException (java.lang.reflect.InvocationTargetException)13 PreparedStatement (java.sql.PreparedStatement)11 Map (java.util.Map)11 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)9 SolrServerException (org.apache.solr.client.solrj.SolrServerException)9 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)8 AccumuloException (org.apache.accumulo.core.client.AccumuloException)8 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)8 Field (org.apache.avro.Schema.Field)8 KuduException (org.apache.kudu.client.KuduException)8 ResultSet (com.datastax.driver.core.ResultSet)7 SimpleStatement (com.datastax.driver.core.SimpleStatement)7 SAXBuilder (org.jdom.input.SAXBuilder)7 InputStream (java.io.InputStream)6