Cassandra IllegalArgumentException:表xyz在键空间my_ks中不存在

Cassandra IllegalArgumentException:表xyz在键空间my_ks中不存在,cassandra,datastax,Cassandra,Datastax,我正在开发一个应用程序,如果不存在表,我将尝试创建一个表并对其进行查询。它在正常情况下工作正常。但是第一次,当创建表时,当尝试查询同一个表时,应用程序抛出: IllegalArgumentException: Table xyz does not exist in keyspace my_ks 如果我删除表,并且代码再次创建表,也会发生同样的情况 对于其他情况,当表存在时,它工作正常。这是某种复制问题,还是应该使用首次创建表时的超时时间 以下是代码片段: // Oredr 1: First

我正在开发一个应用程序,如果不存在表,我将尝试创建一个表并对其进行查询。它在正常情况下工作正常。但是第一次,当创建表时,当尝试查询同一个表时,应用程序抛出:

IllegalArgumentException: Table xyz does not exist in keyspace my_ks
如果我删除表,并且代码再次创建表,也会发生同样的情况

对于其他情况,当表存在时,它工作正常。这是某种复制问题,还是应该使用首次创建表时的超时时间

以下是代码片段:

  // 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();
        }
    }
在isSchemaExists函数中,使用system.tables

从system.tables中选择*,其中keyspace\u name='YOUR keyspace'和table\u name='YOUR table'

对应的Java代码:

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

在isSchemaExists中,您似乎使用的是实际的表和键空间,在删除或不创建时,这些表和键空间将不存在。这就是它向您抛出的错误表不存在的原因。

如果isSchemaExists获得异常,则该异常已被处理,并将返回false。因此,将调用createSchema,然后创建表。按照顺序3,我在该表中进行查询。但它仍然没有给出任何表发现异常。我想在所有节点中复制表需要一些时间。现在,我在创建后使用了5秒的超时时间,工作正常。但这不是一个好的解决方案