[
https://issues.apache.org/jira/browse/GEODE-3195?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16221413#comment-16221413
]
ASF GitHub Bot commented on GEODE-3195:
---------------------------------------
upthewaterspout closed pull request #15: GEODE-3195 Add querying example to the
geode-examples
URL: https://github.com/apache/geode-examples/pull/15
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/queries/README.md b/queries/README.md
new file mode 100644
index 0000000..c87241d
--- /dev/null
+++ b/queries/README.md
@@ -0,0 +1,104 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+# Geode Querying Example
+
+This example demonstrates simple queries on a region.
+
+In this example, two servers host a single partitioned region with entries
+that represent employee information.
+The example does queries through the API and presents example queries
+to be invoked through the `gfsh` command-line interface.
+
+This example assumes that Java and Geode are installed.
+
+## Set up the cluster
+1. Set directory ```geode-examples/queries``` to be the
+current working directory.
+Each step in this example specifies paths relative to that directory.
+
+2. Build the example (with the `EmployeeData` class)
+
+ $ ../gradlew build
+
+
+4. Run a script that starts a locator and two servers,
+and then creates the ```example-region``` region.
+
+ $ ../gradlew start
+
+## Run the example program
+1. Run the example to populate `example-region` with employee information,
+print out the region information,
+and then programmatically invoke three queries,
+printing the results of each query.
+
+ $ ../gradlew run
+
+## Issue `gfsh` commands to query the region
+
+`gfsh` can also be used to issue queries.
+
+1. If you have not already installed Geode,
+the build step will have installed a `gfsh` executable for you
+at a path relative to the current working directory
+within a versioned directory:
+
+ ../build/apache-geode-<version>/bin/gfsh
+
+ You can use this relative path to invoke gfsh by substituting
+the appropriate `<version>`.
+
+2. Start `gfsh` and connect to the cluster:
+
+ $ gfsh
+ ...
+ gfsh>connect --locator=127.0.0.1[10334]
+
+3. The quantity of entries may be observed with `gfsh`:
+
+ gfsh>describe region --name=example-region
+
+ Here are some `gfsh` queries to try on the `example-region` region.
+
+ Query for all entries in the region:
+
+ gfsh>query --query="select * from /example-region"
+
+ Query for the `email` field of all entries in the region:
+
+ gfsh>query --query="SELECT x.email FROM /example-region x"
+
+ Query for all entries that have a `lastName` field that starts
+ with the letter 'C':
+
+ gfsh>query --query="SELECT DISTINCT * FROM /example-region x WHERE
x.lastName.startsWith('C')"
+
+ Exit gfsh:
+
+ gfsh>exit
+
+## Shut down the cluster and (optionally) clean up the directory
+1. Shut down the cluster:
+
+ $ ../gradlew stop
+
+2. If desired, clean up the generated directories containing
+logs:
+
+ $ ../gradlew cleanServer
+
diff --git a/queries/scripts/start.gfsh b/queries/scripts/start.gfsh
new file mode 100644
index 0000000..5757be1
--- /dev/null
+++ b/queries/scripts/start.gfsh
@@ -0,0 +1,25 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+start locator --name=locator --bind-address=127.0.0.1
+
+start server --name=server1 --locators=127.0.0.1[10334] --server-port=0
--classpath=../build/classes/main
+start server --name=server2 --locators=127.0.0.1[10334] --server-port=0
--classpath=../build/classes/main
+
+create region --name=example-region --type=PARTITION
+
+list members
+describe region --name=example-region
diff --git a/queries/scripts/stop.gfsh b/queries/scripts/stop.gfsh
new file mode 100644
index 0000000..9281b31
--- /dev/null
+++ b/queries/scripts/stop.gfsh
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+connect --locator=127.0.0.1[10334]
+shutdown --include-locators=true
\ No newline at end of file
diff --git
a/queries/src/main/java/org/apache/geode/examples/queries/EmployeeData.java
b/queries/src/main/java/org/apache/geode/examples/queries/EmployeeData.java
new file mode 100644
index 0000000..933b512
--- /dev/null
+++ b/queries/src/main/java/org/apache/geode/examples/queries/EmployeeData.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express
+ * or implied. See the License for the specific language governing permissions
and limitations under
+ * the License.
+ */
+package org.apache.geode.examples.queries;
+
+import java.io.Serializable;
+
+public class EmployeeData implements Serializable {
+ private static final long serialVersionUID = 1L;
+
+ private String firstName;
+ private String lastName;
+ private int emplNumber;
+ private String email;
+ private int salary;
+ private int hoursPerWeek;
+
+ public EmployeeData(String firstName, String lastName, int emplNumber,
String email, int salary,
+ int hoursPerWeek) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ this.emplNumber = emplNumber;
+ this.email = email;
+ this.salary = salary;
+ this.hoursPerWeek = hoursPerWeek;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public int getEmplNumber() {
+ return emplNumber;
+ }
+
+ public String getEmail() {
+ return email;
+ }
+
+ public int getSalary() {
+ return salary;
+ }
+
+ public int getHoursPerWeek() {
+ return hoursPerWeek;
+ }
+
+ public String toString() {
+ return "EmployeeData [firstName=" + firstName + ", lastName=" + lastName +
", emplNumber="
+ + emplNumber + ", email= " + email + ", salary=" + salary + ",
hoursPerWeek=" + hoursPerWeek
+ + "]";
+ }
+}
diff --git
a/queries/src/main/java/org/apache/geode/examples/queries/Example.java
b/queries/src/main/java/org/apache/geode/examples/queries/Example.java
new file mode 100644
index 0000000..306a266
--- /dev/null
+++ b/queries/src/main/java/org/apache/geode/examples/queries/Example.java
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express
+ * or implied. See the License for the specific language governing permissions
and limitations under
+ * the License.
+ */
+package org.apache.geode.examples.queries;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.geode.cache.Region;
+import org.apache.geode.cache.client.ClientCache;
+import org.apache.geode.cache.client.ClientCacheFactory;
+import org.apache.geode.cache.client.ClientRegionShortcut;
+import org.apache.geode.cache.query.FunctionDomainException;
+import org.apache.geode.cache.query.NameResolutionException;
+import org.apache.geode.cache.query.QueryInvocationTargetException;
+import org.apache.geode.cache.query.QueryService;
+import org.apache.geode.cache.query.SelectResults;
+import org.apache.geode.cache.query.TypeMismatchException;
+
+
+public class Example {
+ static String REGIONNAME = "example-region";
+ static String QUERY1 = "SELECT DISTINCT * FROM /" + REGIONNAME;
+ static String QUERY2 = "SELECT DISTINCT * FROM /" + REGIONNAME + " h WHERE
h.hoursPerWeek < 40";
+ static String QUERY3 = "SELECT DISTINCT * FROM /" + REGIONNAME + " x WHERE
x.lastName=$1";
+
+ public static void main(String[] args) throws NameResolutionException,
TypeMismatchException,
+ QueryInvocationTargetException, FunctionDomainException {
+ // connect to the locator using default port 10334
+ ClientCache cache = new ClientCacheFactory().addPoolLocator("127.0.0.1",
10334)
+ .set("log-level", "WARN").create();
+
+ // create a region on the server
+ Region<Integer, EmployeeData> region =
+ cache.<Integer,
EmployeeData>createClientRegionFactory(ClientRegionShortcut.PROXY)
+ .create(REGIONNAME);
+
+ // create a set of employee data and put it into the region
+ Map<Integer, EmployeeData> employees = createEmployeeData();
+ region.putAll(employees);
+
+ // count the values in the region
+ int inserted = region.keySetOnServer().size();
+ System.out.println(String.format("Counted %d keys in region %s", inserted,
region.getName()));
+
+ // fetch and print all values in the region (without using a query)
+ region.keySetOnServer().forEach(key ->
System.out.println(region.get(key)));
+
+ // do a set of queries, printing the results of each query
+ doQueries(cache);
+
+ cache.close();
+ }
+
+
+ public static Map<Integer, EmployeeData> createEmployeeData() {
+ String[] firstNames =
+
"Alex,Bertie,Kris,Dale,Frankie,Jamie,Morgan,Pat,Ricky,Taylor,Casey,Jessie,Ryan,Skyler"
+ .split(",");
+ String[] lastNames =
+
"Able,Bell,Call,Driver,Forth,Jive,Minnow,Puts,Reliable,Tack,Catch,Jam,Redo,Skip".split(",");
+ int salaries[] = new int[] {60000, 80000, 75000, 90000, 100000};
+ int hours[] = new int[] {40, 40, 40, 40, 30, 20};
+ int emplNumber = 10000;
+
+ // put data into the hashmap
+ Map<Integer, EmployeeData> employees = new HashMap<Integer,
EmployeeData>();
+ for (int index = 0; index < firstNames.length; index++) {
+ emplNumber = emplNumber + index;
+ String email = firstNames[index] + "." + lastNames[index] +
"@example.com";
+ int salary = salaries[index % 5];
+ int hoursPerWeek = hours[index % 6];
+ EmployeeData value = new EmployeeData(firstNames[index],
lastNames[index], emplNumber, email,
+ salary, hoursPerWeek);
+ employees.put(emplNumber, value);
+ }
+
+ return employees;
+ }
+
+
+ // Demonstrate querying using the API by doing 3 queries.
+ public static void doQueries(ClientCache cache) throws
NameResolutionException,
+ TypeMismatchException, QueryInvocationTargetException,
FunctionDomainException {
+ QueryService queryService = cache.getQueryService();
+
+ // Query for every entry in the region, and print query results.
+ System.out.println("\nExecuting query: " + QUERY1);
+ SelectResults<EmployeeData> results =
+ (SelectResults<EmployeeData>) queryService.newQuery(QUERY1).execute();
+ printSetOfEmployees(results);
+
+ // Query for all part time employees, and print query results.
+ System.out.println("\nExecuting query: " + QUERY2);
+ results = (SelectResults<EmployeeData>)
queryService.newQuery(QUERY2).execute();
+ printSetOfEmployees(results);
+
+ // Query for last name of Jive, and print the full name and employee
number.
+ System.out.println("\nExecuting query: " + QUERY3);
+ results =
+ (SelectResults<EmployeeData>)
queryService.newQuery(QUERY3).execute(new String[] {"Jive"});
+ for (EmployeeData eachEmployee : results) {
+ System.out.println(String.format("Employee %s %s has employee number %d",
+ eachEmployee.getFirstName(), eachEmployee.getLastName(),
eachEmployee.getEmplNumber()));
+ }
+ }
+
+ private static void printSetOfEmployees(SelectResults<EmployeeData> results)
{
+ System.out.println("Query returned " + results.size() + " results.");
+ for (EmployeeData eachEmployee : results) {
+ System.out.println(String.format("Employee: %s",
eachEmployee.toString()));
+ }
+ }
+}
diff --git
a/queries/src/test/java/org/apache/geode/examples/queries/ExampleTest.java
b/queries/src/test/java/org/apache/geode/examples/queries/ExampleTest.java
new file mode 100644
index 0000000..14cb786
--- /dev/null
+++ b/queries/src/test/java/org/apache/geode/examples/queries/ExampleTest.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express
+ * or implied. See the License for the specific language governing permissions
and limitations under
+ * the License.
+ */
+package org.apache.geode.examples.queries;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import java.util.Map;
+
+public class ExampleTest {
+
+ @Test
+ public void testCreateEmployeeData() {
+ Map<Integer, EmployeeData> data = Example.createEmployeeData();
+ assertEquals(14, data.size());
+ }
+}
diff --git a/settings.gradle b/settings.gradle
index de9db34..2c89886 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -18,6 +18,7 @@ rootProject.name = 'geode-examples'
include 'replicated'
include 'partitioned'
+include 'queries'
include 'lucene'
include 'loader'
include 'putall'
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Create new geode-example about querying
> ---------------------------------------
>
> Key: GEODE-3195
> URL: https://issues.apache.org/jira/browse/GEODE-3195
> Project: Geode
> Issue Type: New Feature
> Components: querying
> Reporter: Karen Smoler Miller
> Assignee: Karen Smoler Miller
>
> The more examples, the better. . .
> Create an example that demonstrates how to do some simple OQL queries.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)