org.apache.cassandra.cql3.QueryProcessor Java Examples

The following examples show how to use org.apache.cassandra.cql3.QueryProcessor. 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: SSTableImportTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
/* 
 *  The schema is 
 *      CREATE TABLE cql_keyspace.table1 (k int PRIMARY KEY, v1 text, v2 int)
 * */
public void shouldImportCqlTable() throws IOException, URISyntaxException
{
    String cql_keyspace = "cql_keyspace";
    String cql_table = "table1";
    String jsonUrl = resourcePath("CQLTable.json");
    File tempSS = tempSSTableFile(cql_keyspace, cql_table);
    new SSTableImport(true).importJson(jsonUrl, cql_keyspace, cql_table, tempSS.getPath());
    SSTableReader reader = SSTableReader.open(Descriptor.fromFilename(tempSS.getPath()));
    Keyspace.open(cql_keyspace).getColumnFamilyStore(cql_table).addSSTable(reader);
    
    UntypedResultSet result = QueryProcessor.executeOnceInternal(String.format("SELECT * FROM %s.%s", cql_keyspace, cql_table));
    assertThat(result.size(), is(2));
    assertThat(result, hasItem(withElements(1, "NY", 1980)));
    assertThat(result, hasItem(withElements(2, "CA", 2014)));
    reader.selfRef().release();
}
 
Example #2
Source File: PerRowSecondaryIndexTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSearch() throws IOException
{
    Mutation rm;
    rm = new Mutation("PerRowSecondaryIndex", ByteBufferUtil.bytes("k4"));
    rm.add("Indexed1", Util.cellname("indexed"), ByteBufferUtil.bytes("foo"), 1);
    rm.apply();
    
    // test we can search:
    UntypedResultSet result = QueryProcessor.executeInternal("SELECT * FROM \"PerRowSecondaryIndex\".\"Indexed1\" WHERE indexed = 'foo'");
    assertEquals(1, result.size());

    // test we can't search if the searcher doesn't validate the expression:
    try
    {
        QueryProcessor.executeInternal("SELECT * FROM \"PerRowSecondaryIndex\".\"Indexed1\" WHERE indexed = 'invalid'");
        fail("Query should have been invalid!");
    }
    catch (Exception e)
    {
        assertTrue(e instanceof InvalidRequestException || (e.getCause() != null && (e.getCause() instanceof InvalidRequestException)));
    }
}
 
Example #3
Source File: Cqlsh.java    From sstable-tools with Apache License 2.0 6 votes vote down vote up
public Query getQuery(String command) throws Exception {
    SelectStatement.RawStatement statement = (SelectStatement.RawStatement) QueryProcessor.parseStatement(command);
    if (statement.columnFamily().matches("sstables?")) {
        if (sstables.isEmpty()) {
            return null;
        }
        metadata = CassandraUtils.tableFromBestSource(sstables.iterator().next());
        return new Query(command, sstables, metadata);
    } else {
        File path = new File(statement.columnFamily());
        if (!path.exists()) {
            throw new FileNotFoundException(path.getAbsolutePath());
        }
        metadata = CassandraUtils.tableFromBestSource(path);
        return new Query(command, Collections.singleton(path), metadata);
    }
}
 
Example #4
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 #5
Source File: SSTableAttachedSecondaryIndexTest.java    From sasi with Apache License 2.0 6 votes vote down vote up
private Set<String> executeCQL(String rawStatement) throws Exception
{
    SelectStatement statement = (SelectStatement) QueryProcessor.parseStatement(rawStatement).prepare().statement;
    ResultMessage.Rows cqlRows = statement.executeInternal(QueryState.forInternalCalls(), new QueryOptions(ConsistencyLevel.LOCAL_ONE, Collections.<ByteBuffer>emptyList()));

    Set<String> results = new TreeSet<>();
    for (CqlRow row : cqlRows.toThriftResult().getRows())
    {
        for (org.apache.cassandra.thrift.Column col : row.columns)
        {
            String columnName = UTF8Type.instance.getString(col.bufferForName());
            if (columnName.equals("key"))
                results.add(AsciiType.instance.getString(col.bufferForValue()));
        }
    }

    return results;
}
 
Example #6
Source File: ClientOnlyExample.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private static void testWriting() throws Exception
{
    // do some writing.
    for (int i = 0; i < 100; i++)
    {
        QueryProcessor.process(String.format("INSERT INTO %s.%s (id, name, value) VALUES ( 'key%d', 'colb', 'value%d')",
                                             KEYSPACE,
                                             COLUMN_FAMILY,
                                             i,
                                             i),
                               ConsistencyLevel.QUORUM);

        System.out.println("wrote key" + i);
    }
    System.out.println("Done writing.");
}
 
Example #7
Source File: ScrubTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * For CASSANDRA-6892 too, check that for a compact table with one cluster column, we can insert whatever
 * we want as value for the clustering column, including something that would conflict with a CQL column definition.
 */
@Test
public void testValidationCompactStorage() throws Exception
{
    QueryProcessor.process("CREATE TABLE \"Keyspace1\".test_compact_dynamic_columns (a int, b text, c text, PRIMARY KEY (a, b)) WITH COMPACT STORAGE", ConsistencyLevel.ONE);

    Keyspace keyspace = Keyspace.open("Keyspace1");
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore("test_compact_dynamic_columns");

    QueryProcessor.executeInternal("INSERT INTO \"Keyspace1\".test_compact_dynamic_columns (a, b, c) VALUES (0, 'a', 'foo')");
    QueryProcessor.executeInternal("INSERT INTO \"Keyspace1\".test_compact_dynamic_columns (a, b, c) VALUES (0, 'b', 'bar')");
    QueryProcessor.executeInternal("INSERT INTO \"Keyspace1\".test_compact_dynamic_columns (a, b, c) VALUES (0, 'c', 'boo')");
    cfs.forceBlockingFlush();
    CompactionManager.instance.performScrub(cfs, true);

    // Scrub is silent, but it will remove broken records. So reading everything back to make sure nothing to "scrubbed away"
    UntypedResultSet rs = QueryProcessor.executeInternal("SELECT * FROM \"Keyspace1\".test_compact_dynamic_columns");
    assertEquals(3, rs.size());

    Iterator<UntypedResultSet.Row> iter = rs.iterator();
    assertEquals("foo", iter.next().getString("c"));
    assertEquals("bar", iter.next().getString("c"));
    assertEquals("boo", iter.next().getString("c"));
}
 
Example #8
Source File: KSMetaData.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize only Keyspace attributes without nested ColumnFamilies
 *
 * @param row Keyspace attributes in serialized form
 *
 * @return deserialized keyspace without cf_defs
 */
public static KSMetaData fromSchema(Row row, Iterable<CFMetaData> cfms, UTMetaData userTypes)
{
    UntypedResultSet.Row result = QueryProcessor.resultify("SELECT * FROM system.schema_keyspaces", row).one();
    try
    {
        return new KSMetaData(result.getString("keyspace_name"),
                              AbstractReplicationStrategy.getClass(result.getString("strategy_class")),
                              fromJsonMap(result.getString("strategy_options")),
                              result.getBoolean("durable_writes"),
                              cfms,
                              userTypes);
    }
    catch (ConfigurationException e)
    {
        throw new RuntimeException(e);
    }
}
 
Example #9
Source File: Auth.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private static void setupDefaultSuperuser()
{
    try
    {
        // insert a default superuser if AUTH_KS.USERS_CF is empty.
        if (!hasExistingUsers())
        {
            QueryProcessor.process(String.format("INSERT INTO %s.%s (name, super) VALUES ('%s', %s) USING TIMESTAMP 0",
                                                 AUTH_KS,
                                                 USERS_CF,
                                                 DEFAULT_SUPERUSER_NAME,
                                                 true),
                                   ConsistencyLevel.ONE);
            logger.info("Created default superuser '{}'", DEFAULT_SUPERUSER_NAME);
        }
    }
    catch (RequestExecutionException e)
    {
        logger.warn("Skipped default superuser setup: some nodes were not ready");
    }
}
 
Example #10
Source File: CQLMetrics.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public CQLMetrics()
{
    regularStatementsExecuted = Metrics.newCounter(factory.createMetricName("RegularStatementsExecuted"));
    preparedStatementsExecuted = Metrics.newCounter(factory.createMetricName("PreparedStatementsExecuted"));
    preparedStatementsEvicted = Metrics.newCounter(factory.createMetricName("PreparedStatementsEvicted"));

    preparedStatementsCount = Metrics.newGauge(factory.createMetricName("PreparedStatementsCount"), new Gauge<Integer>()
    {
        public Integer value()
        {
            return QueryProcessor.preparedStatementsCount();
        }
    });
    preparedStatementsRatio = Metrics.newGauge(factory.createMetricName("PreparedStatementsRatio"), new RatioGauge()
    {
        public double getNumerator()
        {
            return preparedStatementsExecuted.count();
        }

        public double getDenominator()
        {
            return regularStatementsExecuted.count() + preparedStatementsExecuted.count();
        }
    });
}
 
Example #11
Source File: OptionsMessage.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public Message.Response execute(QueryState state)
{
    List<String> cqlVersions = new ArrayList<String>();
    cqlVersions.add(QueryProcessor.CQL_VERSION.toString());

    List<String> compressions = new ArrayList<String>();
    if (FrameCompressor.SnappyCompressor.instance != null)
        compressions.add("snappy");
    // LZ4 is always available since worst case scenario it default to a pure JAVA implem.
    compressions.add("lz4");

    Map<String, List<String>> supported = new HashMap<String, List<String>>();
    supported.put(StartupMessage.CQL_VERSION, cqlVersions);
    supported.put(StartupMessage.COMPRESSION, compressions);

    return new SupportedMessage(supported);
}
 
Example #12
Source File: CQLMetricsTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testPreparedStatementsExecuted()
{
    clearMetrics();

    assertEquals(0, QueryProcessor.metrics.preparedStatementsExecuted.count());
    assertEquals(0, QueryProcessor.metrics.regularStatementsExecuted.count());

    for (int i = 0; i < 10; i++)
        session.execute(metricsStatement.bind(i, "val" + i));

    assertEquals(10, QueryProcessor.metrics.preparedStatementsExecuted.count());
    assertEquals(0, QueryProcessor.metrics.regularStatementsExecuted.count());
}
 
Example #13
Source File: ScrubTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testScrubColumnValidation() throws InterruptedException, RequestExecutionException, ExecutionException
{
    QueryProcessor.process("CREATE TABLE \"Keyspace1\".test_compact_static_columns (a bigint, b timeuuid, c boolean static, d text, PRIMARY KEY (a, b))", ConsistencyLevel.ONE);

    Keyspace keyspace = Keyspace.open("Keyspace1");
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore("test_compact_static_columns");

    QueryProcessor.executeInternal("INSERT INTO \"Keyspace1\".test_compact_static_columns (a, b, c, d) VALUES (123, c3db07e8-b602-11e3-bc6b-e0b9a54a6d93, true, 'foobar')");
    cfs.forceBlockingFlush();
    CompactionManager.instance.performScrub(cfs, false);
}
 
Example #14
Source File: CQLMetricsTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testPreparedStatementsRatio()
{
    clearMetrics();

    assertEquals(Double.NaN, QueryProcessor.metrics.preparedStatementsRatio.value());

    for (int i = 0; i < 10; i++)
        session.execute(metricsStatement.bind(i, "val" + i));
    assertEquals(1.0, QueryProcessor.metrics.preparedStatementsRatio.value());

    for (int i = 0; i < 10; i++)
        session.execute(String.format("INSERT INTO junit.metricstest (id, val) VALUES (%d, '%s')", i, "val" + i));
    assertEquals(0.5, QueryProcessor.metrics.preparedStatementsRatio.value());
}
 
Example #15
Source File: TriggerExecutor.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void validate(Collection<Mutation> tmutations) throws InvalidRequestException
{
    for (Mutation mutation : tmutations)
    {
        QueryProcessor.validateKey(mutation.key());
        for (ColumnFamily tcf : mutation.getColumnFamilies())
            for (Cell cell : tcf)
                cell.validateFields(tcf.metadata());
    }
}
 
Example #16
Source File: CQLMetricsTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegularStatementsExecuted()
{
    clearMetrics();

    assertEquals(0, QueryProcessor.metrics.preparedStatementsExecuted.count());
    assertEquals(0, QueryProcessor.metrics.regularStatementsExecuted.count());

    for (int i = 0; i < 10; i++)
        session.execute(String.format("INSERT INTO junit.metricstest (id, val) VALUES (%d, '%s')", i, "val" + i));

    assertEquals(0, QueryProcessor.metrics.preparedStatementsExecuted.count());
    assertEquals(10, QueryProcessor.metrics.regularStatementsExecuted.count());
}
 
Example #17
Source File: CQLMetricsTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testPreparedStatementsCount()
{
    assertEquals(0, (int) QueryProcessor.metrics.preparedStatementsCount.value());
    metricsStatement = session.prepare("INSERT INTO junit.metricstest (id, val) VALUES (?, ?)");
    assertEquals(1, (int) QueryProcessor.metrics.preparedStatementsCount.value());
}
 
Example #18
Source File: KSMetaData.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Deserialize ColumnFamilies from low-level schema representation, all of them belong to the same keyspace
 *
 * @return map containing name of the ColumnFamily and it's metadata for faster lookup
 */
public static Map<String, CFMetaData> deserializeColumnFamilies(Row row)
{
    if (row.cf == null)
        return Collections.emptyMap();

    Map<String, CFMetaData> cfms = new HashMap<>();
    UntypedResultSet results = QueryProcessor.resultify("SELECT * FROM system.schema_columnfamilies", row);
    for (UntypedResultSet.Row result : results)
    {
        CFMetaData cfm = CFMetaData.fromSchema(result);
        cfms.put(cfm.cfName, cfm);
    }
    return cfms;
}
 
Example #19
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception
{
    StorageService.instance.initServer(0);
    if (thriftServer == null || ! thriftServer.isRunning())
    {
        thriftServer = new ThriftServer(InetAddress.getLocalHost(), 9170, 50);
        thriftServer.start();
    }

    String cql = String.format("CREATE KEYSPACE IF NOT EXISTS %s " +
                               "WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1}",
                               ksName);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);

    cql = String.format("CREATE TABLE IF NOT EXISTS %s.%s (k int, v1 int, v2 int, PRIMARY KEY (k))", ksName, cfName);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);

    cql = String.format("CREATE TABLE IF NOT EXISTS %s.%s (k int, v1 int, v2 int, PRIMARY KEY (k))", ksName, otherCf);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);

    // no conditional execution of create trigger stmt yet
    if (! triggerCreated)
    {
        cql = String.format("CREATE TRIGGER trigger_1 ON %s.%s USING '%s'",
                            ksName, cfName, TestTrigger.class.getName());
        QueryProcessor.process(cql, ConsistencyLevel.ONE);
        triggerCreated = true;
    }
}
 
Example #20
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void executeTriggerOnCqlInsert() throws Exception
{
    String cql = String.format("INSERT INTO %s.%s (k, v1) VALUES (0, 0)", ksName, cfName);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);
    assertUpdateIsAugmented(0);
}
 
Example #21
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void executeTriggerOnCqlBatchInsert() throws Exception
{
    String cql = String.format("BEGIN BATCH " +
                               "    INSERT INTO %s.%s (k, v1) VALUES (1, 1); " +
                               "APPLY BATCH",
                               ksName, cfName);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);
    assertUpdateIsAugmented(1);
}
 
Example #22
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void executeTriggerOnCqlInsertWithConditions() throws Exception
{
    String cql = String.format("INSERT INTO %s.%s (k, v1) VALUES (4, 4) IF NOT EXISTS", ksName, cfName);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);
    assertUpdateIsAugmented(4);
}
 
Example #23
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void executeTriggerOnCqlBatchWithConditions() throws Exception
{
    String cql = String.format("BEGIN BATCH " +
                               "  INSERT INTO %1$s.%2$s (k, v1) VALUES (5, 5) IF NOT EXISTS; " +
                               "  INSERT INTO %1$s.%2$s (k, v1) VALUES (5, 5); " +
                               "APPLY BATCH",
                                ksName, cfName);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);
    assertUpdateIsAugmented(5);
}
 
Example #24
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test(expected=RuntimeException.class)
public void onCqlUpdateWithConditionsRejectGeneratedUpdatesForDifferentPartition() throws Exception
{
    String cf = "cf" + System.nanoTime();
    try
    {
        setupTableWithTrigger(cf, CrossPartitionTrigger.class);
        String cql = String.format("INSERT INTO %s.%s (k, v1) VALUES (7, 7) IF NOT EXISTS", ksName, cf);
        QueryProcessor.process(cql, ConsistencyLevel.ONE);
    }
    finally
    {
        assertUpdateNotExecuted(cf, 7);
    }
}
 
Example #25
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test(expected=RuntimeException.class)
public void onCqlUpdateWithConditionsRejectGeneratedUpdatesForDifferentTable() throws Exception
{
    String cf = "cf" + System.nanoTime();
    try
    {
        setupTableWithTrigger(cf, CrossTableTrigger.class);
        String cql = String.format("INSERT INTO %s.%s (k, v1) VALUES (8, 8) IF NOT EXISTS", ksName, cf);
        QueryProcessor.process(cql, ConsistencyLevel.ONE);
    }
    finally
    {
        assertUpdateNotExecuted(cf, 7);
    }
}
 
Example #26
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void setupTableWithTrigger(String cf, Class<? extends ITrigger> triggerImpl)
throws RequestExecutionException
{
    String cql = String.format("CREATE TABLE IF NOT EXISTS %s.%s (k int, v1 int, v2 int, PRIMARY KEY (k))", ksName, cf);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);

    // no conditional execution of create trigger stmt yet
    cql = String.format("CREATE TRIGGER trigger_1 ON %s.%s USING '%s'",
                        ksName, cf, triggerImpl.getName());
    QueryProcessor.process(cql, ConsistencyLevel.ONE);
}
 
Example #27
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void assertUpdateIsAugmented(int key)
{
    UntypedResultSet rs = QueryProcessor.executeInternal(
                            String.format("SELECT * FROM %s.%s WHERE k=%s", ksName, cfName, key));
    assertTrue(String.format("Expected value (%s) for augmented cell v2 was not found", key), rs.one().has("v2"));
    assertEquals(999, rs.one().getInt("v2"));
}
 
Example #28
Source File: SSTableRecordReader.java    From hadoop-sstable with Apache License 2.0 5 votes vote down vote up
private static CreateColumnFamilyStatement getCreateColumnFamilyStatement(String cql) {
    CreateColumnFamilyStatement statement;
    try {
        statement = (CreateColumnFamilyStatement) QueryProcessor.parseStatement(cql).prepare().statement;
    } catch (RequestValidationException e) {
        // Cannot proceed if an error occurs
        throw new RuntimeException("Error configuring SSTable reader. Cannot proceed", e);
    }
    return statement;
}
 
Example #29
Source File: CQLUtil.java    From hadoop-sstable with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a CQL CREATE statement into its CFMetaData object
 *
 * @param cql
 * @return CFMetaData
 * @throws RequestValidationException if CQL is invalid
 */
public static CFMetaData parseCreateStatement(String cql) throws RequestValidationException {
    final CreateColumnFamilyStatement statement =
            (CreateColumnFamilyStatement) QueryProcessor.parseStatement(cql).prepare().statement;

    final CFMetaData cfm =
            new CFMetaData("assess", "kvs_strict", ColumnFamilyType.Standard, statement.comparator, null);

    statement.applyPropertiesTo(cfm);
    return cfm;
}
 
Example #30
Source File: Auth.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static boolean hasExistingUsers() throws RequestExecutionException
{
    // Try looking up the 'cassandra' default super user first, to avoid the range query if possible.
    String defaultSUQuery = String.format("SELECT * FROM %s.%s WHERE name = '%s'", AUTH_KS, USERS_CF, DEFAULT_SUPERUSER_NAME);
    String allUsersQuery = String.format("SELECT * FROM %s.%s LIMIT 1", AUTH_KS, USERS_CF);
    return !QueryProcessor.process(defaultSUQuery, ConsistencyLevel.ONE).isEmpty()
        || !QueryProcessor.process(defaultSUQuery, ConsistencyLevel.QUORUM).isEmpty()
        || !QueryProcessor.process(allUsersQuery, ConsistencyLevel.QUORUM).isEmpty();
}