use of org.apache.gora.util.GoraException in project gora by apache.
the class KuduStoreMetadataAnalyzer method getTableInfo.
@Override
public KuduTableMetadata getTableInfo(String tableName) throws GoraException {
try {
KuduTable openTable = client.openTable(tableName);
List<ColumnSchema> columns = openTable.getSchema().getColumns();
HashMap<String, String> columnas = new HashMap();
String pk = "";
String pkType = "";
for (ColumnSchema esquema : columns) {
if (esquema.isKey()) {
pk = esquema.getName();
pkType = esquema.getType().getName();
} else {
columnas.put(esquema.getName(), esquema.getType().getName());
}
}
return new KuduTableMetadata(pk, pkType, columnas);
} catch (KuduException ex) {
throw new GoraException(ex);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class KuduStore method delete.
@Override
public boolean delete(K key) throws GoraException {
try {
Column pkc = kuduMapping.getPrimaryKey().get(0);
Delete delete = table.newDelete();
PartialRow row = delete.getRow();
KuduClientUtils.addObjectRow(row, pkc, key);
OperationResponse apply = session.apply(delete);
return !apply.hasRowError();
} catch (KuduException ex) {
throw new GoraException(ex);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class KuduStore method execute.
@Override
public Result<K, T> execute(Query<K, T> query) throws GoraException {
String[] avFields = getFieldsToQuery(query.getFields());
List<String> dbFields = new ArrayList<>();
for (String af : avFields) {
dbFields.add(kuduMapping.getFields().get(af).getName());
}
try {
ColumnSchema column = table.getSchema().getColumn(kuduMapping.getPrimaryKey().get(0).getName());
dbFields.add(kuduMapping.getPrimaryKey().get(0).getName());
List<KuduPredicate> rangePredicates = KuduClientUtils.createRangePredicate(column, query.getStartKey(), query.getEndKey());
rangePredicates.add(KuduPredicate.newIsNotNullPredicate(column));
KuduScanner build = createScanner(rangePredicates, dbFields, query.getLimit());
return new KuduResult<>(this, query, build);
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class KuduStore method put.
@Override
public void put(K key, T obj) throws GoraException {
try {
if (obj.isDirty()) {
Column pkc = kuduMapping.getPrimaryKey().get(0);
Upsert upsert = table.newUpsert();
PartialRow row = upsert.getRow();
KuduClientUtils.addObjectRow(row, pkc, key);
Schema schemaObj = obj.getSchema();
List<Schema.Field> fields = schemaObj.getFields();
for (Schema.Field field : fields) {
Column mappedColumn = kuduMapping.getFields().get(field.name());
if (mappedColumn != null) {
Object fieldValue = obj.get(field.pos());
if (fieldValue != null) {
Schema fieldSchema = field.schema();
Object serializedObj = serializeFieldValue(fieldSchema, fieldValue);
KuduClientUtils.addObjectRow(row, mappedColumn, serializedObj);
} else {
row.setNull(mappedColumn.getName());
}
} else {
throw new GoraException("Unmapped field : " + field.name());
}
}
session.apply(upsert);
} else {
LOG.info("Ignored putting object {} in the store as it is neither " + "new, neither dirty.", new Object[] { obj });
}
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class KuduStore method get.
@Override
public T get(K key, String[] fields) throws GoraException {
String[] avFields = getFieldsToQuery(fields);
List<String> dbFields = new ArrayList<>();
for (String af : avFields) {
dbFields.add(kuduMapping.getFields().get(af).getName());
}
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, dbFields, 1);
RowResult waitGetOneOrZero = KuduClientUtils.waitFirstResult(build);
T resp = null;
if (waitGetOneOrZero != null) {
resp = newInstance(waitGetOneOrZero, fields);
}
build.close();
return resp;
} catch (Exception ex) {
throw new GoraException(ex);
}
}
Aggregations