how to compare attributes in hazelcast with predicates - hazelcast

i have an object "ResponseSerializablePlus"
the object has a few attributes
private int id;
private String cod_nrbe_en;
private int num_sec_ac
this field can bring null values so i need to capture all the registers about this field, you can have id or another field duplicated more than once.
what i was thinking was building a query
Predicate sqlQuery = Predicates.sql("i dont know what to put here");
Predicate criteriaQuery = Predicates.and(
Predicates.equal("id", id),
Predicates.equal("code", cod_nrbe_en)
Predicates.equal("num", num_sec_ac)
);
you can have just 1 value and other fields are null or another case the thing is i dont know how to query or compare those 3 attributes
¿any hint?

Related

Query the hazelcast data having inner object arrays using predicate

I have a user Object, Which has inner Object of type Address. Below are the structures of both User and Address.
User{
String name;
String id;
String phoneNumber;
List<Address> address
}
Address{
String type;
String streetName;
String houseNumber;
String Country;
int pin
}
I am storing the User Objects to the Hazelcast Cache. I would like to query Users whose address type is "Primary" and Country is "US".
The problem I am seeing is that each user can have multiple addresses, How to loop through the address and find the one with type "Primary" and for that particular address type how to query the Country "US". Can we use predicate to achieve this? If so, Please help me with how the predicate can be constructed.
Please check Querying in Collections and Arrays in the Hazelcast documentation.
In your case, you would like to have two conditions on the dependent collection, like:
Predicates.equal("address[any].country", "US")
Predicates.equal("address[any].type", "Primary")
But any should apply to the same entity (because you'd like to have US as Primary country). I don't think you can achieve it with just Predicates.
What you can do, however, is to use Custom Attributes and define your own ValueExtractor which would cover the logic you need.

Find distinct values from column of datatable in c# and store distinct values in variables

i want to find distinct values from a column of datatable in c# and also want to store all these distinct values in variables
DataTable dtable = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * from clubbb ", con);
da.Fill(dtable, "clubbb");
int toto = bdtable.AsEnumerable().Distinct().Count();
You need to implement the interface IEqualityComparer<DataRow> methods to provide the way the Distinct method will use to know whether 2 rows are duplicate or not. This can be achieved with a class like this:
public class CustomComparer : IEqualityComparer<DataRow>
{
public bool Equals(DataRow x, DataRow y)
{
// your custom equality logic here
}
public int GetHashCode(DataRow obj)
{
// return hash code depending on your distinct criteria
}
}
Then change the call you made:
int toto = bdtable.AsEnumerable().Distinct(new CustomComparer()).Count();
I had similar problem, and in my case I needed to know distinct values of specific column, so I passed the name of this column to the custom comparer I implemented, so that GetHashCode will return identical hash codes for duplicate values.
You can read more here: https://msdn.microsoft.com/en-us/library/ms132151(v=vs.110).aspx
Hope this helps :)

Hazelcast - query collections of Map values

Assume I have the following as the value in an IMap:
public class Employee{
public int empId;
public List<String> categories;
public List<String> getCategories(){
return this.categories;
}
}
I would like to find all employees that belong to category "Sales". Also, I would like to create an index on getCategories() so that the query returns fast. There seems to be no Predicate available to do this. How do I go about achieving this? It seems like I will have to write a Predicate to do this. Is there example code I can look at that would show me how to build a predicate that uses an index ?
The only way I currently see this happening is to denormalize the data model and use something like a IMap and the following as value:
class EmployeeCategory{int employeeId, String category}
And put an index on category.
It is somewhere on the backlog to provide more advances indices that should be able to do this out of the box.
I tried by iterating the List to a separate Imap and then querying it in the client.
IMap<String,ArrayList< categories >> cache=hazelcastInstance.getMap("cache");
IMap<String, categories> cachemodified = hazelcastInstance.getMap("cachemodified") ;
int[] idx = { 0 };
xref.get("urkey").forEach(cachefelement ->{
cachemodified.put(String.valueOf(idx[0]++),cachefelement);
});
Predicate p = Predicates.equal("categoryId", "SearchValue");
Collection<categories> result = cachemodified.values(p);

How to get count of a generic list in C# and no need to consider items with null or empty values in the count?

I need to get the count of a generic list in C#.No need to consider empty and null items in the count of list.
Below is my code
Public class Student
{
public string Name {get;set;}
public string Age {get;set;}
}
List<student> listStudent = new List<student>();
Student studentOne=new Student();
studentOne.Name="abc";
studentOne.Age="20";
listStudent.Add(studentOne);
Student studentTwo=new Student();
studentOne.Name="def";
studentOne.Age="22";
listStudent.Add(studentTwo);
Student studentThree=new Student();
studentOne.Name=" ";
studentOne.Age=null;
listStudent.Add(studentThree);
I have written below code to get the count
listStudent.count
It returns 3.it is correct as it contains 3 rows in the list.But I need to get the count of elements or items only having values. here in my code values in last item is null or empty.so I need to get count as 2.
Is there any built in method in c# to do the same. is there any way to check without using loops ?
LINQ can help here:
listStudent.Where(
s => !string.IsNullOrWhiteSpace(s.Name) && s.Age != null
).Count();
There is no method in framework that you are looking for. You need to create your own extended method to make this.

JPQL query --- how to use 'is null'

i use the following query in JPQL to query the people whose address column is empty.
List rl =
em.createQuery( "select o from Person as o where
o.address IS NULL" ).setFirstResult(
0).setMaxResults( 50).getResultList();
...
this line of code always return an empty list, obviously the table does has entries that match the condition.
class Person {
Address address;
String name;
... }
class Address {
String name;
... }
anyone knows what's wrong with this jpql statement?
thanks in advance.
As mentioned, address column is empty, then try using IS EMPTY expression instead of IS NULL.
em.createQuery( "SELECT o FROM Person o where (o.address.id IS NULL OR o.address.id = 0").setMaxResults(50).getResultList();
Check constraint according to id's datatype.
Also there is no need to mention setFirstResult(0) as it is not going to skip any results & without it, by default all matching results will be fetched.

Resources