Arpan Das
Arpan Das

Reputation: 1037

IllegalArgumentException: Table xyz does not exist in keyspace my_ks

I am developing an application, where I am trying to create a table if not exists and making a Query on it. It is working fine in normal cases. But for the first time , when the table is created , then when trying to Query the same table, the application is throwing :

IllegalArgumentException: Table xyz does not exist in keyspace my_ks

Same happens if I drop the table, and when my code recreates the table again.

For other cases, when the table exists, it is working fine. Is it some kind of replication issue, or should use a timeout from some time when the table is created for first time.

Following is the code snippet:

  // Oredr 1: First this will be called
     public boolean isSchemaExists() {
            boolean isSchemaExists = false;
        Statement statement = QueryBuilder
                .select()
                .countAll()
                .from(keyspace_name, table_name);
        statement.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
        try {
            Session session = cassandraClient.getSession(someSessionKey);
            ResultSet resultSet = null;
            resultSet = session.execute(statement);
            if (resultSet.one() != null) {
                isSchemaExists = true;
            }
        } catch (all exception handling)

        }
        return isSchemaExists;
    }

  // Oredr 2: if previous method returns false then this will be get called 
    public void createSchema(String createTableScript) {
        Session session = cassandraClient.getSession(someSessionKey);
        if (isKeySpaceExists(keyspaceName, session)) {
            session.execute("USE " + keyspaceName);
        }
        session.execute(createTableScript);
    }

  //Oredr 3: Now read the table, this is throwing the exception when the table 
  // is created for first time
    public int readTable(){
        Session session = cassandraClient.getSession(someSessionKey);
        MappingManager manager = new MappingManager(session);
        Mapper<MyPojo> mapper = manager.mapper(MyPojo.class);
        Statement statement = QueryBuilder
                .select()
                .from(keyspaceName, tableName)
                .where(eq("col_1", someValue)).and(eq("col_2", someValue));
        statement.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
        ResultSet resultSet = session.execute(statement);
        result = mapper.map(resultSet);
        for (MyPojo myPojo : result) {
            return myPojo.getCol1();
        }
    }

Upvotes: 0

Views: 768

Answers (1)

undefined_variable
undefined_variable

Reputation: 6228

In isSchemaExists function use system.tables.

SELECT * FROM system.tables WHERE keyspace_name='YOUR KEYSPACE' AND table_name='YOUR TABLE'

Corresponding Java Code:

Statement statement = QueryBuilder
                .select()
                .from("system", "tables")
                .where(eq("keyspace_name", keyspace)).and(eq("table_name", table)); 

It seems like in isSchemaExists you are using actual table and keyspace which will not exist when dropped or not created. That's the reason it is throwing you error table does not exist.

Upvotes: 1

Related Questions