Esper EPL aggregation with custom function - event-handling

I am trying to do an aggregation with Esper 8.8.0 EPL using the following code. When ProductEvent is published then I am trying to save it into a table after converting the complete ProductEvent bean object into json. Is there any way I can pass ProductEven Object itself in custom function when executing merge statement -
#public create table OutputTable
(
productId string primary key
, productName string
, productJson string
);
#name('stmtUpdateOutputTable') on ProductEvent pe
merge OutputTable ot
where ot.productId = pe.productId
when not matched
then insert select productId, productName, Utils.getJson(*)
when matched
then update
set
ot.productName= pe.productName,
ot.productJson = Utils.getJson(*)
;
ProductEvent is a java bean that contains more than 100 property so it is not a good idea I pass each individual field when call custom function -
public class ProductEvent{
private String productId;
private String productName;
private Double price;
private LocalDate firstAvailableDate;
//..... around 100 more properties here
}
Utils is a helper class that contains static method -
public static String getJson(ProductEvent event) {
return new ObjectMapper().writeValueAsString(event);
}

In the on-merge, there are two aliases: "pe" for ProductEvent and "ot" for OutputTable, so "...Utils.getJson(pe)..." would work.

Related

Spring R2DBC query by example with projected/custom entity

The title maybe is not properly written but here is what, more or less, I want to achieve.
I would like to be able to write dynamic queries with use of Query by Example that would join multiple tables and create (projection?) DTO for me.
This DTO would have fields that are mapped to different columns in joined tables. Consider following:
Tables:
CREATE TABLE address
(
id SERIAL,
address_code VARCHAR(255) NOT NULL,
street_name VARCHAR(255),
building_number VARCHAR(255)
);
CREATE TABLE account
(
id SERIAL,
account_number BIGINT UNIQUE
);
CREATE TABLE customer
(
id SERIAL,
name VARCHAR(255)
)
I would like to be able to create a query which result would be:
address.address_code, account.account_number, customer.name
so basically the result would be a custom DTO. I also mentioned that I would like to have this backed up with Query by Example because I will to dynamically append WHERE clauses so I thought that if I created a DTO like:
public record CustomQueryResultDTO(String addressCode, BigInteger accountNumber, String name) {}
I could simply query just like it is in Spring R2DBC documentation.
The problem here is that I am not sure what should be a viable solution for such problem because on one hand I would like to reuse ReactiveQueryByExampleExecutor but that would mean that I have to create something like:
#Repository
public interface CustomQueryResultRepository extends ReactiveCrudRepository<CustomQueryResultDTO, Integer>, ReactiveQueryByExampleExecutor<CustomQueryResultDTO> {
}
Which kind of seems to me not a way to go as I do not have a corresponding table for CustomQueryResultDTO therefore there is really no mapping for this repository interface - or am I overthinking this and it is actually a way to go?
I think you are potentially overthinking it.
You can do it in a number of ways (note Java 17 text blocks):
Via R2DBC JPA-like #Query
Create a normal ReactiveCrudRepository but collect into a projection (DTOP)
// Repository
#Repository
public interface UserRefreshTokenRepository extends ReactiveCrudRepository<UserRefreshToken, Integer> {
#Query(
"""
select *
from user.user_refresh_tokens t
join user.user_infos c on c.user_id = t.user_id
where c.username = :username
"""
)
Flux<UserRefreshTokenDtop> findAllByUsername(String username);
}
// Entity
#Data
#Builder
#AllArgsConstructor
#NoArgsConstructor
#ToString(exclude = {"refreshToken"})
#Table(schema = "user", name = "user_refresh_tokens")
public class UserRefreshToken {
#Id private Integer id;
private String userId;
private String username; # will be joined
private String ipAddr;
private OffsetDateTime createdAt;
private String refreshToken;
private OffsetDateTime refreshTokenIat;
private OffsetDateTime refreshTokenExp;
}
// DTO projection
public interface UserRefreshTokenDtop {
Integer getId();
String getUserId();
String getUsername(); # will be joined
String getIpAddr();
OffsetDateTime getRefreshTokenIat();
OffsetDateTime getRefreshTokenExp();
}
Via DatabaseClient
This one also uses TransactionalOperator to ensure query atomicity
private final DatabaseClient client;
private final TransactionalOperator operator;
#Override
public void deleteAllUsedExpiredAttempts(Duration resetInterval) {
// language=PostgreSQL
String allUsedExpiredAttempts = """
select t.id failed_id, c.id disable_id, t.username
from user.failed_sign_attempts t
join user.disable_sign_attempts c on c.username = t.username
where c.is_used = true
and :now >= c.expires_at + interval '%d seconds'
""";
// POTENTIAL SQL injection - half-arsed but %d ensures that only Number is allowed
client
.sql(String.format(allUsedExpiredAttempts, resetInterval.getSeconds()))
.bind("now", Instant.now())
.fetch()
.all()
.flatMap(this::deleteFailed)
.flatMap(this::deleteDisabled)
.as(operator::transactional)
.subscribe(v1 -> log.debug("Successfully reset {} user(s)", v1));
}
Via R2dbcEntityTemplate
I don't have a working example but it is pain in the ass to join via the .join() operator
If you are interested check the docs for R2dbcEntityTemplate
13.4.3. Fluent API > Methods for the Criteria Class
https://docs.spring.io/spring-data/r2dbc/docs/current/reference/html

How to get last record in spring mongo?

I have a transaction class which stores the each transaction of a customer,Following are the fields in this class.
class Transaction{
#Id
private String id;
private Date date;
private String customerId;
private double openBalance;
private double transctionAmount;
private double finalAmount;
}
I need to fetch only the last inserted record of a customer (let say for customerId = cust123).
I defined following function in repository.
public interface TranscationRepository extends MongoRepository<Transaction, String> {
Optional<Transaction> findTopByCustomerIdOrderByIdDesc(String id);
}
This method giving last entry not by customerId but overall. I tried few modifications to it but did not get success.
I know I can findAllByCustomer but I don't want to pull huge list of transaction which is of no use in this use case. What is correct signature in spring mongo to get last inserted record by a field? I am ok to use custom #Query also.
Thank you.

HQL - Check if an Array contains a value

I have an array field in my first entity Class as below:
class Entity1{
private Integer col1;
private String col2;
private Integer[] col3Arr;
}
I have another entity class as below:
class Entity2{
private Integer col1;
private String col2;
private Integer col3;
}
I am fetching records by joining multiple other entities along with which I have to join Entity1 if col3Arr contains a value col3 from Entity 2
With PSQL, I could easily achieve this by following statement
//Other part of query
join Entity2 e2 on (//conditions from other joined tables//)
join Entity1 e1 on e2.col3=ANY(e1.col3Arr)
What is the HQL equivalent of ANY?
Or is there any other way in HQL to check if an array contains a specific value?
For mapping the arrays you will need a custom type. You can use the hibernate-types project for this: https://vladmihalcea.com/how-to-map-java-and-sql-arrays-with-jpa-and-hibernate/
Did you try to use e2.col3 = FUNCTION('ANY', e1.col3Arr) yet? If that doesn't work, I would suggest you create a custom SQLFunction that renders the SQL you desire e.g.
public class ArrayAny implements SQLFunction {
#Override
public boolean hasArguments() {
return true;
}
#Override
public boolean hasParenthesesIfNoArguments() {
return true;
}
#Override
public Type getReturnType(Type firstArgumentType, Mapping mapping) throws QueryException {
return firstArgumentType;
}
#Override
public String render(Type firstArgumentType, List args, SessionFactoryImplementor factory) throws QueryException {
return "any(" + args.get(0) + ")";
}
}
You will have to register the function within the Dialect.
According to the hibernate documentation:
When discussing arrays, it is important to understand the distinction between SQL array types and Java arrays that are mapped as part of the application’s domain model.
Not all databases implement the SQL-99 ARRAY type and, for this reason, Hibernate doesn’t support native database array types.
So, there is no equivalent of PSQL ANY in HQL.

Map stored procedure results to a custom complex type in Entity Framework

Consider a stored procedure GetEmployees which has a SELECT statement like
SELECT EMP_ID, EMP_NAME, EMP_EMAIL
FROM EMPLOYEE
This stored procedure will have its results mapped to a complex type GetEmployees_Result
class GetEmployees_Result {
public int EMP_ID;
public string EMP_NAME;
public string EMP_EMAIL;
}
Is it possible to map the result of the function import to a different complex type like the one below:
class GetEmployeesResult {
public int Id;
public string Name;
public string Email;
}
It is a standard feature. You have go to the mapping of the function import and change the result type to the custom type.

Mapping a stored procedure to a custom type in entity framework [duplicate]

Consider a stored procedure GetEmployees which has a SELECT statement like
SELECT EMP_ID, EMP_NAME, EMP_EMAIL
FROM EMPLOYEE
This stored procedure will have its results mapped to a complex type GetEmployees_Result
class GetEmployees_Result {
public int EMP_ID;
public string EMP_NAME;
public string EMP_EMAIL;
}
Is it possible to map the result of the function import to a different complex type like the one below:
class GetEmployeesResult {
public int Id;
public string Name;
public string Email;
}
It is a standard feature. You have go to the mapping of the function import and change the result type to the custom type.