You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@geode.apache.org by John Blum <jb...@pivotal.io> on 2016/07/13 00:57:39 UTC
How to execute a Partition Region join in a Function context...
I have 2 PRs (Customers & Contacts) and I am running a query to find all
customers with contact information. I have collocated both Customers and
Contacts. Initially, I tried to run the query outside a GemFire Function,
which failed with...
java.lang.UnsupportedOperationException: *A query on a Partitioned Region (
Customers ) may not reference any other region if query is NOT executed
within a Function*
at
com.gemstone.gemfire.cache.query.internal.DefaultQuery.checkQueryOnPR(DefaultQuery.java:638)
at
com.gemstone.gemfire.cache.query.internal.DefaultQuery.execute(DefaultQuery.java:351)
at
org.springframework.data.gemfire.GemfireTemplate.find(GemfireTemplate.java:299)
So, then I proceeded to attempt to execute my query...
SELECT DISTINCT customer
FROM /Customers customer, /Contacts contact"
WHERE customer.firstName = contact.person.firstName
AND customer.lastName = contact.person.lastName
... in a Function as instructed, however, I ended up with the same
Exception.
I am trying to figure out how to properly execute a JOIN on 2 "collocated"
PRs from within a Function, given the FunctionContext (or possibly the
local data set of the PR using RegionFunctionContext.getDataSet(), or
something like that).
Any help is appreciated.
Thanks,
-John
Re: How to execute a Partition Region join in a Function context...
Posted by John Blum <jb...@pivotal.io>.
Note, for everyone's reference, I found the following link in a Google
search which helped me figure out my collocated PRs, Equi-Join Query
problem...
https://pubs.vmware.com/vfabric5/index.jsp?topic=/com.vmware.vfabric.gemfire.6.6/developing/partitioned_regions/join_query_partitioned_regions.html
For *Pivotal GemFire*, the reference is here...
http://gemfire.docs.pivotal.io/docs-gemfire/latest/developing/partitioned_regions/join_query_partitioned_regions.html
And for *Apache Geode*, the reference is here...
http://geode.docs.pivotal.io/docs/developing/partitioned_regions/join_query_partitioned_regions.html
I have to say that the key to properly performing an Equi-Join Query on
collocated PRs (inside a Function, no-less), given the following code
snippet from the documentation, was not very obvious...
SelectResults result = (SelectResults)*query
.execute((RegionFunctionContext)context)*;
Thanks,
John
On Tue, Jul 12, 2016 at 6:54 PM, John Blum <jb...@pivotal.io> wrote:
> Thanks for the fast response Dan.
>
> Unfortunately, that did not work, given the following...
>
> protected static final String CUSTOMERS_WITH_CONTACTS_QUERY =
> "SELECT DISTINCT customer FROM /Customers customer, /Contacts contact"
> + " WHERE customer.firstName = contact.person.firstName AND
> customer.lastName = contact.person.lastName";
>
> @GemfireFunction
> public List<Customer>
> findAllCustomersWithContactInformation(FunctionContext functionContext) {
> return executeQueryOnRegion(toRegionFunctionContext(functionContext));
> }
>
> protected List<Customer> executeQueryOnRegion(RegionFunctionContext
> regionFunctionContext) {
> return executeQueryOnRegion(*PartitionRegionHelper.*
> *getLocalDataForContext**(regionFunctionContext)*);
> }
>
> @SuppressWarnings("unchecked")
> protected List<Customer> executeQueryOnRegion(Region<Long, Customer>
> customers) {
> try {
> QueryService queryService =
> customers.getRegionService().getQueryService();
> Query query = queryService.newQuery(CUSTOMERS_WITH_CONTACTS_QUERY);
> Object results = query.execute();
> Assert.isInstanceOf(SelectResults.class, results);
> return ((SelectResults<Customer>) results).asList();
> }
> catch (Exception e) {
> throw new RuntimeException("?", e);
> }
> }
>
> However, the following does work...
>
> @SuppressWarnings("unchecked")
> protected List<Customer> executeQueryInFunctionContext(RegionFunctionContext
> *functionContext*) {
> try {
> QueryService queryService =
> functionContext.getDataSet().getRegionService().getQueryService();
> Query query = queryService.newQuery(CUSTOMERS_WITH_CONTACTS_QUERY);
> Object results = *query.execute(functionContext);*
> Assert.isInstanceOf(SelectResults.class, results);
> return ((SelectResults<Customer>) results).asList();
> }
> catch (Exception e) {
> throw new RuntimeException("?", e);
> }
> }
>
> This would probably make for a good example in the *Apache Geode*
> examples, sometime, ;-). I can contribute this after SpringOne.
>
> Thanks again,
> John
>
>
> On Tue, Jul 12, 2016 at 6:03 PM, Dan Smith <ds...@pivotal.io> wrote:
>
>> I'm not sure, but I think you might need to use the query method on the
>> Region returned from PartitionRegionHelper.getLocalDataForContext.
>>
>> -Dan
>>
>> On Tue, Jul 12, 2016 at 5:57 PM, John Blum <jb...@pivotal.io> wrote:
>>
>> > I have 2 PRs (Customers & Contacts) and I am running a query to find all
>> > customers with contact information. I have collocated both Customers
>> and
>> > Contacts. Initially, I tried to run the query outside a GemFire
>> Function,
>> > which failed with...
>> >
>> > java.lang.UnsupportedOperationException: *A query on a Partitioned
>> Region (
>> > Customers ) may not reference any other region if query is NOT executed
>> > within a Function*
>> > at
>> >
>> >
>> com.gemstone.gemfire.cache.query.internal.DefaultQuery.checkQueryOnPR(DefaultQuery.java:638)
>> > at
>> >
>> >
>> com.gemstone.gemfire.cache.query.internal.DefaultQuery.execute(DefaultQuery.java:351)
>> > at
>> >
>> >
>> org.springframework.data.gemfire.GemfireTemplate.find(GemfireTemplate.java:299)
>> >
>> > So, then I proceeded to attempt to execute my query...
>> >
>> > SELECT DISTINCT customer
>> > FROM /Customers customer, /Contacts contact"
>> > WHERE customer.firstName = contact.person.firstName
>> > AND customer.lastName = contact.person.lastName
>> >
>> > ... in a Function as instructed, however, I ended up with the same
>> > Exception.
>> >
>> > I am trying to figure out how to properly execute a JOIN on 2
>> "collocated"
>> > PRs from within a Function, given the FunctionContext (or possibly the
>> > local data set of the PR using RegionFunctionContext.getDataSet(), or
>> > something like that).
>> >
>> > Any help is appreciated.
>> >
>> > Thanks,
>> >
>> > -John
>> >
>>
>
>
>
> --
> -John
> 503-504-8657
> john.blum10101 (skype)
>
--
-John
503-504-8657
john.blum10101 (skype)
Re: How to execute a Partition Region join in a Function context...
Posted by John Blum <jb...@pivotal.io>.
Thanks for the fast response Dan.
Unfortunately, that did not work, given the following...
protected static final String CUSTOMERS_WITH_CONTACTS_QUERY =
"SELECT DISTINCT customer FROM /Customers customer, /Contacts contact"
+ " WHERE customer.firstName = contact.person.firstName AND
customer.lastName = contact.person.lastName";
@GemfireFunction
public List<Customer>
findAllCustomersWithContactInformation(FunctionContext functionContext) {
return executeQueryOnRegion(toRegionFunctionContext(functionContext));
}
protected List<Customer> executeQueryOnRegion(RegionFunctionContext
regionFunctionContext) {
return executeQueryOnRegion(*PartitionRegionHelper.*
*getLocalDataForContext**(regionFunctionContext)*);
}
@SuppressWarnings("unchecked")
protected List<Customer> executeQueryOnRegion(Region<Long, Customer>
customers) {
try {
QueryService queryService =
customers.getRegionService().getQueryService();
Query query = queryService.newQuery(CUSTOMERS_WITH_CONTACTS_QUERY);
Object results = query.execute();
Assert.isInstanceOf(SelectResults.class, results);
return ((SelectResults<Customer>) results).asList();
}
catch (Exception e) {
throw new RuntimeException("?", e);
}
}
However, the following does work...
@SuppressWarnings("unchecked")
protected List<Customer> executeQueryInFunctionContext(RegionFunctionContext
*functionContext*) {
try {
QueryService queryService =
functionContext.getDataSet().getRegionService().getQueryService();
Query query = queryService.newQuery(CUSTOMERS_WITH_CONTACTS_QUERY);
Object results = *query.execute(functionContext);*
Assert.isInstanceOf(SelectResults.class, results);
return ((SelectResults<Customer>) results).asList();
}
catch (Exception e) {
throw new RuntimeException("?", e);
}
}
This would probably make for a good example in the *Apache Geode* examples,
sometime, ;-). I can contribute this after SpringOne.
Thanks again,
John
On Tue, Jul 12, 2016 at 6:03 PM, Dan Smith <ds...@pivotal.io> wrote:
> I'm not sure, but I think you might need to use the query method on the
> Region returned from PartitionRegionHelper.getLocalDataForContext.
>
> -Dan
>
> On Tue, Jul 12, 2016 at 5:57 PM, John Blum <jb...@pivotal.io> wrote:
>
> > I have 2 PRs (Customers & Contacts) and I am running a query to find all
> > customers with contact information. I have collocated both Customers and
> > Contacts. Initially, I tried to run the query outside a GemFire
> Function,
> > which failed with...
> >
> > java.lang.UnsupportedOperationException: *A query on a Partitioned
> Region (
> > Customers ) may not reference any other region if query is NOT executed
> > within a Function*
> > at
> >
> >
> com.gemstone.gemfire.cache.query.internal.DefaultQuery.checkQueryOnPR(DefaultQuery.java:638)
> > at
> >
> >
> com.gemstone.gemfire.cache.query.internal.DefaultQuery.execute(DefaultQuery.java:351)
> > at
> >
> >
> org.springframework.data.gemfire.GemfireTemplate.find(GemfireTemplate.java:299)
> >
> > So, then I proceeded to attempt to execute my query...
> >
> > SELECT DISTINCT customer
> > FROM /Customers customer, /Contacts contact"
> > WHERE customer.firstName = contact.person.firstName
> > AND customer.lastName = contact.person.lastName
> >
> > ... in a Function as instructed, however, I ended up with the same
> > Exception.
> >
> > I am trying to figure out how to properly execute a JOIN on 2
> "collocated"
> > PRs from within a Function, given the FunctionContext (or possibly the
> > local data set of the PR using RegionFunctionContext.getDataSet(), or
> > something like that).
> >
> > Any help is appreciated.
> >
> > Thanks,
> >
> > -John
> >
>
--
-John
503-504-8657
john.blum10101 (skype)
Re: How to execute a Partition Region join in a Function context...
Posted by Dan Smith <ds...@pivotal.io>.
I'm not sure, but I think you might need to use the query method on the
Region returned from PartitionRegionHelper.getLocalDataForContext.
-Dan
On Tue, Jul 12, 2016 at 5:57 PM, John Blum <jb...@pivotal.io> wrote:
> I have 2 PRs (Customers & Contacts) and I am running a query to find all
> customers with contact information. I have collocated both Customers and
> Contacts. Initially, I tried to run the query outside a GemFire Function,
> which failed with...
>
> java.lang.UnsupportedOperationException: *A query on a Partitioned Region (
> Customers ) may not reference any other region if query is NOT executed
> within a Function*
> at
>
> com.gemstone.gemfire.cache.query.internal.DefaultQuery.checkQueryOnPR(DefaultQuery.java:638)
> at
>
> com.gemstone.gemfire.cache.query.internal.DefaultQuery.execute(DefaultQuery.java:351)
> at
>
> org.springframework.data.gemfire.GemfireTemplate.find(GemfireTemplate.java:299)
>
> So, then I proceeded to attempt to execute my query...
>
> SELECT DISTINCT customer
> FROM /Customers customer, /Contacts contact"
> WHERE customer.firstName = contact.person.firstName
> AND customer.lastName = contact.person.lastName
>
> ... in a Function as instructed, however, I ended up with the same
> Exception.
>
> I am trying to figure out how to properly execute a JOIN on 2 "collocated"
> PRs from within a Function, given the FunctionContext (or possibly the
> local data set of the PR using RegionFunctionContext.getDataSet(), or
> something like that).
>
> Any help is appreciated.
>
> Thanks,
>
> -John
>