Created
April 10, 2016 21:31
-
-
Save frankwis/f3349aaaab57783846ec3cffd1ed4a3d to your computer and use it in GitHub Desktop.
Prepared select statements don't reflect table alterations without Cluster re-instantiation in Cassandra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void reprepareStatement() { | |
Cluster cluster = Cluster.builder().addContactPoints("localhost").build(); | |
Session session = cluster.connect("test"); | |
Statement create = createTable("test") | |
.addPartitionKey("id", DataType.text()) | |
.addColumn("initial_column", DataType.text()) | |
.ifNotExists(); | |
session.execute(create); | |
PreparedStatement prepared = session.prepare("select * from test"); | |
ResultSet result = session.execute(prepared.bind()); | |
logger.info("Columns before alter table: {}", getColumnDefinitions(result)); | |
// Columns before alter table: [id, initial_column] | |
session.execute(alterTable("test").addColumn("new_column").type(DataType.text())); | |
result = session.execute(prepared.bind()); | |
logger.info("Columns after alter table: {}", getColumnDefinitions(result)); | |
// Columns after alter table: [id, initial_column] | |
result = session.execute("select * from test"); | |
logger.info("Columns after unprepared execute: {}", getColumnDefinitions(result)); | |
// Columns after unprepared execute: [id, initial_column, new_column] | |
prepared = session.prepare("select * from test"); | |
result = session.execute(prepared.bind()); // Cassandra Warning: "Re-preparing already prepared query select * from test. Please note that preparing the same query more than once is generally an anti-pattern and will likely affect performance. Consider preparing the statement only once." | |
logger.info("Columns after re-prepare: {}", getColumnDefinitions(result)); | |
// Columns after re-prepare: [id, initial_column] | |
session.close(); | |
session = cluster.connect("test"); | |
prepared = session.prepare("select * from test"); // Cassandra Warning: "Re-preparing already prepared query select * from test. Please note that preparing the same query more than once is generally an anti-pattern and will likely affect performance. Consider preparing the statement only once." | |
result = session.execute(prepared.bind()); | |
logger.info("Columns recreating session via re-connect on cluster: {}", getColumnDefinitions(result)); | |
// Columns recreating session via re-connect on cluster: [id, initial_column] | |
session.close(); | |
cluster.close(); | |
session = Cluster.builder().addContactPoints("localhost").build().connect("test"); | |
prepared = session.prepare("select * from test"); | |
result = session.execute(prepared.bind()); | |
logger.info("Columns after recreating session via re-instantiating cluster: {}", getColumnDefinitions(result)); | |
// Columns after recreating session via re-instantiating cluster: [id, initial_column, new_column] | |
session.execute(dropTable("test")); | |
} | |
private List<String> getColumnDefinitions(ResultSet result) { | |
return result.getColumnDefinitions() | |
.asList() | |
.stream() | |
.map(definition -> definition.getName()) | |
.collect(Collectors.toList()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment