org.apache.cassandra.cql3.statements.CreateTableStatement Java Examples

The following examples show how to use org.apache.cassandra.cql3.statements.CreateTableStatement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CFMetaData.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public static CFMetaData compile(String cql, String keyspace)
{
    try
    {
        CFStatement parsed = (CFStatement)QueryProcessor.parseStatement(cql);
        parsed.prepareKeyspace(keyspace);
        CreateTableStatement statement = (CreateTableStatement) parsed.prepare().statement;
        CFMetaData cfm = newSystemMetadata(keyspace, statement.columnFamily(), "", statement.comparator);
        statement.applyPropertiesTo(cfm);
        return cfm.rebuild();
    }
    catch (RequestValidationException e)
    {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: CFMetadataUtility.java    From aegisthus with Apache License 2.0 6 votes vote down vote up
public static CFMetaData initializeCfMetaData(Configuration configuration) {
    final String cql = configuration.get(Aegisthus.Feature.CONF_CQL_SCHEMA);
    Preconditions.checkNotNull(cql, "Cannot proceed without CQL definition.");

    final CreateTableStatement statement = getCreateTableStatement(cql);

    try {
        final CFMetaData cfMetaData = statement.getCFMetaData();
        cfMetaData.rebuild();

        return cfMetaData;
    } catch (RequestValidationException e) {
        // Cannot proceed if an error occurs
        throw new RuntimeException("Error initializing CFMetadata from CQL.", e);
    }
}
 
Example #3
Source File: CassandraUtils.java    From sstable-tools with Apache License 2.0 5 votes vote down vote up
public static CFMetaData tableFromCQL(InputStream source, UUID cfid) throws IOException {
    String schema = CharStreams.toString(new InputStreamReader(source, "UTF-8"));
    logger.trace("Loading Schema" + schema);
    CFStatement statement = (CFStatement) QueryProcessor.parseStatement(schema);
    String keyspace = "";
    try {
        keyspace = statement.keyspace() == null ? "turtles" : statement.keyspace();
    } catch (AssertionError e) { // if -ea added we should provide lots of warnings that things probably wont work
        logger.warn("Remove '-ea' JVM option when using sstable-tools library");
        keyspace = "turtles";
    }
    statement.prepareKeyspace(keyspace);
    if(Schema.instance.getKSMetaData(keyspace) == null) {
        Schema.instance.setKeyspaceMetadata(KeyspaceMetadata.create(keyspace, KeyspaceParams.local(), Tables.none(),
                Views.none(), getTypes(), Functions.none()));
    }
    CFMetaData cfm;
    if(cfid != null) {
        cfm = ((CreateTableStatement) statement.prepare().statement).metadataBuilder().withId(cfid).build();
        KeyspaceMetadata prev = Schema.instance.getKSMetaData(keyspace);
        List<CFMetaData> tables = Lists.newArrayList(prev.tablesAndViews());
        tables.add(cfm);
        Schema.instance.setKeyspaceMetadata(prev.withSwapped(Tables.of(tables)));
        Schema.instance.load(cfm);
    } else {
        cfm = ((CreateTableStatement) statement.prepare().statement).getCFMetaData();
    }
    return cfm;
}
 
Example #4
Source File: Cqlsh.java    From sstable-tools with Apache License 2.0 5 votes vote down vote up
public void doSchema(String command) throws Exception {
    String path = command.substring(6).trim().replaceAll("\"", "");

    if (path.equalsIgnoreCase("off")) {
        System.out.println(DISABLING_SCHEMA);
        CassandraUtils.cqlOverride = null;
    } else if (Strings.isNullOrEmpty(path)) {
        if (!Strings.isNullOrEmpty(CassandraUtils.cqlOverride)) {
            System.out.printf(USER_DEFINED_SCHEMA, CassandraUtils.cqlOverride);
        } else {
            System.out.println(NO_USER_DEFINED_SCHEMA);
        }
    } else {
        File schemaFile = new File(path);
        if (!schemaFile.exists()) {
            System.err.printf(CANNOT_FIND_FILE, schemaFile.getAbsolutePath());
        } else {
            String cql = new String(Files.readAllBytes(schemaFile.toPath()));
            try {
                ParsedStatement statement = QueryProcessor.parseStatement(cql);
                if (statement instanceof CreateTableStatement.RawStatement) {
                    CassandraUtils.cqlOverride = cql;
                    System.out.printf(IMPORTED_SCHEMA, schemaFile.getAbsolutePath());
                } else {
                    System.err.printf(FAILED_TO_IMPORT_SCHEMA, schemaFile.getAbsoluteFile(), "Wrong type of statement, " + statement.getClass());
                }
            } catch (SyntaxException se) {
                System.err.printf(FAILED_TO_IMPORT_SCHEMA, schemaFile.getAbsoluteFile(), se.getMessage());
            }
        }
    }

}