/// <summary> /// Gets the list of temperature stored for a provided weather station and day. /// </summary> public RowSet GetTemperatureRecords(string weatherStationId, DateTime day) { var selectCql = "SELECT * FROM temperature_by_day WHERE weatherstation_id = ? AND date = ?"; //Create a statement var selectStatement = new SimpleStatement(selectCql); //Add the parameters selectStatement.Bind(weatherStationId, DateTime.Now.ToString("yyyyMMdd")); //Execute the select statement return Session.Execute(selectStatement); }
/// <summary> /// Add a temperature information for a given weather station /// </summary> public void AddTemperature(string weatherStationId, decimal value) { var insertCql = @" INSERT INTO temperature_by_day (weatherstation_id, date, event_time, temperature) VALUES (?, ?, ?, ?)"; //Create an insert statement var insertStatement = new SimpleStatement(insertCql); //Bind the parameters to the statement insertStatement.Bind(weatherStationId, DateTime.Now.ToString("yyyyMMdd"), DateTime.Now, value); //You can set other options of the statement execution, for example the consistency level. insertStatement.SetConsistencyLevel(ConsistencyLevel.Quorum); //Execute the insert Session.Execute(insertStatement); }
/// <summary> /// Add a temperature information for a given weather station asynchronously /// </summary> public Task AddTemperatureAsync(string weatherStationId, decimal value) { //It is basically the same code as the AddTemperature //Except it returns a Task that already started but didn't finished //The row would not be in Cassandra yet when this method finishes executing. var insertCql = @" INSERT INTO temperature_by_day (weatherstation_id, date, event_time, temperature) VALUES (?, ?, ?, ?)"; //Create an insert statement var insertStatement = new SimpleStatement(insertCql); //Bind the parameters to the statement insertStatement.Bind(weatherStationId, DateTime.Now.ToString("yyyyMMdd"), DateTime.Now, value); //You can set other options of the statement execution, for example the consistency level. insertStatement.SetConsistencyLevel(ConsistencyLevel.Quorum); //Execute the insert return Session.ExecuteAsync(insertStatement); }
/// <summary> /// Gets multiple user profiles by their Ids. /// </summary> public async Task<IEnumerable<UserProfile>> GetUserProfiles(ISet<Guid> userIds) { if (userIds == null || userIds.Count == 0) return Enumerable.Empty<UserProfile>(); // Since we're essentially doing a multi-get here, limit the number userIds (i.e. partition keys) to 20 in an attempt // to enforce some performance sanity. Anything larger and we might want to consider a different data model that doesn't // involve doing a multi-get if (userIds.Count > 20) throw new ArgumentOutOfRangeException("userIds", "Cannot do multi-get on more than 20 user id keys."); // As an example, we'll do the multi-get at the CQL level using an IN() clause (i.e. let Cassandra handle it). For an example of // doing it at the driver level, see the VideoReadModel.GetVideoPreviews method // Build a parameterized CQL statement with an IN clause var parameterList = string.Join(", ", Enumerable.Repeat("?", userIds.Count)); var statement = new SimpleStatement(string.Format("SELECT userid, firstname, lastname, email FROM users WHERE userid IN ({0})", parameterList)); statement.Bind(userIds.Cast<object>().ToArray()); // Execute and map to UserProfile object RowSet resultRows = await _session.ExecuteAsync(statement).ConfigureAwait(false); return resultRows.Select(MapRowToUserProfile).ToList(); }