Order DynamoDB paginated Boto scan by order - sorting

I have clear code for ordering a DynamoDB scan by ascending or descending using the
response = table.query(
ScanIndexForward=False # true = ascending, false = descending
)
argument. Likewise I have the boto paginator for paginating responses using the following:
paginator = dynamodb.get_paginator('scan')
response_iterator = paginator.paginate(
TableName=table.table_name,
PaginationConfig={"MaxItems": 25, "PageSize": 1}
)
But I am unable to find an optional argument or method to do both. The order returned by the paginator class appears to be random.
Is there a way to order the notifications by ascending or descending and then split into paginated shards?
I have investigated the optional arguments passed to the paginator scan in the documentation but ScanIndexForward is not an optional argument on SCAN and there is no ASC or DESC option in the conditions that can be passed to ScanFilter.
The table is created within the python CDK with the following partition and sort keys:
dynamodb.Table(
self,
"NotificationsTable",
partition_key=dynamodb.Attribute(
name="_id", type=dynamodb.AttributeType.STRING
),
sort_key=dynamodb.Attribute(
name="Date", type=dynamodb.AttributeType.NUMBER
)
)

You cannot order a Scan operation as it spans multiple partition keys, each pk will return at random, but in order by its accompanying sort-key. Your original request was a Query with ScanIndexForward, this only returns a single partition key ordered by the sort key.

Related

pymysql-How to return the results of a query in the form of a list of tuples

Lets say I have this code:
sql_query="select actor.actor_id from actor where actor='%s'"
cursor.execute=(sql_query,(actorID))
result=cursor.fetchall()
return(result)
What should I do to my code so the results are in the form of a list of tuples?Also I want the first tuple to be the name of the columns of my query.
For example: [(“Name”, “Id”,),(“Jim”,7,),(“Tom”,13,)]
Here is a sample example:
cur.execute('''SELECT * FROM patient_login''')
results = cur.fetchall()
nested_tuple_list = []
for result in results:
nested_tuple_list.append(result)
print(nested_tuple_list)
As you can see, we enter our select statement with cur.execute(). We fetch all of the results and store them in the variable results. We run a for loop, and each result will be a tuple of a result in our DB. We then append them to the end of the list. When we print the results, here is the output:
[(4, 'sikudabo', 'monkey1'), (83, 'sikudabo2', 'monkey2')]
We end up with a list of tuples.
Here is another answer that simple grabs the column names and stores them in a nested tuple:
cur.execute('''DESCRIBE patient_login''')
results = cur.fetchall()
nested_tuple_list = []
nested_tuple_list_2 = []
for result in results:
result = ((result[0]))
nested_tuple_list.append(result)
nested_tuple_list = tuple(nested_tuple_list)
nested_tuple_list_2.append(nested_tuple_list)
print(nested_tuple_list_2)
The describe command in SQL will Describe the table by telling you which columns exist within the table, and various characteristics of the table such as primary key, datatype ect. This command will return a tuple of the described data. Here we search each result and grab the first index in the results for each of the nested tuples. The first index corresponds with the column name in the for loop. We can append this to the first empty list. After we append all of the column names, we change the list to a tuple. We then append that tuple to the list and have all of the column names in the list within a tuple. Here is the output:
[('ID', 'username', 'password')]
If you want each element in the list to be a tuple in it of itself, here is the code:
nested_tuple_list = tuple(nested_tuple_list)
nested_tuple_list_2 = [(x,) for x in nested_tuple_list]
print(nested_tuple_list_2)
We can do a list comprehension with x representing each column, and here is our output for each of my columns in the DataFrame:
[('ID',), ('username',), ('password',)]

Get n-th result returned from MATCH query in Neo4J database

I've set up a library database where users borrow books. Using a MATCH Command i can return the book titles and number of their lendings by descending order.
My Cypher for returning the list of books and number of lendings is:
MATCH (user)-[:LENDING]->(b:Book)
RETURN b.title, COUNT(b.title) as numberOfRents
ORDER BY numberOfRents DESC
This is working properly. However, i need to get the n-th book(by lendings) returned only(let's say the third for example), which is something i failed to do until now.
Sounds like you need SKIP and LIMIT
MATCH (user)-[:LENDING]->(b:Book)
RETURN b.title, COUNT(b.title) as numberOfRents
ORDER BY numberOfRents DESC
SKIP 2 LIMIT 1
// skips the first 2, so you only get the 3rd

Search Multiple Indexes with condition

Here is requirement I am working on
There are multiple indexes with name content_ssc, content_teal, content_mmy.
These indexes can have common data (co_code is one of the field in the documents of these indexes)
a. content_ssc can have documents with co_code = teal/ssc/mmy
b. content_mmy can have documents with co_code = ssc/mmy
I need to get the data using below condition (this is one of the approach to get the unique data from these indexes)
a. (Index = content_ssc and site_code = ssc) OR (Index = content_mmy and site_code = mmy)
Basically I am getting a duplicate data from these indexes currently so I need any solution which should fetch unique data from these indexes using the above condition.
I have tried using boolean query with multiple indices from this link but it didn't produce unique result.
Please suggest.
You can use distinct query , and you will get unique result

How to retrieve the last 100 documents with a MongoDB/Moped query?

I am using the Ruby Mongoid gem and trying to create a query to retrieve the last 100 documents from a collection. Rather than using Mongoid, I would like to create the query using the underlying driver (Moped). The Moped documentation only mentions how to retrieve the first 100 records:
session[:my_collection].find.limit(100)
How can I retrieve the last 100?
I have found a solution, but you will need to sort collection in descending order. If you have a field id or date you would do:
Method .sort({fieldName: 1 or -1})
The 1 will sort ascending (oldest to newest), -1 will sort descending (newest to oldest). This will reverse entries of your collection.
session[:my_collection].find().sort({id:-1}) or
session[:my_collection].find().sort({date:-1})
If your collection contain field id (_id) that identifier have a date embedded, so you can use
session[:my_collection].find().sort({_id:-1})
In accordance with your example using .limit() the complete query will be:
session[:my_collection].find().sort({id:-1}).limit(100);
Technically that query isn't finding the first 100, that's essentially finding 100 random documents because you haven't specified an order. If you want the first then you'd have to say explicitly sort them:
session[:my_collection].find.sort(:some_field => 1).limit(100)
and to reverse the order to find the last 100 with respect to :some_field:
session[:my_collection].find.sort(:some_field => -1).limit(100)
# -----------------------------------------------^^
Of course you have decide what :some_field is going to be so the "first" and "last" make sense for you.
If you want them sorted by :some_field but want to peel off the last 100 then you could reverse them in Ruby:
session[:my_collection].find
.sort(:some_field => -1)
.limit(100)
.reverse
or you could use use count to find out how many there are then skip to offset into the results:
total = session[:my_collection].find.count
session[:my_collection].find
.sort(:some_field => 1)
.skip(total - 100)
You'd have to check that total >= 100 and adjust the skip argument if it wasn't of course. I suspect that the first solution would be faster but you should benchmark it with your data to see what reality says.

redis sort by multi fields

It's easy to use sql to query with multi sort fields.For example:
select * from user order by score desc,name desc
with two fields sort(score,name).
how should do this in redis?
Use sorted set of redis which is sorted by score. You have to prepare score according to your needs.
finalScore = score*MAX_NAME_VALUE + getIntRepresentation(name)
//MAX_NAME_VALUE is the maximum value returned by getIntRepresentation() method
and then use
zadd myset finalScore value
and the just use
zrevrange myset 0 10

Resources