com.datastax.driver.core.querybuilder.BuiltStatement Java Examples
The following examples show how to use
com.datastax.driver.core.querybuilder.BuiltStatement.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SnapshotProcessor.java From debezium-incubator with Apache License 2.0 | 6 votes |
/** * Build the SELECT query statement for execution. For every non-primary-key column, the TTL, WRITETIME, and execution * time are also queried. * * For example, a table t with columns a, b, and c, where A is the partition key, B is the clustering key, and C is a * regular column, looks like the following: * <pre> * {@code SELECT now() as execution_time, a, b, c, TTL(c) as c_ttl, WRITETIME(c) as c_writetime FROM t;} * </pre> */ private static BuiltStatement generateSnapshotStatement(TableMetadata tableMetadata) { List<String> allCols = tableMetadata.getColumns().stream().map(ColumnMetadata::getName).collect(Collectors.toList()); Set<String> primaryCols = tableMetadata.getPrimaryKey().stream().map(ColumnMetadata::getName).collect(Collectors.toSet()); List<String> collectionCols = tableMetadata.getColumns().stream().filter(cm -> collectionTypes.contains(cm.getType().getName())) .map(ColumnMetadata::getName).collect(Collectors.toList()); Select.Selection selection = QueryBuilder.select().raw(CASSANDRA_NOW_UNIXTIMESTAMP).as(EXECUTION_TIME_ALIAS); for (String col : allCols) { selection.column(withQuotes(col)); if (!primaryCols.contains(col) && !collectionCols.contains(col)) { selection.ttl(withQuotes(col)).as(ttlAlias(col)); } } return selection.from(tableMetadata.getKeyspace().getName(), tableMetadata.getName()); }
Example #2
Source File: CassandraImportJobDAO.java From modernmt with Apache License 2.0 | 6 votes |
/** * This method stores a ImportJob object in the DB * with a new, sequentially generated ID * * @param job the ImportJob object to store in the DB * @return the same ImportJob object received as a parameter, updated with its new ID * @throws PersistenceException if couldn't insert the importjob in the DB */ @Override public ImportJob store(ImportJob job) throws PersistenceException { long id = CassandraIdGenerator.generate(connection, CassandraDatabase.IMPORT_JOBS_TABLE_ID); String[] columns = {"id", "memory", "\"begin\"", "end", "data_channel", "size"}; Object[] values = {id, job.getMemory(), job.getBegin(), job.getEnd(), job.getDataChannel(), job.getSize()}; BuiltStatement statement = QueryBuilder .insertInto("import_jobs") .values(columns, values) .ifNotExists(); boolean success = CassandraUtils.checkedExecute(connection, statement).wasApplied(); if (!success) throw new PersistenceException("Unable to insert import job into Cassandra Database: " + job); job.setId(id); return job; }
Example #3
Source File: CassandraImportJobDAO.java From modernmt with Apache License 2.0 | 6 votes |
/** * This method receives a unique UUID for an ImportJob, * uses it to extract the corresponding ID employed in the DB * and retrieves the relative ImportJob from the importjobs table * * @param uuid the ID of the ImportJob object to retrieve, in the UUID format * @return the ImportJob object with the given ID, if there is one in the DB; * else, this method returns null * @throws PersistenceException */ @Override public ImportJob retrieve(UUID uuid) throws PersistenceException { long id = ImportJob.getLongId(uuid); BuiltStatement statement = QueryBuilder. select(). from("import_jobs"). where(QueryBuilder.eq("id", id)); ResultSet result = CassandraUtils.checkedExecute(connection, statement); Row row = result.one(); if (row != null) return read(row); else return null; }
Example #4
Source File: CassandraIdGenerator.java From modernmt with Apache License 2.0 | 6 votes |
/** * This method updates the current counter for a table to a given value * if it is greater than the current counter for that table * * @param connection the current connection with the database * @param newCounter the new memories counter (if it is greater than the current one) * @throws PersistenceException */ public static boolean advanceCounter(CassandraConnection connection, int tableID, long newCounter) throws PersistenceException { /* Statement for updating the last ID only if it smaller than the new counter*/ BuiltStatement update = QueryBuilder.update(CassandraDatabase.COUNTERS_TABLE) .with(QueryBuilder.set("table_counter", newCounter)) .where(QueryBuilder.eq("table_id", tableID)) .onlyIf(QueryBuilder.lt("table_counter", newCounter)); /* Statement for retrieving the last ID*/ BuiltStatement get = QueryBuilder.select("table_counter") .from(CassandraDatabase.COUNTERS_TABLE) .where(QueryBuilder.eq("table_id", tableID)); /*Try to update the last ID and check if you have succeeded. * If you have not succeeded, try again. * If succeeded OR if the new value you are trying to write is too small * (e.g. a bigger value was written in the meantime, * and the advance is not successful) * return whether you have the advance or not*/ while (true) { boolean wasApplied = CassandraUtils.checkedExecute(connection, update).wasApplied(); long counter = CassandraUtils.checkedExecute(connection, get).one().getLong("table_counter"); if (counter >= newCounter) return wasApplied; } }
Example #5
Source File: MutateStatementHandler.java From scalardb with Apache License 2.0 | 5 votes |
protected void setCondition(BuiltStatement statement, Mutation mutation) { mutation .getCondition() .ifPresent( c -> { c.accept(new ConditionSetter(statement)); }); }
Example #6
Source File: CassandraTables.java From hugegraph with Apache License 2.0 | 5 votes |
private static Statement setTtl(BuiltStatement statement, CassandraBackendEntry.Row entry) { long ttl = entry.ttl(); if (ttl != 0L) { int calcTtl = (int) Math.ceil(ttl / 1000D); Using usingTtl = QueryBuilder.ttl(calcTtl); if (statement instanceof Insert) { ((Insert) statement).using(usingTtl); } else { assert statement instanceof Update; ((Update) statement).using(usingTtl); } } return statement; }
Example #7
Source File: SnapshotProcessor.java From debezium-incubator with Apache License 2.0 | 5 votes |
/** * Runs a SELECT query on a given table and process each row in the result set * by converting the row into a record and enqueue it to {@link ChangeRecord} */ private void takeTableSnapshot(TableMetadata tableMetadata) throws IOException { BuiltStatement statement = generateSnapshotStatement(tableMetadata); statement.setConsistencyLevel(consistencyLevel); LOGGER.info("Executing snapshot query '{}' with consistency level {}", statement.getQueryString(), statement.getConsistencyLevel()); ResultSet resultSet = cassandraClient.execute(statement); processResultSet(tableMetadata, resultSet); LOGGER.debug("The snapshot of table '{}' has been taken", tableName(tableMetadata)); }
Example #8
Source File: CassandraMemoryDAO.java From modernmt with Apache License 2.0 | 5 votes |
/** * This method retrieves a Memory object in a Cassandra DB * by the ID it was stored with. * * @param id the ID of the Memory object to retrieve * @return the Memory object stored with the passed id, if there is one in the DB; * else, this method returns null * @throws PersistenceException */ @Override public Memory retrieve(long id) throws PersistenceException { BuiltStatement statement = QueryBuilder.select() .from(CassandraDatabase.MEMORIES_TABLE) .where(QueryBuilder.eq("id", id)); ResultSet result = CassandraUtils.checkedExecute(connection, statement); return read(result.one()); }
Example #9
Source File: CassandraMemoryDAO.java From modernmt with Apache License 2.0 | 5 votes |
/** * This method retrieves from the Cassandra DB * all the Memory objects the ids of which * are contained in a given collection * * @param ids the collection of ids of the Memories to retrieve * @return the Memory objects the ids of which are contained in the passed id collection * @throws PersistenceException */ @Override public Map<Long, Memory> retrieve(Collection<Long> ids) throws PersistenceException { Map<Long, Memory> map = new HashMap<>(ids.size()); /*if the list is empty, return an empty map*/ if (ids.isEmpty()) return map; ArrayList<Long> list = new ArrayList<>(ids.size()); list.addAll(ids); BuiltStatement statement = QueryBuilder. select(). from(CassandraDatabase.MEMORIES_TABLE). where(QueryBuilder.in("id", list)); /*execute the query*/ ResultSet result = CassandraUtils.checkedExecute(connection, statement); /*create the Memory objects from the rows*/ while (!result.isExhausted()) { Memory memory = read(result.one()); map.put(memory.getId(), memory); } return map; }
Example #10
Source File: CassandraMemoryDAO.java From modernmt with Apache License 2.0 | 5 votes |
/** * This method retrieves from the Cassandra DB * all the Memory objects stored in the corresponding table * * @return a list with all the Memory objects in the DB * @throws PersistenceException */ @Override public Collection<Memory> retrieveAll() throws PersistenceException { ArrayList<Memory> list = new ArrayList<>(); BuiltStatement statement = QueryBuilder.select(). from(CassandraDatabase.MEMORIES_TABLE). where(); ResultSet result = CassandraUtils.checkedExecute(connection, statement); for (Row row : result.all()) list.add(read(row)); return list; }
Example #11
Source File: CassandraMemoryDAO.java From modernmt with Apache License 2.0 | 5 votes |
/** * This method stores a Memory object in the DB * * @param memory the Memory object to store in the DB * @param forceId if it is true, then this method tries to store memory with its ID. * Else it uses a new, sequentially generated ID. * @return if the memory was successfully stored, the method returns memory itself * (with its ID update to the new one if forceId was false). * Else, throws an exception. * @throws PersistenceException if couldn't insert the importjob in the DB */ @Override public Memory store(Memory memory, boolean forceId) throws PersistenceException { long id; if (!forceId) { id = CassandraIdGenerator.generate(connection, CassandraDatabase.MEMORIES_TABLE_ID); } else { id = memory.getId(); CassandraIdGenerator.advanceCounter(connection, CassandraDatabase.MEMORIES_TABLE_ID, id); } UUID owner = memory.getOwner(); String[] columns = {"id", "owner_msb", "owner_lsb", "name"}; Object[] values = { id, owner == null ? 0L : owner.getMostSignificantBits(), owner == null ? 0L : owner.getLeastSignificantBits(), memory.getName()}; BuiltStatement statement = QueryBuilder .insertInto(CassandraDatabase.MEMORIES_TABLE) .values(columns, values) .ifNotExists(); boolean success = CassandraUtils.checkedExecute(connection, statement).wasApplied(); if (!success) throw new PersistenceException("Unable to insert memory into Cassandra Database: " + memory); memory.setId(id); return memory; }
Example #12
Source File: CassandraMemoryDAO.java From modernmt with Apache License 2.0 | 5 votes |
/** * This method receives a Memory object * and stores it in the DB overwriting an existing row with same ID. * If in the DB there is no row with that ID nothing happens. * * @param memory the Memory object to store in the DB * in place of an already existing one * @return the same memory object passed as a parameter, * if the overwrite is successful * (if an object with that ID was already in the DB) * or null if the overwrite was not successful. * @throws PersistenceException */ @Override public Memory update(Memory memory) throws PersistenceException { BuiltStatement built = QueryBuilder.update(CassandraDatabase.MEMORIES_TABLE) .with(QueryBuilder.set("name", memory.getName())) .where(QueryBuilder.eq("id", memory.getId())) .ifExists(); ResultSet result = CassandraUtils.checkedExecute(connection, built); if (result.wasApplied()) return memory; else return null; }
Example #13
Source File: CassandraMemoryDAO.java From modernmt with Apache License 2.0 | 5 votes |
/** * This method deletes a Memory object from the DB * * @param id the id of the Memory object to delete * @return True if the object was successfully deleted; * False if no object with the passed ID could be found. * @throws PersistenceException */ @Override public boolean delete(long id) throws PersistenceException { BuiltStatement built = QueryBuilder.delete(). from(CassandraDatabase.MEMORIES_TABLE). where(QueryBuilder.eq("id", id)). ifExists(); ResultSet result = CassandraUtils.checkedExecute(connection, built); return result.wasApplied(); }
Example #14
Source File: CassandraIdGenerator.java From modernmt with Apache License 2.0 | 5 votes |
/** * This method generates a new ID for a new object * that must be stored in a certain table. * The new IDs are long and are generated in a sequential way. * <p> * This method is thread-safe. * * @param connection the current connection with the database * @param tableId the ID of the table in which we want to store a new * @return the newly generated ID, * @throws PersistenceException */ public static long generate(CassandraConnection connection, int tableId) throws PersistenceException { /*the table COUNTERS_TABLE has a row for each other table in our cassandra; each row holds the table id and a counter marking the last ID that has been employed when storing an object in that table.*/ /*statement for getting the last ID used in the table under analysis * from the Counters_table*/ BuiltStatement get = QueryBuilder.select("table_counter"). from(CassandraDatabase.COUNTERS_TABLE). where(QueryBuilder.eq("table_id", tableId)); /*Read the last ID used in the table under analysis. If it is still the same, increment it and return the new incremented ID. Otherwise, read again. This technique lets us read and update the ID atomically, so it is thread-safe (even it may be if a bit slow).*/ while (true) { /* Get the the last ID used in the table under analysis*/ long oldCount = CassandraUtils.checkedExecute(connection, get).one().getLong("table_counter"); /* Statement for updating the last ID only if it is still the same*/ BuiltStatement set = QueryBuilder.update(CassandraDatabase.COUNTERS_TABLE). with(QueryBuilder.set("table_counter", (oldCount + 1L))). where(QueryBuilder.eq("table_id", tableId)). onlyIf(QueryBuilder.eq("table_counter", oldCount)); /* Try to execute the statement; if it succeeded, * then it means that no-one has updated the last ID * after this thread has read it, so it can use it*/ if (CassandraUtils.checkedExecute(connection, set).wasApplied()) return oldCount + 1L; } }
Example #15
Source File: CassandraIdGenerator.java From modernmt with Apache License 2.0 | 5 votes |
/** * This method creates the necessary statements to store * in the counters_table a new entry for each table * created during the database initialization * * @param connection the current connection with the database * @param tableIds the IDs of the tables in the DB * @throws PersistenceException */ public static void initializeTableCounter(CassandraConnection connection, int[] tableIds) throws PersistenceException { String[] columns = {"table_id", "table_counter"}; for (int table_id : tableIds) { Object[] values = {table_id, 0}; BuiltStatement built = QueryBuilder .insertInto(CassandraDatabase.COUNTERS_TABLE) .values(columns, values) .ifNotExists(); CassandraUtils.checkedExecute(connection, built); } }
Example #16
Source File: ConditionSetter.java From scalardb with Apache License 2.0 | 2 votes |
/** * Constructs {@code ConditionSetter} with the specified {@code BuiltStatement} * * @param statement {@code BuiltStatement} to set conditions */ public ConditionSetter(BuiltStatement statement) { this.statement = statement; }