How to compare default Timestamp property in a cosmos DB table - azure-cosmosdb

The cosmos DB table entry manages a default Timestamp property for each table operation. While I am trying to query last updated entries based on the same time stamp field the result is not behaving as expected. The TableQuery looks like below:
TableQuery.GenerateFilterConditionForDate("Timestamp",
QueryComparisons.GreaterThanOrEqual,
timestamp)
Where timestamp is a DateTimeOffset object. I am getting 0 rows retrieved even with rows existing in the table with the Timestamp column holding a higher value. What is missing here?

Data in my table.
Query params.
Result.
var query = TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThanOrEqual, DateTimeOffset.Now.AddDays(-10).Date);
var exQuery = new TableQuery<CustomerEntity>().Where(query);
var results0 = sourcetable.ExecuteQuery(exQuery).ToList();
//var results1 = sourcetable.ExecuteQuery(exQuery).Select(ent => (CustomerEntity)ent).ToList();

Related

Result of SELECT MAX(id) is null

On the database open, I inserted 5 records into the Expense table. Then I removed all 5 records one by one.
And before I insert new entry I check max inserted id. For some reason it's null. What's wrong?
var result = await db.rawQuery("SELECT MAX(id) as last_inserted_id FROM Expense");
final maxID = result.first["last_inserted_id"];
print('maxID:$maxID'); //maxID:null
If there are no rows in the table, there is no maximum defined. What you rather should do is look for the ID sequence number of the table to find out what the next ID will be. That information is usually stored in some meta table of the DB.
SELECT seq FROM SQLITE_SEQUENCE WHERE name='Expense';

How to query AWS dynamodb to get for every one hour, for every day and for 30 days?

I wrote query like below. I am able to retrieve data fromtime and totime. My problem is for every minute they are 30 records. I would like to get help to get the first record for every one hour and 24 records for one day and I need this for 30 days.
var config = new QueryRequest
{
TableName = "dfgfdgdfg",
KeyConditionExpression = "id= :id AND plctime BETWEEN :fromtime AND :totime",
ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
{
":serialNumber", new AttributeValue {S = id}
},
{
":fromtime", new AttributeValue {S = fromtime }
},
{
":totime", new AttributeValue {S = totime }
}
},
};
return await _dynamoClient.QueryAsync(config);
In addition to storing your record as is, you could consider inserting another record that looks like this :
{
pk : "DailyMarker_" + DateTime.Now.ToString("yyMMdd"), // partition key
sk : "HourlyMarker_" + DateTime.Now.ToString("yyMMddhh") // range key
record: <your entire record>
}
pk and sk would be of the structure DailyMarker_201911 and HourlyMarker_2019112101. Basically the part after the underscore acts as a date/time stamp with only the granularity you are interested in.
While inserting a marker record, you can add precondition checks, which, if they fail, will prevent the insertion from taking place (see PutItem -> ConditionExpression. This operation throws an exception with most SDKs if the condition evaluates to false, so you want to handle that exception.
At this point only the first record per hour is being inserted into this PK/SK combination, and all SKs for one day end up under the same PK
To query for different ranges, you will have to perform some calculations in your application code to determine the start and end buckets (pk and sk) that you want to query. While you will need to make one call per pk you are interested in, the range key can be queried using range queries
You could also switch the pk to be monthly instead of daily, so that will reduce the number of PKs to query while increasing the potential for imbalanced keys (aka. hot keys)

get all data from dynamo db table without supplying any PK

I am trying to fetch all the data from my dynamodb table but unable to get as all the methods for Query / Scan operates with input parameter. so i tried getting all the rows which having primary key greater than 0.
var table = Table.LoadTable(client,Utilities.Utility.EmployeeTable);
ScanFilter filter = new ScanFilter();
filter.AddCondition("iemp_id", ScanOperator.GreaterThan, 0);
ScanOperationConfig config = new ScanOperationConfig()
{
Filter = filter,
// Optional parameters.
Select = SelectValues.SpecificAttributes,
AttributesToGet = new List<string> { "iemp_id", "demp_salary", "semp_name" }
//ConsistentRead = true
};
Search search = table.Scan(config);`
Here i am getting search.Matches = 0 where it should return data from my table.
You have only two options
1.Query : You need to supply Partition Key(mandatory) and optionally Range key.
2.Scan: Full scan of the table with out Partition key/Range Key.
In your case you will have to do full scan of the table.
DynamoDBQScanExpression scanExpression = new DynamoDBScanExpression();
scanExpression .withFilterExpression(filterexpression)
.withExpressionAttributeValues(expression values);

Insert data in second table using data inserted in first

I'm trying to use a form to gather data and store them in a table and immediately do some calculations on that data and store the results in a separate table..
But I don't seem to be able to read the first table immediately after insertion..
I always encounter a zero in my calculation meaning that the first table is considered empty when trying to calculate (but is not empty when the page is refreshed, meaning that the data finally finds its way to the first table anyway) ..
This is the example:
#{
// this is the code that gathers data from a form and stores the data in a table
foreach(var row in db.Query(selectquestions))
{
var questionid = row.QuestionId;
var answer = Request["Q" + row.QuestionId.ToString()];
var insertanswers = "INSERT into Answers (UserId,QuestionId,Answer,ExamId) VALUES(#0, #1, #2, #3)";
db.Execute(insertanswers,userid,questionid,answer,1);
}
// this is the code that tries to immediately use the data stored by the above statement and store the new calculation results in a separate table
int totalquestions= db.Query( some query using the first table ).Count();
int correctanswers= db.Query( some query using the first table ).Count();
int incorrectanswers=db.Query( some query using the first table ).Count();
var score= (float)(correctanswers * 3 - incorrectanswers) / (totalquestions * 3)*100;
var insertscores = "INSERT into Scores (UserId,ExamId,ItemId,Score) VALUES(#0, #1, #2, #3)";
db.Execute(insertscores,userid,1,1,score.ToString());
}
I'm using web pages/razor/SQL Server CE .. any advice would be appreciated

Using dates with Cassandra

I've just started my adventure with Cassandra database. I've managed to learn some basics but what I still can't understand is how to work with dates in Cassandra?
So for example in MySQL we have a datetime type for a field and we can query (for example) all fields with creation date less then 2010-01-01. Furthermore we can order the result by creation date field.
How can we achieve the same with Cassandra? How to define the corresponding Column Family and how to query (CQL) it to get the same result?
You can use type DateType to define a column of type DateType in your column family. You should really read this page, it has description and example how to do range query (that is creationdate < 2010-01-01). For ordering, you can refer to the SliceRange but this will probably cover in the cassandra client already. You will probably want to look into the cassandra client to do the query.
This is a snippet on how to do query in cassandra using hector client.
// 2010-01-01
Date date = new Date(1262275200L);
try
{
getConnection();
IndexedSlicesQuery<String, String, String> indexedSlicesQuery = HFactory.createIndexedSlicesQuery(keyspace, ss, ss, ss);
indexedSlicesQuery.setColumnNames("name");
indexedSlicesQuery.addLtExpression("timestamp", ByteBufferUtil.string(date_s.toByteBuffer(date)));
indexedSlicesQuery.addEqualsExpression("searchall", ByteBufferUtil.string(bs.toByteBuffer(true)));
indexedSlicesQuery.setColumnFamily(column_family);
indexedSlicesQuery.setStartKey("");
System.out.println(indexedSlicesQuery.toString());
QueryResult<OrderedRows<String, String, String>> res = indexedSlicesQuery.execute();
List<Row<String, String, String>> list = res.get().getList();
for (Row<?, ?, ?> row : list)
{
System.out.println(row.getKey());
}
}

Resources