Skip to content

Instantly share code, notes, and snippets.

@zshamrock
Last active June 15, 2016 10:46
Show Gist options
  • Save zshamrock/ac5e907bf1b091ad05570f77ae1ba69f to your computer and use it in GitHub Desktop.
Save zshamrock/ac5e907bf1b091ad05570f77ae1ba69f to your computer and use it in GitHub Desktop.
package experiments.ignite;
import java.util.Arrays;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteLogger;
import org.apache.ignite.IgniteSet;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.CollectionConfiguration;
import org.apache.ignite.lang.IgniteRunnable;
import org.apache.ignite.resources.IgniteInstanceResource;
import org.apache.ignite.resources.LoggerResource;
public class DistributedSetDemo {
public static void main(final String[] args) {
final Ignite ignite = Ignition.start();
final CollectionConfiguration collectionConfiguration = new CollectionConfiguration();
collectionConfiguration.setCollocated(true); // 1
// collectionConfiguration.setBackups(1); // 2
System.out.printf("Started node %s\n", ignite.cluster().localNode().id());
IgniteSet<Long> numbers = ignite.set("numbers", null);
if (numbers == null) {
numbers = ignite.set("numbers", collectionConfiguration);
}
System.out.printf("[local] Size of the numbers distributed set is %d, and items are %s\n",
numbers.size(),
Arrays.toString(numbers.toArray(new Long[numbers.size()])));
System.out.printf("[local] Is collocated? %b\n", numbers.collocated());
System.out.printf("[local] numbers contains 42? %b\n", numbers.contains(42L));
numbers.affinityRun(new IgniteRunnable() {
@IgniteInstanceResource
private Ignite ignite;
@LoggerResource
private IgniteLogger logger;
@Override
public void run() {
System.out.println(String.format("[affinity] Running on %s node", ignite.cluster().localNode().id()));
final IgniteSet<Long> affinityNumbers = ignite.set("numbers", null);
System.out.printf("[affinity] Size of the numbers distributed set is %d, and items are %s\n",
affinityNumbers.size(),
Arrays.toString(affinityNumbers.toArray(new Long[affinityNumbers.size()])));
System.out.printf("[affinity] Is collocated? %b\n", affinityNumbers.collocated());
System.out.printf("[affinity] numbers contains 42? %b\n", affinityNumbers.contains(42L));
}
});
numbers.add(42L);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment