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