Search in sources :

Example 1 with AlreadyExistsException

use of com.datastax.driver.core.exceptions.AlreadyExistsException in project newts by OpenNMS.

the class SchemaManager method create.

public void create(Schema schema, boolean ifNotExists, boolean printOnly) throws IOException {
    checkNotNull(schema, "schema argument");
    InputStream stream = checkNotNull(schema.getInputStream(), "schema input stream");
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
    String line, scrubbed;
    StringBuilder statement = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        scrubbed = scrub(line);
        statement.append(scrubbed);
        if (scrubbed.endsWith(";")) {
            // Substitute the actual keyspace name for any KEYSPACE macros.
            String queryString = statement.toString().replace(KEYSPACE_PLACEHOLDER, m_keyspace);
            queryString = queryString.replace(REPLICATION_FACTOR_PLACEHOLDER, Integer.toString(m_replicationFactor));
            if (printOnly) {
                System.err.println(queryString);
            } else {
                try {
                    m_session.execute(queryString);
                } catch (AlreadyExistsException e) {
                    if (ifNotExists) {
                        System.err.printf("%s; Nothing here to do%n", e.getLocalizedMessage());
                    } else {
                        throw e;
                    }
                } catch (SyntaxError e) {
                    System.out.printf("ERROR: %s (query: \"%s\").%n", e.getLocalizedMessage(), queryString);
                    throw e;
                }
            }
            statement = new StringBuilder();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) AlreadyExistsException(com.datastax.driver.core.exceptions.AlreadyExistsException) SyntaxError(com.datastax.driver.core.exceptions.SyntaxError) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader)

Example 2 with AlreadyExistsException

use of com.datastax.driver.core.exceptions.AlreadyExistsException in project cassandra by apache.

the class SettingsSchema method createKeySpaces.

/**
     * Create Keyspace with Standard and Super/Counter column families
     */
public void createKeySpaces(StressSettings settings) {
    JavaDriverClient client = settings.getJavaDriverClient(false);
    try {
        //Keyspace
        client.execute(createKeyspaceStatementCQL3(), org.apache.cassandra.db.ConsistencyLevel.LOCAL_ONE);
        client.execute("USE \"" + keyspace + "\"", org.apache.cassandra.db.ConsistencyLevel.LOCAL_ONE);
        //Add standard1 and counter1
        client.execute(createStandard1StatementCQL3(settings), org.apache.cassandra.db.ConsistencyLevel.LOCAL_ONE);
        client.execute(createCounter1StatementCQL3(settings), org.apache.cassandra.db.ConsistencyLevel.LOCAL_ONE);
        System.out.println(String.format("Created keyspaces. Sleeping %ss for propagation.", settings.node.nodes.size()));
        // seconds
        Thread.sleep(settings.node.nodes.size() * 1000L);
    } catch (AlreadyExistsException e) {
    //Ok.
    } catch (Exception e) {
        throw new RuntimeException("Encountered exception creating schema", e);
    }
}
Also used : JavaDriverClient(org.apache.cassandra.stress.util.JavaDriverClient) AlreadyExistsException(com.datastax.driver.core.exceptions.AlreadyExistsException) CharacterCodingException(java.nio.charset.CharacterCodingException) AlreadyExistsException(com.datastax.driver.core.exceptions.AlreadyExistsException)

Example 3 with AlreadyExistsException

use of com.datastax.driver.core.exceptions.AlreadyExistsException in project ignite by apache.

the class CassandraSessionImpl method createTableIndexes.

/**
     * Creates Cassandra table indexes.
     *
     * @param settings Persistence settings.
     */
private void createTableIndexes(String table, KeyValuePersistenceSettings settings) {
    List<String> indexDDLStatements = settings.getIndexDDLStatements(table);
    if (indexDDLStatements == null || indexDDLStatements.isEmpty())
        return;
    int attempt = 0;
    Throwable error = null;
    String tableFullName = settings.getKeyspace() + "." + table;
    String errorMsg = "Failed to create indexes for Cassandra table " + tableFullName;
    while (attempt < CQL_EXECUTION_ATTEMPTS_COUNT) {
        try {
            log.info("-----------------------------------------------------------------------");
            log.info("Creating indexes for Cassandra table '" + tableFullName + "'");
            log.info("-----------------------------------------------------------------------");
            for (String statement : indexDDLStatements) {
                try {
                    log.info(statement);
                    log.info("-----------------------------------------------------------------------");
                    session().execute(statement);
                } catch (AlreadyExistsException ignored) {
                } catch (Throwable e) {
                    if (!(e instanceof InvalidQueryException) || !"Index already exists".equals(e.getMessage()))
                        throw new IgniteException(errorMsg, e);
                }
            }
            log.info("Indexes for Cassandra table '" + tableFullName + "' were successfully created");
            return;
        } catch (Throwable e) {
            if (CassandraHelper.isHostsAvailabilityError(e))
                handleHostsAvailabilityError(e, attempt, errorMsg);
            else if (CassandraHelper.isTableAbsenceError(e))
                createTable(table, settings);
            else
                throw new IgniteException(errorMsg, e);
            error = e;
        }
        attempt++;
    }
    throw new IgniteException(errorMsg, error);
}
Also used : AlreadyExistsException(com.datastax.driver.core.exceptions.AlreadyExistsException) IgniteException(org.apache.ignite.IgniteException) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException)

Example 4 with AlreadyExistsException

use of com.datastax.driver.core.exceptions.AlreadyExistsException in project cassandra by apache.

the class StressProfile method maybeCreateSchema.

public void maybeCreateSchema(StressSettings settings) {
    if (!schemaCreated) {
        JavaDriverClient client = settings.getJavaDriverClient(false);
        if (keyspaceCql != null) {
            try {
                client.execute(keyspaceCql, org.apache.cassandra.db.ConsistencyLevel.ONE);
            } catch (AlreadyExistsException e) {
            }
        }
        client.execute("use " + keyspaceName, org.apache.cassandra.db.ConsistencyLevel.ONE);
        if (tableCql != null) {
            try {
                client.execute(tableCql, org.apache.cassandra.db.ConsistencyLevel.ONE);
            } catch (AlreadyExistsException e) {
            }
            System.out.println(String.format("Created schema. Sleeping %ss for propagation.", settings.node.nodes.size()));
            Uninterruptibles.sleepUninterruptibly(settings.node.nodes.size(), TimeUnit.SECONDS);
        }
        if (extraSchemaDefinitions != null) {
            for (String extraCql : extraSchemaDefinitions) {
                try {
                    client.execute(extraCql, org.apache.cassandra.db.ConsistencyLevel.ONE);
                } catch (AlreadyExistsException e) {
                }
            }
            System.out.println(String.format("Created extra schema. Sleeping %ss for propagation.", settings.node.nodes.size()));
            Uninterruptibles.sleepUninterruptibly(settings.node.nodes.size(), TimeUnit.SECONDS);
        }
        schemaCreated = true;
    }
    maybeLoadSchemaInfo(settings);
}
Also used : JavaDriverClient(org.apache.cassandra.stress.util.JavaDriverClient) AlreadyExistsException(com.datastax.driver.core.exceptions.AlreadyExistsException)

Aggregations

AlreadyExistsException (com.datastax.driver.core.exceptions.AlreadyExistsException)4 JavaDriverClient (org.apache.cassandra.stress.util.JavaDriverClient)2 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)1 SyntaxError (com.datastax.driver.core.exceptions.SyntaxError)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1 IgniteException (org.apache.ignite.IgniteException)1