ignite-1351: moved portable API examples to Ignite except cross-platform example
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/6a5a48a0 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/6a5a48a0 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/6a5a48a0 Branch: refs/heads/master Commit: 6a5a48a0c9e379ecf83c8a799d0d8e2397fe5b3c Parents: c5d303b Author: Denis Magda <[email protected]> Authored: Thu Sep 3 16:59:36 2015 +0300 Committer: Denis Magda <[email protected]> Committed: Thu Sep 3 16:59:36 2015 +0300 ---------------------------------------------------------------------- examples/config/example-default.xml | 76 +++++ examples/config/example-ignite.xml | 56 +--- .../config/portable/example-ignite-portable.xml | 44 +++ .../ignite/examples/portable/Address.java | 72 +++++ .../ignite/examples/portable/Employee.java | 93 ++++++ .../ignite/examples/portable/EmployeeKey.java | 90 ++++++ .../portable/ExamplePortableNodeStartup.java | 36 +++ .../ignite/examples/portable/Organization.java | 93 ++++++ .../examples/portable/OrganizationType.java | 32 ++ ...mputeClientPortableTaskExecutionExample.java | 154 +++++++++ .../portable/computegrid/ComputeClientTask.java | 116 +++++++ .../portable/computegrid/package-info.java | 21 ++ .../CacheClientPortablePutGetExample.java | 226 +++++++++++++ .../CacheClientPortableQueryExample.java | 323 +++++++++++++++++++ .../portable/datagrid/package-info.java | 21 ++ .../ignite/examples/portable/package-info.java | 21 ++ .../CacheClientPortableExampleTest.java | 46 +++ .../ComputeClientPortableExampleTest.java | 37 +++ .../testsuites/IgniteExamplesSelfTestSuite.java | 6 + 19 files changed, 1513 insertions(+), 50 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/config/example-default.xml ---------------------------------------------------------------------- diff --git a/examples/config/example-default.xml b/examples/config/example-default.xml new file mode 100644 index 0000000..e6c359d --- /dev/null +++ b/examples/config/example-default.xml @@ -0,0 +1,76 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + 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. +--> + +<!-- + Ignite configuration with all defaults and enabled p2p deployment and enabled events. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:util="http://www.springframework.org/schema/util" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/util + http://www.springframework.org/schema/util/spring-util.xsd"> + <bean abstract="true" id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration"> + <!-- Set to true to enable distributed class loading for examples, default is false. --> + <property name="peerClassLoadingEnabled" value="true"/> + + <!-- Enable task execution events for examples. --> + <property name="includeEventTypes"> + <list> + <!--Task execution events--> + <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_STARTED"/> + <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_FINISHED"/> + <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_FAILED"/> + <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_TIMEDOUT"/> + <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_SESSION_ATTR_SET"/> + <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_REDUCED"/> + + <!--Cache events--> + <util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_PUT"/> + <util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_READ"/> + <util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_REMOVED"/> + </list> + </property> + + <!-- Explicitly configure TCP discovery SPI to provide list of initial nodes. --> + <property name="discoverySpi"> + <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi"> + <property name="ipFinder"> + <!-- + Ignite provides several options for automatic discovery that can be used + instead os static IP based discovery. For information on all options refer + to our documentation: http://apacheignite.readme.io/docs/cluster-config + --> + <!-- Uncomment static IP finder to enable static-based discovery of initial nodes. --> + <!--<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">--> + <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder"> + <property name="addresses"> + <list> + <!-- In distributed environment, replace with actual host IP address. --> + <value>127.0.0.1:47500..47509</value> + </list> + </property> + </bean> + </property> + </bean> + </property> + </bean> +</beans> http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/config/example-ignite.xml ---------------------------------------------------------------------- diff --git a/examples/config/example-ignite.xml b/examples/config/example-ignite.xml index e7adb54..d842a6d 100644 --- a/examples/config/example-ignite.xml +++ b/examples/config/example-ignite.xml @@ -22,62 +22,18 @@ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:util="http://www.springframework.org/schema/util" - xsi:schemaLocation=" - http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/util - http://www.springframework.org/schema/util/spring-util.xsd"> - <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration"> - <!-- Set to true to enable distributed class loading for examples, default is false. --> - <property name="peerClassLoadingEnabled" value="true"/> + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd"> + <!-- Imports default Ignite configuration --> + <import resource="example-default.xml"/> + <bean parent="ignite.cfg"> + <!-- Enabled optimized marshaller --> <property name="marshaller"> <bean class="org.apache.ignite.marshaller.optimized.OptimizedMarshaller"> <!-- Set to false to allow non-serializable objects in examples, default is true. --> <property name="requireSerializable" value="false"/> </bean> </property> - - <!-- Enable task execution events for examples. --> - <property name="includeEventTypes"> - <list> - <!--Task execution events--> - <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_STARTED"/> - <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_FINISHED"/> - <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_FAILED"/> - <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_TIMEDOUT"/> - <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_SESSION_ATTR_SET"/> - <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_REDUCED"/> - - <!--Cache events--> - <util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_PUT"/> - <util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_READ"/> - <util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_REMOVED"/> - </list> - </property> - - <!-- Explicitly configure TCP discovery SPI to provide list of initial nodes. --> - <property name="discoverySpi"> - <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi"> - <property name="ipFinder"> - <!-- - Ignite provides several options for automatic discovery that can be used - instead os static IP based discovery. For information on all options refer - to our documentation: http://apacheignite.readme.io/docs/cluster-config - --> - <!-- Uncomment static IP finder to enable static-based discovery of initial nodes. --> - <!--<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">--> - <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder"> - <property name="addresses"> - <list> - <!-- In distributed environment, replace with actual host IP address. --> - <value>127.0.0.1:47500..47509</value> - </list> - </property> - </bean> - </property> - </bean> - </property> </bean> </beans> http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/config/portable/example-ignite-portable.xml ---------------------------------------------------------------------- diff --git a/examples/config/portable/example-ignite-portable.xml b/examples/config/portable/example-ignite-portable.xml new file mode 100644 index 0000000..cde15ea --- /dev/null +++ b/examples/config/portable/example-ignite-portable.xml @@ -0,0 +1,44 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + 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. +--> + +<!-- + Ignite configuration with all defaults and enabled p2p deployment, events and portable marshaller. + + Use this configuration file when running HTTP REST examples (see 'examples/rest' folder). + + When starting a standalone node, you need to execute the following command: + {IGNITE_HOME}/bin/ignite.{bat|sh} examples/config/portable/example-ignite-portable.xml + + When starting Ignite from Java IDE, pass path to this file to Ignition: + Ignition.start("examples/config/portable/example-ignite-portable.xml"); +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd"> + <!-- Imports default Ignite configuration --> + <import resource="../example-default.xml"/> + + <bean parent="ignite.cfg"> + <!-- Enables portable marshaller --> + <property name="marshaller"> + <bean class="org.apache.ignite.marshaller.portable.PortableMarshaller"/> + </property> + </bean> +</beans> http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/Address.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/Address.java b/examples/src/main/java/org/apache/ignite/examples/portable/Address.java new file mode 100644 index 0000000..cb08b25 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/Address.java @@ -0,0 +1,72 @@ +/* + * 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.ignite.examples.portable; + +import org.apache.ignite.portable.PortableException; +import org.apache.ignite.portable.PortableMarshalAware; +import org.apache.ignite.portable.PortableReader; +import org.apache.ignite.portable.PortableWriter; + +/** + * Employee address. + * <p> + * This class implements {@link PortableMarshalAware} only for example purposes, + * in order to show how to customize serialization and deserialization of + * portable objects. + */ +public class Address implements PortableMarshalAware { + /** Street. */ + private String street; + + /** ZIP code. */ + private int zip; + + /** + * Required for portable deserialization. + */ + public Address() { + // No-op. + } + + /** + * @param street Street. + * @param zip ZIP code. + */ + public Address(String street, int zip) { + this.street = street; + this.zip = zip; + } + + /** {@inheritDoc} */ + @Override public void writePortable(PortableWriter writer) throws PortableException { + writer.writeString("street", street); + writer.writeInt("zip", zip); + } + + /** {@inheritDoc} */ + @Override public void readPortable(PortableReader reader) throws PortableException { + street = reader.readString("street"); + zip = reader.readInt("zip"); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "Address [street=" + street + + ", zip=" + zip + ']'; + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/Employee.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/Employee.java b/examples/src/main/java/org/apache/ignite/examples/portable/Employee.java new file mode 100644 index 0000000..9614168 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/Employee.java @@ -0,0 +1,93 @@ +/* + * 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.ignite.examples.portable; + +import java.util.Collection; + +/** + * This class represents employee object. + */ +public class Employee { + /** Name. */ + private String name; + + /** Salary. */ + private long salary; + + /** Address. */ + private Address address; + + /** Departments. */ + private Collection<String> departments; + + /** + * Required for portable deserialization. + */ + public Employee() { + // No-op. + } + + /** + * @param name Name. + * @param salary Salary. + * @param address Address. + * @param departments Departments. + */ + public Employee(String name, long salary, Address address, Collection<String> departments) { + this.name = name; + this.salary = salary; + this.address = address; + this.departments = departments; + } + + /** + * @return Name. + */ + public String name() { + return name; + } + + /** + * @return Salary. + */ + public long salary() { + return salary; + } + + /** + * @return Address. + */ + public Address address() { + return address; + } + + /** + * @return Departments. + */ + public Collection<String> departments() { + return departments; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "Employee [name=" + name + + ", salary=" + salary + + ", address=" + address + + ", departments=" + departments + ']'; + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/EmployeeKey.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/EmployeeKey.java b/examples/src/main/java/org/apache/ignite/examples/portable/EmployeeKey.java new file mode 100644 index 0000000..f322167 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/EmployeeKey.java @@ -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. + */ + +package org.apache.ignite.examples.portable; + +/** + * This class represents key for employee object. + * <p> + * Used in query example to collocate employees + * with their organizations. + */ +public class EmployeeKey { + /** ID. */ + private int id; + + /** Organization ID. */ + private int organizationId; + + /** + * Required for portable deserialization. + */ + public EmployeeKey() { + // No-op. + } + + /** + * @param id ID. + * @param organizationId Organization ID. + */ + public EmployeeKey(int id, int organizationId) { + this.id = id; + this.organizationId = organizationId; + } + + /** + * @return ID. + */ + public int id() { + return id; + } + + /** + * @return Organization ID. + */ + public int organizationId() { + return organizationId; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + if (this == o) + return true; + + if (o == null || getClass() != o.getClass()) + return false; + + EmployeeKey key = (EmployeeKey)o; + + return id == key.id && organizationId == key.organizationId; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + int res = id; + + res = 31 * res + organizationId; + + return res; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "EmployeeKey [id=" + id + + ", organizationId=" + organizationId + ']'; + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/ExamplePortableNodeStartup.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/ExamplePortableNodeStartup.java b/examples/src/main/java/org/apache/ignite/examples/portable/ExamplePortableNodeStartup.java new file mode 100644 index 0000000..87a41f7 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/ExamplePortableNodeStartup.java @@ -0,0 +1,36 @@ +/* + * 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.ignite.examples.portable; + +import org.apache.ignite.IgniteException; +import org.apache.ignite.Ignition; + +/** + * Starts up an empty node with example configuration and portable marshaller enabled. + */ +public class ExamplePortableNodeStartup { + /** + * Start up an empty node with example configuration and portable marshaller enabled. + * + * @param args Command line arguments, none required. + * @throws IgniteException If failed. + */ + public static void main(String[] args) throws IgniteException { + Ignition.start("examples/config/portable/example-ignite-portable.xml"); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/Organization.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/Organization.java b/examples/src/main/java/org/apache/ignite/examples/portable/Organization.java new file mode 100644 index 0000000..f52cac1 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/Organization.java @@ -0,0 +1,93 @@ +/* + * 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.ignite.examples.portable; + +import java.sql.Timestamp; + +/** + * This class represents organization object. + */ +public class Organization { + /** Name. */ + private String name; + + /** Address. */ + private Address address; + + /** Type. */ + private OrganizationType type; + + /** Last update time. */ + private Timestamp lastUpdated; + + /** + * Required for portable deserialization. + */ + public Organization() { + // No-op. + } + + /** + * @param name Name. + * @param address Address. + * @param type Type. + * @param lastUpdated Last update time. + */ + public Organization(String name, Address address, OrganizationType type, Timestamp lastUpdated) { + this.name = name; + this.address = address; + this.type = type; + this.lastUpdated = lastUpdated; + } + + /** + * @return Name. + */ + public String name() { + return name; + } + + /** + * @return Address. + */ + public Address address() { + return address; + } + + /** + * @return Type. + */ + public OrganizationType type() { + return type; + } + + /** + * @return Last update time. + */ + public Timestamp lastUpdated() { + return lastUpdated; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "Organization [name=" + name + + ", address=" + address + + ", type=" + type + + ", lastUpdated=" + lastUpdated + ']'; + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/OrganizationType.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/OrganizationType.java b/examples/src/main/java/org/apache/ignite/examples/portable/OrganizationType.java new file mode 100644 index 0000000..c753e2d --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/OrganizationType.java @@ -0,0 +1,32 @@ +/* + * 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.ignite.examples.portable; + +/** + * Organization type enum. + */ +public enum OrganizationType { + /** Non-profit organization. */ + NON_PROFIT, + + /** Private organization. */ + PRIVATE, + + /** Government organization. */ + GOVERNMENT +} http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/computegrid/ComputeClientPortableTaskExecutionExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/computegrid/ComputeClientPortableTaskExecutionExample.java b/examples/src/main/java/org/apache/ignite/examples/portable/computegrid/ComputeClientPortableTaskExecutionExample.java new file mode 100644 index 0000000..34d9cde --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/computegrid/ComputeClientPortableTaskExecutionExample.java @@ -0,0 +1,154 @@ +/* + * 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.ignite.examples.portable.computegrid; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import org.apache.ignite.Ignite; +import org.apache.ignite.Ignition; +import org.apache.ignite.examples.portable.Address; +import org.apache.ignite.examples.portable.Employee; +import org.apache.ignite.examples.portable.ExamplePortableNodeStartup; +import org.apache.ignite.portable.PortableObject; + +/** + * This example demonstrates use of portable objects with task execution. + * Specifically it shows that portable objects are simple Java POJOs and do not require any special treatment. + * <p> + * The example executes map-reduce task that accepts collection of portable objects as an argument. + * Since these objects are never deserialized on remote nodes, classes are not required on classpath + * of these nodes. + * <p> + * Remote nodes should always be started with special configuration file which + * enables the portable marshaller: {@code 'ignite.{sh|bat} examples/config/portable/example-ignite-portable.xml'}. + * <p> + * Alternatively you can run {@link ExamplePortableNodeStartup} in another JVM which will + * start node with {@code examples/config/portable/example-ignite-portable.xml} configuration. + */ +public class ComputeClientPortableTaskExecutionExample { + /** + * Executes example. + * + * @param args Command line arguments, none required. + */ + public static void main(String[] args) { + try (Ignite ignite = Ignition.start("examples/config/portable/example-ignite-portable.xml")) { + System.out.println(); + System.out.println(">>> Portable objects task execution example started."); + + if (ignite.cluster().forRemotes().nodes().isEmpty()) { + System.out.println(); + System.out.println(">>> This example requires remote nodes to be started."); + System.out.println(">>> Please start at least 1 remote node."); + System.out.println(">>> Refer to example's javadoc for details on configuration."); + System.out.println(); + + return; + } + + // Generate employees to calculate average salary for. + Collection<Employee> employees = employees(); + + System.out.println(); + System.out.println(">>> Calculating average salary for employees:"); + + for (Employee employee : employees) + System.out.println(">>> " + employee); + + // Convert collection of employees to collection of portable objects. + // This allows to send objects across nodes without requiring to have + // Employee class on classpath of these nodes. + Collection<PortableObject> portables = ignite.portables().toPortable(employees); + + // Execute task and get average salary. + Long avgSalary = ignite.compute(ignite.cluster().forRemotes()).execute(new ComputeClientTask(), portables); + + System.out.println(); + System.out.println(">>> Average salary for all employees: " + avgSalary); + System.out.println(); + } + } + + /** + * Creates collection of employees. + * + * @return Collection of employees. + */ + private static Collection<Employee> employees() { + Collection<Employee> employees = new ArrayList<>(); + + employees.add(new Employee( + "James Wilson", + 12500, + new Address("1096 Eddy Street, San Francisco, CA", 94109), + Arrays.asList("Human Resources", "Customer Service") + )); + + employees.add(new Employee( + "Daniel Adams", + 11000, + new Address("184 Fidler Drive, San Antonio, TX", 78205), + Arrays.asList("Development", "QA") + )); + + employees.add(new Employee( + "Cristian Moss", + 12500, + new Address("667 Jerry Dove Drive, Florence, SC", 29501), + Arrays.asList("Logistics") + )); + + employees.add(new Employee( + "Allison Mathis", + 25300, + new Address("2702 Freedom Lane, Hornitos, CA", 95325), + Arrays.asList("Development") + )); + + employees.add(new Employee( + "Breana Robbin", + 6500, + new Address("3960 Sundown Lane, Austin, TX", 78758), + Arrays.asList("Sales") + )); + + employees.add(new Employee( + "Philip Horsley", + 19800, + new Address("2803 Elsie Drive, Sioux Falls, SD", 57104), + Arrays.asList("Sales") + )); + + employees.add(new Employee( + "Brian Peters", + 10600, + new Address("1407 Pearlman Avenue, Boston, MA", 12110), + Arrays.asList("Development", "QA") + )); + + employees.add(new Employee( + "Jack Yang", + 12900, + new Address("4425 Parrish Avenue Smithsons Valley, TX", 78130), + Arrays.asList("Sales") + )); + + return employees; + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/computegrid/ComputeClientTask.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/computegrid/ComputeClientTask.java b/examples/src/main/java/org/apache/ignite/examples/portable/computegrid/ComputeClientTask.java new file mode 100644 index 0000000..0eee8c6 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/computegrid/ComputeClientTask.java @@ -0,0 +1,116 @@ +/* + * 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.ignite.examples.portable.computegrid; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import org.apache.ignite.compute.ComputeJob; +import org.apache.ignite.compute.ComputeJobAdapter; +import org.apache.ignite.compute.ComputeJobResult; +import org.apache.ignite.compute.ComputeTaskSplitAdapter; +import org.apache.ignite.lang.IgniteBiTuple; +import org.apache.ignite.portable.PortableObject; +import org.jetbrains.annotations.Nullable; + +/** + * Task that is used for {@link ComputeClientPortableTaskExecutionExample} and + * similar examples in .NET and C++. + * <p> + * This task calculates average salary for provided collection of employees. + * It splits the collection into batches of size {@code 3} and creates a job + * for each batch. After all jobs are executed, there results are reduced to + * get the average salary. + */ +public class ComputeClientTask extends ComputeTaskSplitAdapter<Collection<PortableObject>, Long> { + /** {@inheritDoc} */ + @Override protected Collection<? extends ComputeJob> split( + int gridSize, + Collection<PortableObject> arg + ) { + Collection<ComputeClientJob> jobs = new ArrayList<>(); + + Collection<PortableObject> employees = new ArrayList<>(); + + // Split provided collection into batches and + // create a job for each batch. + for (PortableObject employee : arg) { + employees.add(employee); + + if (employees.size() == 3) { + jobs.add(new ComputeClientJob(employees)); + + employees = new ArrayList<>(3); + } + } + + if (!employees.isEmpty()) + jobs.add(new ComputeClientJob(employees)); + + return jobs; + } + + /** {@inheritDoc} */ + @Nullable @Override public Long reduce(List<ComputeJobResult> results) { + long sum = 0; + int cnt = 0; + + for (ComputeJobResult res : results) { + IgniteBiTuple<Long, Integer> t = res.getData(); + + sum += t.get1(); + cnt += t.get2(); + } + + return sum / cnt; + } + + /** + * Remote job for {@link ComputeClientTask}. + */ + private static class ComputeClientJob extends ComputeJobAdapter { + /** Collection of employees. */ + private final Collection<PortableObject> employees; + + /** + * @param employees Collection of employees. + */ + private ComputeClientJob(Collection<PortableObject> employees) { + this.employees = employees; + } + + /** {@inheritDoc} */ + @Nullable @Override public Object execute() { + long sum = 0; + int cnt = 0; + + for (PortableObject employee : employees) { + System.out.println(">>> Processing employee: " + employee.field("name")); + + // Get salary from portable object. Note that object + // doesn't need to be fully deserialized. + long salary = employee.field("salary"); + + sum += salary; + cnt++; + } + + return new IgniteBiTuple<>(sum, cnt); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/computegrid/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/computegrid/package-info.java b/examples/src/main/java/org/apache/ignite/examples/portable/computegrid/package-info.java new file mode 100644 index 0000000..469128c --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/computegrid/package-info.java @@ -0,0 +1,21 @@ +/* + * 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. + */ + +/** + * Demonstrates the usage of portable objects with task execution. + */ +package org.apache.ignite.examples.portable.computegrid; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/datagrid/CacheClientPortablePutGetExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/datagrid/CacheClientPortablePutGetExample.java b/examples/src/main/java/org/apache/ignite/examples/portable/datagrid/CacheClientPortablePutGetExample.java new file mode 100644 index 0000000..19c5685 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/datagrid/CacheClientPortablePutGetExample.java @@ -0,0 +1,226 @@ +/* + * 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.ignite.examples.portable.datagrid; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.Ignition; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.examples.portable.Address; +import org.apache.ignite.examples.portable.ExamplePortableNodeStartup; +import org.apache.ignite.examples.portable.Organization; +import org.apache.ignite.examples.portable.OrganizationType; +import org.apache.ignite.portable.PortableObject; + +/** + * This example demonstrates use of portable objects with Ignite cache. + * Specifically it shows that portable objects are simple Java POJOs and do not require any special treatment. + * <p> + * The example executes several put-get operations on Ignite cache with portable values. Note that + * it demonstrates how portable object can be retrieved in fully-deserialized form or in portable object + * format using special cache projection. + * <p> + * Remote nodes should always be started with special configuration file which + * enables the portable marshaller: {@code 'ignite.{sh|bat} examples/config/portable/example-ignite-portable.xml'}. + * <p> + * Alternatively you can run {@link ExamplePortableNodeStartup} in another JVM which will + * start node with {@code examples/config/portable/example-ignite-portable.xml} configuration. + */ +public class CacheClientPortablePutGetExample { + /** Cache name. */ + private static final String CACHE_NAME = CacheClientPortablePutGetExample.class.getSimpleName(); + + /** + * Executes example. + * + * @param args Command line arguments, none required. + */ + public static void main(String[] args) { + try (Ignite ignite = Ignition.start("examples/config/portable/example-ignite-portable.xml")) { + System.out.println(); + System.out.println(">>> Portable objects cache put-get example started."); + + CacheConfiguration<Integer, Organization> cfg = new CacheConfiguration<>(); + + cfg.setCacheMode(CacheMode.PARTITIONED); + cfg.setName(CACHE_NAME); + cfg.setAtomicityMode(CacheAtomicityMode.ATOMIC); + + try (IgniteCache<Integer, Organization> cache = ignite.createCache(cfg)) { + if (ignite.cluster().forDataNodes(cache.getName()).nodes().isEmpty()) { + System.out.println(); + System.out.println(">>> This example requires remote cache node nodes to be started."); + System.out.println(">>> Please start at least 1 remote cache node."); + System.out.println(">>> Refer to example's javadoc for details on configuration."); + System.out.println(); + + return; + } + + putGet(cache); + putGetPortable(cache); + putGetAll(cache); + putGetAllPortable(cache); + + System.out.println(); + } + } + } + + /** + * Execute individual put and get. + * + * @param cache Cache. + */ + private static void putGet(IgniteCache<Integer, Organization> cache) { + // Create new Organization portable object to store in cache. + Organization org = new Organization( + "Microsoft", // Name. + new Address("1096 Eddy Street, San Francisco, CA", 94109), // Address. + OrganizationType.PRIVATE, // Type. + new Timestamp(System.currentTimeMillis())); // Last update time. + + // Put created data entry to cache. + cache.put(1, org); + + // Get recently created organization as a strongly-typed fully de-serialized instance. + Organization orgFromCache = cache.get(1); + + System.out.println(); + System.out.println(">>> Retrieved organization instance from cache: " + orgFromCache); + } + + /** + * Execute individual put and get, getting value in portable format, without de-serializing it. + * + * @param cache Cache. + */ + private static void putGetPortable(IgniteCache<Integer, Organization> cache) { + // Create new Organization portable object to store in cache. + Organization org = new Organization( + "Microsoft", // Name. + new Address("1096 Eddy Street, San Francisco, CA", 94109), // Address. + OrganizationType.PRIVATE, // Type. + new Timestamp(System.currentTimeMillis())); // Last update time. + + // Put created data entry to cache. + cache.put(1, org); + + // Get cache that will get values as portable objects. + IgniteCache<Integer, PortableObject> portableCache = cache.withKeepPortable(); + + // Get recently created organization as a portable object. + PortableObject po = portableCache.get(1); + + // Get organization's name from portable object (note that + // object doesn't need to be fully deserialized). + String name = po.field("name"); + + System.out.println(); + System.out.println(">>> Retrieved organization name from portable object: " + name); + } + + /** + * Execute bulk {@code putAll(...)} and {@code getAll(...)} operations. + * + * @param cache Cache. + */ + private static void putGetAll(IgniteCache<Integer, Organization> cache) { + // Create new Organization portable objects to store in cache. + Organization org1 = new Organization( + "Microsoft", // Name. + new Address("1096 Eddy Street, San Francisco, CA", 94109), // Address. + OrganizationType.PRIVATE, // Type. + new Timestamp(System.currentTimeMillis())); // Last update time. + + Organization org2 = new Organization( + "Red Cross", // Name. + new Address("184 Fidler Drive, San Antonio, TX", 78205), // Address. + OrganizationType.NON_PROFIT, // Type. + new Timestamp(System.currentTimeMillis())); // Last update time. + + Map<Integer, Organization> map = new HashMap<>(); + + map.put(1, org1); + map.put(2, org2); + + // Put created data entries to cache. + cache.putAll(map); + + // Get recently created organizations as a strongly-typed fully de-serialized instances. + Map<Integer, Organization> mapFromCache = cache.getAll(map.keySet()); + + System.out.println(); + System.out.println(">>> Retrieved organization instances from cache:"); + + for (Organization org : mapFromCache.values()) + System.out.println(">>> " + org); + } + + /** + * Execute bulk {@code putAll(...)} and {@code getAll(...)} operations, + * getting values in portable format, without de-serializing it. + * + * @param cache Cache. + */ + private static void putGetAllPortable(IgniteCache<Integer, Organization> cache) { + // Create new Organization portable objects to store in cache. + Organization org1 = new Organization( + "Microsoft", // Name. + new Address("1096 Eddy Street, San Francisco, CA", 94109), // Address. + OrganizationType.PRIVATE, // Type. + new Timestamp(System.currentTimeMillis())); // Last update time. + + Organization org2 = new Organization( + "Red Cross", // Name. + new Address("184 Fidler Drive, San Antonio, TX", 78205), // Address. + OrganizationType.NON_PROFIT, // Type. + new Timestamp(System.currentTimeMillis())); // Last update time. + + Map<Integer, Organization> map = new HashMap<>(); + + map.put(1, org1); + map.put(2, org2); + + // Put created data entries to cache. + cache.putAll(map); + + // Get cache that will get values as portable objects. + IgniteCache<Integer, PortableObject> portableCache = cache.withKeepPortable(); + + // Get recently created organizations as portable objects. + Map<Integer, PortableObject> poMap = portableCache.getAll(map.keySet()); + + Collection<String> names = new ArrayList<>(); + + // Get organizations' names from portable objects (note that + // objects don't need to be fully deserialized). + for (PortableObject po : poMap.values()) + names.add(po.<String>field("name")); + + System.out.println(); + System.out.println(">>> Retrieved organization names from portable objects: " + names); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/datagrid/CacheClientPortableQueryExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/datagrid/CacheClientPortableQueryExample.java b/examples/src/main/java/org/apache/ignite/examples/portable/datagrid/CacheClientPortableQueryExample.java new file mode 100644 index 0000000..1eb43b3 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/datagrid/CacheClientPortableQueryExample.java @@ -0,0 +1,323 @@ +/* + * 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.ignite.examples.portable.datagrid; + +import java.sql.Timestamp; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.cache.Cache; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.Ignition; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cache.CacheTypeMetadata; +import org.apache.ignite.cache.query.QueryCursor; +import org.apache.ignite.cache.query.SqlFieldsQuery; +import org.apache.ignite.cache.query.SqlQuery; +import org.apache.ignite.cache.query.TextQuery; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.examples.portable.Address; +import org.apache.ignite.examples.portable.Employee; +import org.apache.ignite.examples.portable.EmployeeKey; +import org.apache.ignite.examples.portable.ExamplePortableNodeStartup; +import org.apache.ignite.examples.portable.Organization; +import org.apache.ignite.examples.portable.OrganizationType; +import org.apache.ignite.portable.PortableObject; + +/** + * This example demonstrates use of portable objects with cache queries. + * The example populates cache with sample data and runs several SQL and full text queries over this data. + * <p> + * Remote nodes should always be started with special configuration file which + * enables the portable marshaller: {@code 'ignite.{sh|bat} examples/config/portable/example-ignite-portable.xml'}. + * <p> + * Alternatively you can run {@link ExamplePortableNodeStartup} in another JVM which will + * start node with {@code examples/config/portable/example-ignite-portable.xml} configuration. + */ +public class CacheClientPortableQueryExample { + /** Organization cache name. */ + private static final String ORGANIZATION_CACHE_NAME = CacheClientPortableQueryExample.class.getSimpleName() + + "Organizations"; + + /** Employee cache name. */ + private static final String EMPLOYEE_CACHE_NAME = CacheClientPortableQueryExample.class.getSimpleName() + + "Employees"; + + /** + * Executes example. + * + * @param args Command line arguments, none required. + */ + public static void main(String[] args) { + try (Ignite ignite = Ignition.start("examples/config/portable/example-ignite-portable.xml")) { + System.out.println(); + System.out.println(">>> Portable objects cache query example started."); + + CacheConfiguration<Integer, Organization> orgCacheCfg = new CacheConfiguration<>(); + + orgCacheCfg.setCacheMode(CacheMode.PARTITIONED); + orgCacheCfg.setName(ORGANIZATION_CACHE_NAME); + + orgCacheCfg.setTypeMetadata(Arrays.asList(createOrganizationTypeMetadata())); + + CacheConfiguration<EmployeeKey, Employee> employeeCacheCfg = new CacheConfiguration<>(); + + employeeCacheCfg.setCacheMode(CacheMode.PARTITIONED); + employeeCacheCfg.setName(EMPLOYEE_CACHE_NAME); + + employeeCacheCfg.setTypeMetadata(Arrays.asList(createEmployeeTypeMetadata())); + + try (IgniteCache<Integer, Organization> orgCache = ignite.createCache(orgCacheCfg); + IgniteCache<EmployeeKey, Employee> employeeCache = ignite.createCache(employeeCacheCfg) + ) { + if (ignite.cluster().forDataNodes(orgCache.getName()).nodes().isEmpty()) { + System.out.println(); + System.out.println(">>> This example requires remote cache nodes to be started."); + System.out.println(">>> Please start at least 1 remote cache node."); + System.out.println(">>> Refer to example's javadoc for details on configuration."); + System.out.println(); + + return; + } + + // Populate cache with sample data entries. + populateCache(orgCache, employeeCache); + + // Get cache that will work with portable objects. + IgniteCache<PortableObject, PortableObject> portableCache = employeeCache.withKeepPortable(); + + // Run SQL query example. + sqlQuery(portableCache); + + // Run SQL query with join example. + sqlJoinQuery(portableCache); + + // Run SQL fields query example. + sqlFieldsQuery(portableCache); + + // Run full text query example. + textQuery(portableCache); + + System.out.println(); + } + } + } + + /** + * Create cache type metadata for {@link Employee}. + * + * @return Cache type metadata. + */ + private static CacheTypeMetadata createEmployeeTypeMetadata() { + CacheTypeMetadata employeeTypeMeta = new CacheTypeMetadata(); + + employeeTypeMeta.setValueType(Employee.class); + + employeeTypeMeta.setKeyType(EmployeeKey.class); + + Map<String, Class<?>> ascFields = new HashMap<>(); + + ascFields.put("name", String.class); + ascFields.put("salary", Long.class); + ascFields.put("address.zip", Integer.class); + ascFields.put("organizationId", Integer.class); + + employeeTypeMeta.setAscendingFields(ascFields); + + employeeTypeMeta.setTextFields(Arrays.asList("address.street")); + + return employeeTypeMeta; + } + + /** + * Create cache type metadata for {@link Organization}. + * + * @return Cache type metadata. + */ + private static CacheTypeMetadata createOrganizationTypeMetadata() { + CacheTypeMetadata organizationTypeMeta = new CacheTypeMetadata(); + + organizationTypeMeta.setValueType(Organization.class); + + organizationTypeMeta.setKeyType(Integer.class); + + Map<String, Class<?>> ascFields = new HashMap<>(); + + ascFields.put("name", String.class); + + Map<String, Class<?>> queryFields = new HashMap<>(); + + queryFields.put("address.street", String.class); + + organizationTypeMeta.setAscendingFields(ascFields); + + organizationTypeMeta.setQueryFields(queryFields); + + return organizationTypeMeta; + } + + /** + * Queries employees that have provided ZIP code in address. + * + * @param cache Ignite cache. + */ + private static void sqlQuery(IgniteCache<PortableObject, PortableObject> cache) { + SqlQuery<PortableObject, PortableObject> query = new SqlQuery<>(Employee.class, "zip = ?"); + + int zip = 94109; + + QueryCursor<Cache.Entry<PortableObject, PortableObject>> employees = cache.query(query.setArgs(zip)); + + System.out.println(); + System.out.println(">>> Employees with zip " + zip + ':'); + + for (Cache.Entry<PortableObject, PortableObject> e : employees.getAll()) + System.out.println(">>> " + e.getValue().deserialize()); + } + + /** + * Queries employees that work for organization with provided name. + * + * @param cache Ignite cache. + */ + private static void sqlJoinQuery(IgniteCache<PortableObject, PortableObject> cache) { + SqlQuery<PortableObject, PortableObject> query = new SqlQuery<>(Employee.class, + "from Employee, \"" + ORGANIZATION_CACHE_NAME + "\".Organization as org " + + "where Employee.organizationId = org._key and org.name = ?"); + + String organizationName = "GridGain"; + + QueryCursor<Cache.Entry<PortableObject, PortableObject>> employees = + cache.query(query.setArgs(organizationName)); + + System.out.println(); + System.out.println(">>> Employees working for " + organizationName + ':'); + + for (Cache.Entry<PortableObject, PortableObject> e : employees.getAll()) + System.out.println(">>> " + e.getValue()); + } + + /** + * Queries names and salaries for all employees. + * + * @param cache Ignite cache. + */ + private static void sqlFieldsQuery(IgniteCache<PortableObject, PortableObject> cache) { + SqlFieldsQuery query = new SqlFieldsQuery("select name, salary from Employee"); + + QueryCursor<List<?>> employees = cache.query(query); + + System.out.println(); + System.out.println(">>> Employee names and their salaries:"); + + for (List<?> row : employees.getAll()) + System.out.println(">>> [Name=" + row.get(0) + ", salary=" + row.get(1) + ']'); + } + + /** + * Queries employees that live in Texas using full-text query API. + * + * @param cache Ignite cache. + */ + private static void textQuery(IgniteCache<PortableObject, PortableObject> cache) { + TextQuery<PortableObject, PortableObject> query = new TextQuery<>(Employee.class, "TX"); + + QueryCursor<Cache.Entry<PortableObject, PortableObject>> employees = cache.query(query); + + System.out.println(); + System.out.println(">>> Employees living in Texas:"); + + for (Cache.Entry<PortableObject, PortableObject> e : employees.getAll()) + System.out.println(">>> " + e.getValue().deserialize()); + } + + /** + * Populates cache with data. + * + * @param orgCache Organization cache. + * @param employeeCache Employee cache. + */ + private static void populateCache(IgniteCache<Integer, Organization> orgCache, + IgniteCache<EmployeeKey, Employee> employeeCache) { + orgCache.put(1, new Organization( + "GridGain", + new Address("1065 East Hillsdale Blvd, Foster City, CA", 94404), + OrganizationType.PRIVATE, + new Timestamp(System.currentTimeMillis()) + )); + + orgCache.put(2, new Organization( + "Microsoft", + new Address("1096 Eddy Street, San Francisco, CA", 94109), + OrganizationType.PRIVATE, + new Timestamp(System.currentTimeMillis()) + )); + + employeeCache.put(new EmployeeKey(1, 1), new Employee( + "James Wilson", + 12500, + new Address("1096 Eddy Street, San Francisco, CA", 94109), + Arrays.asList("Human Resources", "Customer Service") + )); + + employeeCache.put(new EmployeeKey(2, 1), new Employee( + "Daniel Adams", + 11000, + new Address("184 Fidler Drive, San Antonio, TX", 78130), + Arrays.asList("Development", "QA") + )); + + employeeCache.put(new EmployeeKey(3, 1), new Employee( + "Cristian Moss", + 12500, + new Address("667 Jerry Dove Drive, Florence, SC", 29501), + Arrays.asList("Logistics") + )); + + employeeCache.put(new EmployeeKey(4, 2), new Employee( + "Allison Mathis", + 25300, + new Address("2702 Freedom Lane, San Francisco, CA", 94109), + Arrays.asList("Development") + )); + + employeeCache.put(new EmployeeKey(5, 2), new Employee( + "Breana Robbin", + 6500, + new Address("3960 Sundown Lane, Austin, TX", 78130), + Arrays.asList("Sales") + )); + + employeeCache.put(new EmployeeKey(6, 2), new Employee( + "Philip Horsley", + 19800, + new Address("2803 Elsie Drive, Sioux Falls, SD", 57104), + Arrays.asList("Sales") + )); + + employeeCache.put(new EmployeeKey(7, 2), new Employee( + "Brian Peters", + 10600, + new Address("1407 Pearlman Avenue, Boston, MA", 12110), + Arrays.asList("Development", "QA") + )); + } +} + http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/datagrid/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/datagrid/package-info.java b/examples/src/main/java/org/apache/ignite/examples/portable/datagrid/package-info.java new file mode 100644 index 0000000..b24f233 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/datagrid/package-info.java @@ -0,0 +1,21 @@ +/* + * 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. + */ + +/** + * Demonstrates the usage of portable objects with cache. + */ +package org.apache.ignite.examples.portable.datagrid; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/main/java/org/apache/ignite/examples/portable/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/portable/package-info.java b/examples/src/main/java/org/apache/ignite/examples/portable/package-info.java new file mode 100644 index 0000000..4301027 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/portable/package-info.java @@ -0,0 +1,21 @@ +/* + * 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. + */ + +/** + * Contains portable classes and examples. + */ +package org.apache.ignite.examples.portable; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/test/java/org/apache/ignite/examples/CacheClientPortableExampleTest.java ---------------------------------------------------------------------- diff --git a/examples/src/test/java/org/apache/ignite/examples/CacheClientPortableExampleTest.java b/examples/src/test/java/org/apache/ignite/examples/CacheClientPortableExampleTest.java new file mode 100644 index 0000000..6ea1484 --- /dev/null +++ b/examples/src/test/java/org/apache/ignite/examples/CacheClientPortableExampleTest.java @@ -0,0 +1,46 @@ +/* + * 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.ignite.examples; + +import org.apache.ignite.examples.portable.datagrid.CacheClientPortablePutGetExample; +import org.apache.ignite.examples.portable.datagrid.CacheClientPortableQueryExample; +import org.apache.ignite.testframework.junits.common.GridAbstractExamplesTest; + +/** + * + */ +public class CacheClientPortableExampleTest extends GridAbstractExamplesTest { + /** {@inheritDoc} */ + @Override protected String defaultConfig() { + return "examples/config/portable/example-ignite-portable.xml"; + } + + /** + * @throws Exception If failed. + */ + public void testPortablePutGetExample() throws Exception { + CacheClientPortablePutGetExample.main(new String[] {}); + } + + /** + * @throws Exception If failed. + */ + public void testPortableQueryExample() throws Exception { + CacheClientPortableQueryExample.main(new String[] {}); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/test/java/org/apache/ignite/examples/ComputeClientPortableExampleTest.java ---------------------------------------------------------------------- diff --git a/examples/src/test/java/org/apache/ignite/examples/ComputeClientPortableExampleTest.java b/examples/src/test/java/org/apache/ignite/examples/ComputeClientPortableExampleTest.java new file mode 100644 index 0000000..2223aec --- /dev/null +++ b/examples/src/test/java/org/apache/ignite/examples/ComputeClientPortableExampleTest.java @@ -0,0 +1,37 @@ +/* + * 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.ignite.examples; + +import org.apache.ignite.examples.portable.computegrid.ComputeClientPortableTaskExecutionExample; +import org.apache.ignite.testframework.junits.common.GridAbstractExamplesTest; + +/** + * + */ +public class ComputeClientPortableExampleTest extends GridAbstractExamplesTest { + /** {@inheritDoc} */ + @Override protected String defaultConfig() { + return "examples/config/portable/example-ignite-portable.xml"; + } + + /** + * @throws Exception If failed. + */ + public void testPortableTaskExecutionExample() throws Exception { + ComputeClientPortableTaskExecutionExample.main(new String[] {}); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/6a5a48a0/examples/src/test/java/org/apache/ignite/testsuites/IgniteExamplesSelfTestSuite.java ---------------------------------------------------------------------- diff --git a/examples/src/test/java/org/apache/ignite/testsuites/IgniteExamplesSelfTestSuite.java b/examples/src/test/java/org/apache/ignite/testsuites/IgniteExamplesSelfTestSuite.java index 4669ae4..baa23fc 100644 --- a/examples/src/test/java/org/apache/ignite/testsuites/IgniteExamplesSelfTestSuite.java +++ b/examples/src/test/java/org/apache/ignite/testsuites/IgniteExamplesSelfTestSuite.java @@ -20,10 +20,12 @@ package org.apache.ignite.testsuites; import junit.framework.TestSuite; import org.apache.ignite.examples.BasicExamplesMultiNodeSelfTest; import org.apache.ignite.examples.BasicExamplesSelfTest; +import org.apache.ignite.examples.CacheClientPortableExampleTest; import org.apache.ignite.examples.CacheExamplesMultiNodeSelfTest; import org.apache.ignite.examples.CacheExamplesSelfTest; import org.apache.ignite.examples.CheckpointExamplesSelfTest; import org.apache.ignite.examples.ClusterGroupExampleSelfTest; +import org.apache.ignite.examples.ComputeClientPortableExampleTest; import org.apache.ignite.examples.ContinuationExamplesMultiNodeSelfTest; import org.apache.ignite.examples.ContinuationExamplesSelfTest; import org.apache.ignite.examples.ContinuousMapperExamplesMultiNodeSelfTest; @@ -93,6 +95,10 @@ public class IgniteExamplesSelfTestSuite extends TestSuite { suite.addTest(new TestSuite(MonteCarloExamplesMultiNodeSelfTest.class)); suite.addTest(new TestSuite(HibernateL2CacheExampleMultiNodeSelfTest.class)); + // Portable. + suite.addTest(new TestSuite(CacheClientPortableExampleTest.class)); + suite.addTest(new TestSuite(ComputeClientPortableExampleTest.class)); + return suite; } } \ No newline at end of file
