Repository: geode-examples
Updated Branches:
  refs/heads/develop 6055021d7 -> 500cc51a6


GEODE-3202 Adding new lucene example.
This closes #10


Project: http://git-wip-us.apache.org/repos/asf/geode-examples/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode-examples/commit/500cc51a
Tree: http://git-wip-us.apache.org/repos/asf/geode-examples/tree/500cc51a
Diff: http://git-wip-us.apache.org/repos/asf/geode-examples/diff/500cc51a

Branch: refs/heads/develop
Commit: 500cc51a6b899a380603f2ab822b8a6bada97e41
Parents: 6055021
Author: Diane Hardman <dhard...@pivotal.io>
Authored: Mon Jul 3 15:04:46 2017 -0700
Committer: zhouxh <gz...@pivotal.io>
Committed: Mon Jul 17 14:29:21 2017 -0700

----------------------------------------------------------------------
 lucene/README.md                                | 90 ++++++++++++++++++++
 lucene/scripts/start.gfsh                       | 31 +++++++
 lucene/scripts/stop.gfsh                        | 18 ++++
 .../geode/examples/lucene/EmployeeData.java     | 69 +++++++++++++++
 .../apache/geode/examples/lucene/Example.java   | 68 +++++++++++++++
 .../geode/examples/lucene/ExampleTest.java      | 38 +++++++++
 replicated/scripts/start.gfsh                   |  4 +-
 settings.gradle                                 |  1 +
 8 files changed, 318 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode-examples/blob/500cc51a/lucene/README.md
----------------------------------------------------------------------
diff --git a/lucene/README.md b/lucene/README.md
new file mode 100644
index 0000000..6f30610
--- /dev/null
+++ b/lucene/README.md
@@ -0,0 +1,90 @@
+<!--
+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 Lucene Index Example
+
+This example demonstrates the use of a simple Lucene index. Lucene provides
+a powerful text search and analysis. 
+
+In this example, two servers host a single partitioned region with entries
+that represent employee information. The example indexes the first and last
+names of employees.
+
+This example assumes that Java and Geode are installed.
+
+## Set up the Lucene index and region
+1. Set directory ```geode-examples/lucene``` to be the
+current working directory.
+Each step in this example specifies paths relative to that directory.
+
+2. Build the example (with `EmployeeData` class)
+
+        $ ../gradlew build
+
+3. Run a script that starts a locator and two servers, creates a Lucene index
+called ```simpleIndex```, and then creates the ```example-region``` region.
+A Lucene index must be created before creating the region.
+
+        $ gfsh run --file=scripts/start.gfsh
+
+4. Run the example to populate both the Lucene index and `example-region`. The 
data
+will also be retrieved from the region and printed to the console.
+
+        $ ../gradlew run
+
+## Try ```gfsh``` commands that interact with the region and do Lucene searches
+1. Run a `gfsh` command to see the contents of the region
+
+        $ gfsh
+        ...
+        gfsh>connect --locators=127.0.0.1[10334]
+        gfsh>query --query="select * from /example-region"
+        ...
+
+2. Try different Lucene searches for data in example-region
+
+        gfsh> list lucene indexes
+
+    Note that each server that holds partitioned data for this region has both 
the ```simpleIndex``` and the ```analyzerIndex```. Each Lucene index is stored 
as a co-located region with the partitioned data region.
+
+     // Search for an exact name match
+        gfsh>search lucene --name=simpleIndex --region=example-region 
--queryStrings="Jive" --defaultField=lastName
+
+     // Search for last name using fuzzy logic: sounds like 'chive'
+        gfsh>search lucene --name=simpleIndex --region=example-region 
--queryStrings="chive~" --defaultField=lastName
+
+     // Do a compound search on first and last name using fuzzy sounds like 
logic
+        gfsh>search lucene --name=simpleIndex --region=example-region 
--queryStrings="firstName:cat~ OR lastName:chive~" --defaultField=lastName
+
+     // Do a compound search on last name and email using analyzerIndex
+        gfsh>search lucene --name=analyzerIndex --region=example-region 
--queryStrings="lastName:hall~ AND email:kris.c...@example.com" 
--defaultField=lastName
+
+3. Examine the Lucene index statistics
+
+        gfsh>describe lucene index --name=simpleIndex --region=example-region
+
+    Note the statistic show the fields that are indexed and the Lucene 
analyzer used for each field. In the next example we will specify a different 
Lucene analyzer for each field. Additional statistics listed are the number of 
documents (region entries) indexed, number of entries committed as well as the 
number of queries executed for each Lucene index.
+
+4. Exit gfsh and shut down the cluster
+
+        gfsh>exit
+        $ gfsh run --file=scripts/stop.gfsh
+
+5. Clean up any generated directories and files so this example can be rerun.
+    
+        $ ../gradlew cleanServer
+

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/500cc51a/lucene/scripts/start.gfsh
----------------------------------------------------------------------
diff --git a/lucene/scripts/start.gfsh b/lucene/scripts/start.gfsh
new file mode 100644
index 0000000..ac8356d
--- /dev/null
+++ b/lucene/scripts/start.gfsh
@@ -0,0 +1,31 @@
+#
+# 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 --enable-time-statistics 
--statistic-archive-file=lucene1.gfs
+start server --name=server2 --locators=127.0.0.1[10334] --server-port=0 
--classpath=../build/classes/main --enable-time-statistics 
--statistic-archive-file=lucene2.gfs
+
+## simpleIndex uses default Lucene StandardAnalyzer
+create lucene index --name=simpleIndex --region=example-region 
--field=firstName,lastName
+
+## analyzerIndex uses both the default StandardAnalyzer and the KeywordAnalyzer
+create lucene index --name=analyzerIndex --region=example-region 
--field=lastName,email 
--analyzer=DEFAULT,org.apache.lucene.analysis.core.KeywordAnalyzer
+
+create region --name=example-region --type=PARTITION --enable-statistics=true
+
+list members
+describe region --name=example-region

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/500cc51a/lucene/scripts/stop.gfsh
----------------------------------------------------------------------
diff --git a/lucene/scripts/stop.gfsh b/lucene/scripts/stop.gfsh
new file mode 100644
index 0000000..9281b31
--- /dev/null
+++ b/lucene/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

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/500cc51a/lucene/src/main/java/org/apache/geode/examples/lucene/EmployeeData.java
----------------------------------------------------------------------
diff --git 
a/lucene/src/main/java/org/apache/geode/examples/lucene/EmployeeData.java 
b/lucene/src/main/java/org/apache/geode/examples/lucene/EmployeeData.java
new file mode 100644
index 0000000..ce7c899
--- /dev/null
+++ b/lucene/src/main/java/org/apache/geode/examples/lucene/EmployeeData.java
@@ -0,0 +1,69 @@
+/*
+ * 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.lucene;
+
+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;
+  }
+
+  @Override
+  public String toString() {
+    return "EmployeeData [firstName=" + firstName + ", lastName=" + lastName + 
", emplNumber="
+        + emplNumber + ", email= " + email + ", salary=" + salary + ", 
hoursPerWeek=" + hoursPerWeek
+        + "]";
+  }
+}

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/500cc51a/lucene/src/main/java/org/apache/geode/examples/lucene/Example.java
----------------------------------------------------------------------
diff --git a/lucene/src/main/java/org/apache/geode/examples/lucene/Example.java 
b/lucene/src/main/java/org/apache/geode/examples/lucene/Example.java
new file mode 100644
index 0000000..84cfc4b
--- /dev/null
+++ b/lucene/src/main/java/org/apache/geode/examples/lucene/Example.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.lucene;
+
+import java.util.Arrays;
+import java.util.Random;
+import java.util.function.Consumer;
+
+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;
+
+public class Example implements Consumer<Region<Integer, EmployeeData>> {
+  public static void main(String[] args) {
+    // 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 local region that matches the server region
+    Region<Integer, EmployeeData> region =
+        cache.<Integer, 
EmployeeData>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
+            .create("example-region");
+
+    new Example().accept(region);
+    cache.close();
+  }
+
+  @Override
+  public void accept(Region<Integer, EmployeeData> region) {
+    // insert values into the region
+    String[] firstNames = 
"Alex,Bertie,Kris,Dale,Frankie,Jamie,Morgan,Pat,Ricky,Taylor".split(",");
+    String[] lastNames = 
"Able,Bell,Call,Driver,Forth,Jive,Minnow,Puts,Reliable,Tack".split(",");
+    int salaries[] = new int[] {60000, 80000, 75000, 90000, 100000};
+    int hours[] = new int[] {40, 40, 40, 30, 20};
+    int emplNumber = 10000;
+    for (int index = 0; index < firstNames.length; index++) {
+      emplNumber = emplNumber + index;
+      Integer key = emplNumber;
+      String email = firstNames[index] + "." + lastNames[index] + 
"@example.com";
+      // Generating random number between 0 and 100000 for salary
+      int salary = salaries[index % 5];
+      int hoursPerWeek = hours[index % 5];
+      EmployeeData val = new EmployeeData(firstNames[index], lastNames[index], 
emplNumber, email,
+          salary, hoursPerWeek);
+      region.put(key, val);
+    }
+
+    // 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 the values in the region
+    region.keySetOnServer().forEach(key -> 
System.out.println(region.get(key)));
+  }
+}

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/500cc51a/lucene/src/test/java/org/apache/geode/examples/lucene/ExampleTest.java
----------------------------------------------------------------------
diff --git 
a/lucene/src/test/java/org/apache/geode/examples/lucene/ExampleTest.java 
b/lucene/src/test/java/org/apache/geode/examples/lucene/ExampleTest.java
new file mode 100644
index 0000000..c3e1013
--- /dev/null
+++ b/lucene/src/test/java/org/apache/geode/examples/lucene/ExampleTest.java
@@ -0,0 +1,38 @@
+/*
+ * 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.lucene;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.geode.cache.Region;
+import org.geode.examples.util.Mocks;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.contrib.java.lang.system.SystemOutRule;
+
+public class ExampleTest {
+
+  @Rule
+  public SystemOutRule systemOutRule = new SystemOutRule().enableLog();
+
+  @Test
+  public void testExample() throws Exception {
+    Region<Integer, EmployeeData> region = Mocks.region("example-region");
+    new Example().accept(region);
+
+    assertThat(systemOutRule.getLog()).contains("Counted 10 keys in region");
+    assertThat(systemOutRule.getLog()).contains("Jamie");
+  }
+}

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/500cc51a/replicated/scripts/start.gfsh
----------------------------------------------------------------------
diff --git a/replicated/scripts/start.gfsh b/replicated/scripts/start.gfsh
index acc58a5..88eb2a5 100644
--- a/replicated/scripts/start.gfsh
+++ b/replicated/scripts/start.gfsh
@@ -19,7 +19,9 @@ start locator --name=locator --bind-address=127.0.0.1
 start server --name=server1 --locators=127.0.0.1[10334] --server-port=0
 start server --name=server2 --locators=127.0.0.1[10334] --server-port=0
 
+#connect
+
 create region --name=example-region --type=REPLICATE
 
 list members
-describe region --name=example-region
\ No newline at end of file
+describe region --name=example-region

http://git-wip-us.apache.org/repos/asf/geode-examples/blob/500cc51a/settings.gradle
----------------------------------------------------------------------
diff --git a/settings.gradle b/settings.gradle
index 7c07f23..3604a11 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -18,4 +18,5 @@ rootProject.name = 'geode-examples'
 
 include 'replicated'
 include 'partitioned'
+include 'lucene'
 include 'loader'

Reply via email to