I am trying to add a Trace to an OQL Query as mentioned in the docs, using GemfireTemplate query method. But the OQL validation fails with QueryInvalidException "unexpected tokern: <". Any ideas?
-
I'm not sure why the exception is being thrown, apparently the query generated by the `GemFireTemplate` is somehow invalid?. Anyways, I've been able to workaround the problem by using `GemFireTemplate.find("
SELECT * FROM /myRegion)` instead of `GemFireTemplate.query(" – Juan Ramos Sep 07 '20 at 08:37SELECT * FROM /myRegion)`. -
As a side note, you might want to use [Spring Data for Apache Geode Repositories](https://docs.spring.io/spring-data/geode/docs/2.3.3.RELEASE/reference/html/#gemfire-repositories) and the [@Trace](https://docs.spring.io/spring-data/geode/docs/2.3.3.RELEASE/reference/html/#gemfire-repositories.queries.oql-extensions) instead of the `GemFireTemplate`, is more straightforward and allows you to significantly reduce the amount of boilerplate code required to implement data access layers. – Juan Ramos Sep 07 '20 at 08:39
-
@juan - Why do you think the query generated by `GemfireTemplate` when calling the `GemfireTemplate.query(:String)` method is invalid? First of all, it doesn't "generate" any query... it passes the argument to the `query(:String)` method directly to the GemFire/Geode API. See my answer below. – John Blum Sep 08 '20 at 17:32
-
Hey @John, I didn't investigate that much into the stack trace and "invalid" was not the right word to use, sorry about that; I just saw on the logs from the server that the `queryString` didn't look right. After reading your answer, though, it's clear where the problem is; and I didn't know about the `Region.query()` method restriction, BTW, thanks for pointing that out!. – Juan Ramos Sep 09 '20 at 08:18
1 Answers
This is not a bug with SDG's GemfireTemplate
, nor a problem with SDG in general.
There are 2 ways to query in GemFire/Geode.
First, is to use the
QueryService
, which can be obtained from the cache. Alternatively, you can obtai theQueryService
from theClientCache
or even from thePool
attached to theRegion
on which you are running the OQL query. This is all handled for you automatically when using SDG's Repository abstraction extension.The second way to query a
Region
is to pass a query "PREDICATE" to theRegion.query(:String)
method.
Which GemFire/Geode API do you think the GemfireTemplate.query(:String)
method is using?
GemfireTemplate.query(:String)
uses the Region.query(:String)
API.
The GemfireTemplate.find(:String)
method uses the QueryService
.
Only the QueryService
can accept fully valid OQL queries, e.g. <TRACE> SELECT * FROM /SomeRegion WHERE id = 1
, where as the Region.query(:String)
method ONLY accepts the OQL Query PREDICATE, i.e. id = 1
.
Any other OQL Query reserved words or query syntax in general, passed to the GemfireTemplate.query(:String)
method (and by extension Region.query(:String)
API) results in a invalid OQL Query.
If you want to pass <HINT 'IDIndex', ...> <TRACE> SELECT * FROM /SomeRegion WHERE id = 1 AND ...
, then you should call GemfireTemplate.find(:String)
, which uses the GemFire/Geode QueryService
that accepts the complete OQL Query syntax.
Alternatively, you can use the Spring Data for Apache Geode (or VMware Tanzu GemFire) Repository extension.
It is even still possible to add HINTS, TRACES, LIMITS or other query facilities to derived Repository query methods as well as query methods annotated with @Query
. See the documentation for more details.

- 7,381
- 1
- 20
- 30