Custom JPQL query in Corda for doing group by operations - jpql

I have a queryable state in which I have a sender and amount fields. Is it possible to write a custom HQL or JPQL queries to get the total sum grouped by each sender.
example
select sender,sum(amount) from stateTable group by sender
If possible please let me know how

I used the JDBC session present in the serviceHub to add the custom JDBC query.
Please find below the reference for the same
https://docs.corda.net/api-persistence.html#jdbc-session-ref

Related

Querying data using partial data match?

Given a table like:
CREATE TABLE customers (
id    bigint,
email text,
fullname text,
PRIMARY KEY (id)
);
I'd like to have the capability to occasionally search using a partial data match on email address or full name.
Apache Cassandra has support for SASI which I think enables this kind of querying.
How would this be done best when using CosmosDB and its Cassandra API?
Cosmos DB does not have SASI support but there is limited support for secondary indexes with the Cassandra API (see Wire protocol support for details).
You can create an index on email with:
CREATE INDEX customers_email_idx ON customers (email);
And you can query the table with:
SELECT id, fullname FROM customers WHERE email = ?
But partial matches on fullname is a challenge since you need SASI for it. Cheers!

How to count number of record filtered by selector

I'm making smart contract with Go and I want to use Rich Query to get total count of records from CouchDB filtered by some selector like:
{\"selector\":{\"doc_type\": \"person\"}}
It is similar to:
select count(*) from tb where ...
as SQL query but how to do it with CouchDB?
If you are going to perform a rich query in chaincode then all you can do is iterate over the results and count each one. Also note that hyperledger fabric bounds the total number records you can query (it's a configuration parameter) so that would also be another consideration.
I would recommend reading this section https://hyperledger-fabric.readthedocs.io/en/release-1.4/couchdb_as_state_database.html#good-practices-for-queries
as it sounds like what you are trying to perform is likely to be not very performant

ARRAY_CONTAINS vs JOIN in azure-cosmosDB

The JSON documents that we plan to ingest into DocumentDb look as follows…
[
{"id":"id1","LastName": “user1”, "GroupMembership":["g1","g2"]},
{"id":"id2","LastName": “user2”, "GroupMembership":["g1","g4","g5"]},
{"id":"id3","LastName": “user3”, "GroupMembership":["g3","g4","g2"]},
…
]
We want to answer queries such as, get me count of all users who are members of group “g1” or “g2” etc…. The number of users is very large (few millions)…
What is the best way to implement this query and use the index and avoid any scans…
Should I be using ARRAY_CONTAINS or JOIN (does ARRAY_CONTAINS internally use the index or is it doing a scan)…
Option1)
SELECT VALUE COUNT(1) FROM Users WHERE ARRAY_CONTAINS(Users.GroupMembership, "g1") or ARRAY_CONTAINS(Users.GroupMembership, "g2")
Option2)
SELECT VALUE COUNT(1) FROM Users JOIN Membership in Users.GroupMembership WHERE Membership = "g1" or Membership = "g2"
Both queries should utilize the index the same way, but ARRAY_CONTAINS is likely to provide a better execution time compared to JOIN. You could profile both queries using the Query Metrics as per this article: https://learn.microsoft.com/en-us/azure/cosmos-db/documentdb-sql-query-metrics#query-execution-metrics
Both shall provide same index utilization, however with the JOIN usage you can get duplicating results per entry and with the ARRAY_CONTAINS you won't. I guess that difference is very significant. See more about duplicating issue in the replies to Getting duplicate records in select query for the Azure DocumentDB and Cosmos db joins give duplicate results SO question.

How to make sure that we are writing a unique record in apache-cassandra?

I want to write a record in cassandra DB with a java program.But before that I wanted to make sure that record is not present in database. Whats the best solution for that?
1. Can we impose a unique constraint in table ? Or
2. Do we need to perform a read operation before write operation to make sure its unique?(But I think performing a read op will be an overhead)
Please suggest if there is better option. Can u please share a sample program.Thanks in advance
You should be able to use a lightweight transaction for this. Example:
INSERT INTO USERS (login, email, name, login_count)
values ('jbellis', 'jbellis#datastax.com', 'Jonathan Ellis', 1)
IF NOT EXISTS
Code taken from: Lightweight Transactions in Cassandra 2.0
You can also use data types that are designed for uniqueness like UUID or TIMEUUID, and generate a new one on each INSERT.
There is a "IF NOT EXISTS" clause on inserts that uses light weight transactions. http://docs.datastax.com/en/cql/3.3/cql/cql_using/useInsertLWT.html

DynamoDB: Store array of IDs and do a batchGet for each ID

In DynamoDB, I have a Groups table and a Users table. One or many Users can belong to a Group.
Using DynamoDB, is it possible to perform one query to get a single Group by ID, and also all of the Users in that Group by the User IDs in that Group record?
If not, what is the most efficient way to do this?
No, you cannot do JOINs in NoSQL databases. The way you can do is to retrieve your group. read all the userIds. And then use either batchGet or query/scan(if its primary index) using "IN" operator

Resources