Spring Batch JdbcPagingItemReader sort by two columns/keys - spring-batch

I have this JdbcPagingItem reader and I want to sort by 2 columns.
Map<String, Order> sortKey = new HashMap<>();
sortKey.put("xbin", Order.ASCENDING);
sortKey.put("ybin", Order.ASCENDING);
Since the sortKey is a Map, I think this should work.
I need some confirmation if this is how I am supposed to do?
thx, Markus.

Yes, you can sort items by multiple columns and your code snippet is correct. The resulting query performed by the item reader will be something like:
select ... from table .. ORDER BY xbin ASC, ybin ASC

Related

Select new list of subquery in hql

I am trying to use the subquery in the attributes section.
This works fine when the subquery returns only one result. Otherwise it shows an error.
[2019-12-30 11:24:26] [21000] ERROR: more than one row returned by a subquery used as an expression
#Query("select new com.example.rest.ShiftResponse(s.id, s.displayTitle, s.startTime, s.endTime, s.max, (select comm from ShiftGroup sg inner join sg.committee comm where sg.shift.id = s.id)) from Shift s where s.event.id = ?1")
Here's the constructor's signature
public ShiftResponse(Long id, MultilangText displayTitle, LocalDateTime startTime, LocalDateTime endTime, Integer max, List<Committee> committees) {
Is there any way other than this to specify that I want that parameter to be a List?
Have you checked this link ? HQL new List(..) within new Object(..)
It seems to me it was a similar question. Does it answer your question ?

DISTINCT on a single column

I'm using spring data JPA along with Hibernate. I have to find all entries by id, however I'm selecting only some of the columns.
I managed to do this by using specified constructor
#Query("SELECT new Foo(f.field1, f.field2, f.field3)
FROM FooTable f WHERE f.field1 = :field")
I need to make field1 as a DISTINCT, however putting this column into DISTINCT(field1) doesn't work.
Is there a way to make it work?
Try this:
#Query("SELECT new Foo(f.field1, f.field2, f.field3)
FROM FooTable f, FooTable f2
WHERE f.field1 = :field
AND f.field1 != f2.field1")
Note that it'll miss the null values.

How to sort a collection by row in MongoDB?

I am using MongoDB with hapi.JS. I have a collection which contains few rows in the schema. I want to sort the rows in either asc order or desc order but want to mention it in the URI. for example the URI should look something like this
/api/v1/customers?sort=name&direction=asc&limit=30
How can I sort this collection by asc or desc order and limit can be fixed or flexible as well.
I have defined like this as of now but even if I mention the sort in URI it gives the output only in asc order.
Models.Account.find(criteria,projection,{skip:5,limit:5},function(err,resp){
if(err)
callbackRoute(err);
else
callbackRoute(err,resp);
}).sort({[_id]:"asc"});
db.yourcollection.find(...).sort({ name:1 }).limit(30)
or with dynamic values:
// following is ECMA 6 only
// get params and make sure values are what you expect (check for injection) + direction must be = "asc" || "desc"
db.yourcollection.find(...).sort({ [sort]: direction }).limit(30)

Minus logic implementation not working with spark/scala

Minus Logic in Hive:
The below (Hive)query will return only records available in left side table ( Full_Table ft), but not in both.
Select ft.* from Full_Table ft left join Stage_Table stg where stg.primary_key1 IS null and stg.primary_key2 IS null
I tried to implement the same in spark/scala using following method ( To support both primary key and composite key ) , But joined result set does not have column from right table, because of that not able to apply stg.primary_key2 IS null condition in joined result set.
ft.join(stg,usingColumns, “left_outer”) // used seq to support composite key column join
Please suggest me how to implement minus logic in spark scala.
Thanks,
Saravanan
https://www.linkedin.com/in/saravanan303/
If your tables have the same columns you can use except method from DataSet:
fullTable.except(stageTable)
If they don't have, but you are interested only on subset of columns that exists in both tables you can first select those column using select transformation and than use except:
val fullTableSelectedColumns = fullTable.select(c1,c2,c3)
val stageTableSelectedColumns = stageTable.select(c1,c2,c3)
fullTableSelectedColumns.except(stageTableSelectedColumns)
On other case, you can use join and filter transformations:
fullTable
.join(stageTable, fullTable("primary_key") === stageTable("primary_key"), "left")
.filter(stageTable("primary_key1").isNotNull)

How can I query cli query in Cassandra by composite key?

I create the following Column Family in Cassandra:
CREATE COLUMN FAMILY test with comparator = 'CompositeType(UTF8Type,UTF8Type)' and key_validation_class=UTF8Type;
Now I want to add some data:
set test['a']['b:c'] = 'abc'
set test['a']['b:d'] = 'abd'
set test['a']['e:f'] = 'aef'
set test['a']['e:g'] = 'aeg';
Now I would like to retrieve all rows which have e in its Composite key:
something like:
get test['a']['e:*];
and result should be 'aef' and 'aeg'.
How cli query should look like?
I am not sure about CQL, but with playOrm, if you partitioned by a, you can just do S-SQL(scalable SQL) query of
PARTITIONS alias('a') SELECT alias FROM Table as alias WHERE a.column = 'e';
A partition can have millions of rows.
Anyways, just thought it might help you a bit.