use of org.apache.gora.util.GoraException in project gora by apache.
the class IgniteStore method schemaExists.
@Override
public boolean schemaExists() throws GoraException {
try (Statement stmt = connection.createStatement()) {
String tableExistsSQL = IgniteSQLBuilder.tableExists(igniteMapping.getTableName());
ResultSet executeQuery = stmt.executeQuery(tableExistsSQL);
executeQuery.close();
return true;
} catch (SQLException ex) {
if (ex.getSQLState() != null && ex.getSQLState().equals(IgniteBackendConstants.DEFAULT_IGNITE_TABLE_NOT_EXISTS_CODE)) {
return false;
} else {
throw new GoraException(ex);
}
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class IgniteStore method createSchema.
@Override
public void createSchema() throws GoraException {
if (connection == null) {
throw new GoraException("Impossible to create the schema as no connection has been initiated.");
}
if (schemaExists()) {
return;
}
try (Statement stmt = connection.createStatement()) {
String createTableSQL = IgniteSQLBuilder.createTable(igniteMapping);
stmt.executeUpdate(createTableSQL);
LOG.info("Table {} has been created for Ignite instance.", igniteMapping.getTableName());
} catch (SQLException ex) {
throw new GoraException(ex);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class IgniteStore method delete.
@Override
public boolean delete(K key) throws GoraException {
String deleteQuery = null;
Object[] keyArray = null;
if (igniteMapping.getPrimaryKey().size() == 1) {
deleteQuery = IgniteSQLBuilder.createDeleteQuery(igniteMapping);
keyArray = new Object[] { key };
} else {
// Composite key pending
}
try (PreparedStatement stmt = connection.prepareStatement(deleteQuery)) {
IgniteSQLBuilder.fillDeleteQuery(stmt, igniteMapping, keyArray);
stmt.executeUpdate();
return true;
} catch (SQLException ex) {
throw new GoraException(ex);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class IgniteStore method get.
@Override
public T get(K key, String[] fields) throws GoraException {
String[] avFields = getFieldsToQuery(fields);
Object[] keyl = null;
if (igniteMapping.getPrimaryKey().size() == 1) {
keyl = new Object[] { key };
} else {
// Composite key pending
}
// Avro fields to Ignite fields
List<String> dbFields = new ArrayList<>();
for (String af : avFields) {
dbFields.add(igniteMapping.getFields().get(af).getName());
}
String selectQuery = IgniteSQLBuilder.createSelectQueryGet(igniteMapping, dbFields);
try (PreparedStatement stmt = connection.prepareStatement(selectQuery)) {
IgniteSQLBuilder.fillSelectQuery(stmt, igniteMapping, keyl);
ResultSet rs = stmt.executeQuery();
boolean data = rs.next();
T resp = null;
if (data) {
resp = newInstance(rs, fields);
if (rs.next()) {
LOG.warn("Multiple results for primary key {} in the schema {}, ignoring additional rows.", keyl, igniteMapping.getTableName());
}
}
rs.close();
return resp;
} catch (SQLException | IOException ex) {
throw new GoraException(ex);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class IgniteStore method initialize.
@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException {
try {
super.initialize(keyClass, persistentClass, properties);
IgniteMappingBuilder<K, T> builder = new IgniteMappingBuilder<K, T>(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);
igniteMapping = builder.getIgniteMapping();
igniteParameters = IgniteParameters.load(properties);
connection = acquireConnection(this.igniteParameters);
LOG.info("Ignite store was successfully initialized");
if (!schemaExists()) {
createSchema();
}
} catch (ClassNotFoundException | SQLException ex) {
LOG.error("Error while initializing Ignite store", ex);
throw new GoraException(ex);
}
}
Aggregations