Search in sources :

Example 61 with GoraException

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

the class InfinispanStore method execute.

/**
 * Execute the query and return the result.
 */
@Override
public Result<K, T> execute(Query<K, T> query) throws GoraException {
    LOG.debug("execute()");
    try {
        ((InfinispanQuery<K, T>) query).build();
        InfinispanResult<K, T> result = null;
        result = new InfinispanResult<>(this, (InfinispanQuery<K, T>) query);
        LOG.trace("query: " + query.toString());
        LOG.trace("result size: " + result.size());
        return result;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) InfinispanQuery(org.apache.gora.infinispan.query.InfinispanQuery) IOException(java.io.IOException) GoraException(org.apache.gora.util.GoraException)

Example 62 with GoraException

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

the class KuduStore method initialize.

@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException {
    try {
        super.initialize(keyClass, persistentClass, properties);
        KuduMappingBuilder<K, T> builder = new KuduMappingBuilder<>(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(getConf().get(PARSE_MAPPING_FILE_KEY, DEFAULT_MAPPING_FILE));
        }
        builder.readMappingFile(mappingStream);
        kuduMapping = builder.getKuduMapping();
        KuduParameters kuduParameters = KuduParameters.load(properties, getConf());
        KuduClient.KuduClientBuilder kuduClientBuilder = new KuduClient.KuduClientBuilder(kuduParameters.getMasterAddresses());
        if (kuduParameters.getBossCount() != null) {
            kuduClientBuilder.bossCount(kuduParameters.getBossCount());
        }
        if (kuduParameters.getDefaultAdminOperationTimeoutMs() != null) {
            kuduClientBuilder.defaultAdminOperationTimeoutMs(kuduParameters.getDefaultAdminOperationTimeoutMs());
        }
        if (kuduParameters.getDefaultOperationTimeoutMs() != null) {
            kuduClientBuilder.defaultOperationTimeoutMs(kuduParameters.getDefaultOperationTimeoutMs());
        }
        if (kuduParameters.getDefaultSocketReadTimeoutMs() != null) {
            kuduClientBuilder.defaultSocketReadTimeoutMs(kuduParameters.getDefaultSocketReadTimeoutMs());
        }
        if (kuduParameters.getWorkerCount() != null) {
            kuduClientBuilder.workerCount(kuduParameters.getWorkerCount());
        }
        if (kuduParameters.isClientStatistics() != null && !kuduParameters.isClientStatistics()) {
            kuduClientBuilder.disableStatistics();
        }
        client = kuduClientBuilder.build();
        session = client.newSession();
        if (kuduParameters.getFlushMode() != null) {
            session.setFlushMode(SessionConfiguration.FlushMode.valueOf(kuduParameters.getFlushMode()));
        }
        if (kuduParameters.getFlushInterval() != null) {
            session.setFlushInterval(kuduParameters.getFlushInterval());
        }
        LOG.info("Kudu store was successfully initialized");
        if (!schemaExists()) {
            createSchema();
        } else {
            table = client.openTable(kuduMapping.getTableName());
        }
    } catch (Exception ex) {
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) InputStream(java.io.InputStream) KuduClient(org.apache.kudu.client.KuduClient) KuduMappingBuilder(org.apache.gora.kudu.mapping.KuduMappingBuilder) Charset(java.nio.charset.Charset) KuduParameters(org.apache.gora.kudu.utils.KuduParameters) KuduException(org.apache.kudu.client.KuduException) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 63 with GoraException

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

the class KuduStore method deleteByQuery.

@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
    try {
        long count = 0;
        Column pkc = kuduMapping.getPrimaryKey().get(0);
        ColumnSchema column = table.getSchema().getColumn(pkc.getName());
        List<String> dbFields = new ArrayList<>();
        dbFields.add(pkc.getName());
        List<KuduPredicate> rangePredicates = KuduClientUtils.createRangePredicate(column, query.getStartKey(), query.getEndKey());
        rangePredicates.add(KuduPredicate.newIsNotNullPredicate(column));
        KuduScanner build = createScanner(rangePredicates, dbFields, query.getLimit());
        while (build.hasMoreRows()) {
            RowResultIterator nextRows = build.nextRows();
            for (RowResult it : nextRows) {
                count++;
                K key = (K) KuduClientUtils.getObjectRow(it, pkc);
                if (query.getFields() != null && query.getFields().length < kuduMapping.getFields().size()) {
                    Update updateOp = table.newUpdate();
                    PartialRow row = updateOp.getRow();
                    String[] avFields = getFieldsToQuery(query.getFields());
                    KuduClientUtils.addObjectRow(row, pkc, key);
                    for (String af : avFields) {
                        row.setNull(kuduMapping.getFields().get(af).getName());
                    }
                    session.apply(updateOp);
                } else {
                    delete(key);
                }
            }
        }
        build.close();
        return count;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) PartialRow(org.apache.kudu.client.PartialRow) ColumnSchema(org.apache.kudu.ColumnSchema) Update(org.apache.kudu.client.Update) KuduPredicate(org.apache.kudu.client.KuduPredicate) KuduException(org.apache.kudu.client.KuduException) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) RowResultIterator(org.apache.kudu.client.RowResultIterator) RowResult(org.apache.kudu.client.RowResult) GoraException(org.apache.gora.util.GoraException) Column(org.apache.gora.kudu.mapping.Column) KuduScanner(org.apache.kudu.client.KuduScanner)

Example 64 with GoraException

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

the class KuduStore method exists.

@Override
public boolean exists(K key) throws GoraException {
    try {
        ColumnSchema column = table.getSchema().getColumn(kuduMapping.getPrimaryKey().get(0).getName());
        ArrayList<KuduPredicate> equalPredicate = new ArrayList<>();
        equalPredicate.add(KuduClientUtils.createEqualPredicate(column, key));
        KuduScanner build = createScanner(equalPredicate, new ArrayList<>(), 1);
        RowResult waitFirstResult = KuduClientUtils.waitFirstResult(build);
        build.close();
        return waitFirstResult != null;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : RowResult(org.apache.kudu.client.RowResult) GoraException(org.apache.gora.util.GoraException) KuduScanner(org.apache.kudu.client.KuduScanner) ArrayList(java.util.ArrayList) ColumnSchema(org.apache.kudu.ColumnSchema) KuduPredicate(org.apache.kudu.client.KuduPredicate) KuduException(org.apache.kudu.client.KuduException) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 65 with GoraException

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

the class KuduMappingBuilder method readMappingFile.

/**
 * Reads Kudu mappings from file
 *
 * @param inputStream Mapping input stream
 * @throws org.apache.gora.util.GoraException Error reading mapping file
 */
public void readMappingFile(InputStream inputStream) throws GoraException {
    try {
        SAXBuilder saxBuilder = new SAXBuilder();
        if (inputStream == null) {
            LOG.error("The mapping input stream is null!");
            throw new GoraException("The mapping input stream is null!");
        }
        Document document = saxBuilder.build(inputStream);
        if (document == null) {
            LOG.error("The mapping document is null!");
            throw new GoraException("The mapping document is null!");
        }
        @SuppressWarnings("unchecked") List<Element> classes = document.getRootElement().getChildren("class");
        boolean keyClassMatches = false;
        for (Element classElement : classes) {
            if (classElement.getAttributeValue("keyClass").equals(dataStore.getKeyClass().getCanonicalName()) && classElement.getAttributeValue("name").equals(dataStore.getPersistentClass().getCanonicalName())) {
                keyClassMatches = true;
                LOG.debug("Keyclass and nameclass match.");
                final String tableNameFromMapping = classElement.getAttributeValue("table");
                final String tablenumReplicasMapping = classElement.getAttributeValue("numReplicas");
                String tableName = dataStore.getSchemaName(tableNameFromMapping, dataStore.getPersistentClass());
                kuduMapping.setTableName(tableName);
                kuduMapping.setNumReplicas(Integer.parseInt(tablenumReplicasMapping));
                @SuppressWarnings("unchecked") List<Element> tables = document.getRootElement().getChildren("table");
                for (Element tableElement : tables) {
                    if (tableElement.getAttributeValue("name").equals(tableNameFromMapping)) {
                        @SuppressWarnings("unchecked") List<Element> pkColumns = tableElement.getChildren("primaryKey");
                        List<Column> pkFields = new ArrayList<>();
                        for (Element aPrimaryKey : pkColumns) {
                            String columnName = aPrimaryKey.getAttributeValue("column");
                            String columnType = aPrimaryKey.getAttributeValue("type");
                            Type aDataType = Type.valueOf(columnType);
                            if (aDataType == Type.DECIMAL) {
                                int precision = Integer.parseInt(aPrimaryKey.getAttributeValue("precision"));
                                int scale = Integer.parseInt(aPrimaryKey.getAttributeValue("scale"));
                                pkFields.add(new Column(columnName, new Column.FieldType(precision, scale)));
                            } else {
                                pkFields.add(new Column(columnName, new Column.FieldType(aDataType)));
                            }
                        }
                        kuduMapping.setPrimaryKey(pkFields);
                        Element hashPartition = tableElement.getChild("hashPartition");
                        if (hashPartition != null) {
                            int numBuckets = Integer.parseInt(hashPartition.getAttributeValue("numBuckets"));
                            kuduMapping.setHashBuckets(numBuckets);
                        }
                        List<Map.Entry<String, String>> ranges = new ArrayList<>();
                        @SuppressWarnings("unchecked") List<Element> rangePartitions = tableElement.getChildren("rangePartition");
                        for (Element rangePartition : rangePartitions) {
                            String lower = rangePartition.getAttributeValue("lower");
                            String upper = rangePartition.getAttributeValue("upper");
                            ranges.add(new AbstractMap.SimpleEntry<>(lower, upper));
                        }
                        kuduMapping.setRangePartitions(ranges);
                    }
                }
                @SuppressWarnings("unchecked") List<Element> fields = classElement.getChildren("field");
                Map<String, Column> fieldsMappings = new HashMap<>();
                for (Element field : fields) {
                    String fieldName = field.getAttributeValue("name");
                    String columnName = field.getAttributeValue("column");
                    String columnType = field.getAttributeValue("type");
                    Type aDataType = Type.valueOf(columnType);
                    if (aDataType == Type.DECIMAL) {
                        int precision = Integer.parseInt(field.getAttributeValue("precision"));
                        int scale = Integer.parseInt(field.getAttributeValue("scale"));
                        fieldsMappings.put(fieldName, new Column(columnName, new Column.FieldType(precision, scale)));
                    } else {
                        fieldsMappings.put(fieldName, new Column(columnName, new Column.FieldType(aDataType)));
                    }
                }
                kuduMapping.setFields(fieldsMappings);
                break;
            }
        }
        if (!keyClassMatches) {
            throw new GoraException("gora-kudu-mapping does not include the name and keyClass in the databean.");
        }
    } catch (IOException | JDOMException e) {
        throw new GoraException(e);
    }
    LOG.info("Gora Kudu mapping file was read successfully.");
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) HashMap(java.util.HashMap) Element(org.jdom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) AbstractMap(java.util.AbstractMap) GoraException(org.apache.gora.util.GoraException) Type(org.apache.kudu.Type)

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