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

时间:2017-06-23 07:32:06

标签: cassandra datastax

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

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();
        }
    }

1 个答案:

答案 0 :(得分:1)

sub adder() dim zcontrol as worksheet dim wsmaster as worksheet application.screenupdating=false application.displayalerts=false On error goto Reset_settings Set wsmaster = Workbooks("Master").Worksheets("Master") Set zcontrol = Workbooks("zControl").Worksheets("zControl") zcontrol_lr=zcontrol.range("A" & rows.count).end(xlup).row wsmaster_lr=wsmaster.range("A" & rows.count).end(xlup).row wsmaster_lc=wsmaster.cells(1,columns.count).end(xltoleft).column For i=2 to wsmaster_lr for j=2 to zcontrol_lr if instr(1,Trim(wsmaster.range("M" & i).value), trim(zcontrol.range("A"& j).value),vbBinaryCompare) > 0 then wsmaster.range("N"& i).value=zcontrol.cells(j,2).value end if next j next i Msgbox "Completed!",vbinfomration,"" reset_settings: application.screenupdating=true application.displayalerts=true end sub 函数中使用isSchemaExists

system.tables

对应的Java代码:

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

似乎在Statement statement = QueryBuilder .select() .from("system", "tables") .where(eq("keyspace_name", keyspace)).and(eq("table_name", table)); 中您使用的是实际的表和键空间,在删除或未创建时不会存在。这就是它抛出错误表不存在的原因。