0
我正在開發一個應用程序,我試圖創建一個表(如果不存在並對其進行查詢)。它在正常情況下工作正常。但對於第一次,在創建表,試圖查詢相同的表,然後當,應用程序拋出:如果我把表IllegalArgumentException:在鍵空間中不存在表xyz my_ks
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()得到異常,則處理該異常並返回false。因此,createSchema()將被調用,然後表被創建。爲了3,我在該表中進行查詢。它仍然沒有發現表格異常。我想它需要一些時間來複制所有節點中的表。現在我在創建後使用了5秒的超時時間,並且工作正常。但這不是一個好的解決方案 –