Created
September 14, 2022 12:24
-
-
Save ptupitsyn/260411f27d0dcd0bf4391506ad3e54b2 to your computer and use it in GitHub Desktop.
Ignite.NET mixed cluster Cache.Invoke
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.ignite.Ignite; | |
import org.apache.ignite.IgniteException; | |
import org.apache.ignite.cluster.ClusterNode; | |
import org.apache.ignite.compute.ComputeJob; | |
import org.apache.ignite.compute.ComputeJobAdapter; | |
import org.apache.ignite.compute.ComputeJobResult; | |
import org.apache.ignite.compute.ComputeTaskAdapter; | |
import org.apache.ignite.resources.IgniteInstanceResource; | |
import org.jetbrains.annotations.NotNull; | |
import org.jetbrains.annotations.Nullable; | |
import javax.cache.processor.EntryProcessor; | |
import javax.cache.processor.EntryProcessorException; | |
import javax.cache.processor.MutableEntry; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Map; | |
public class EntryProcessorComputeTask extends ComputeTaskAdapter<Object, Object> { | |
@Override | |
public @NotNull Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, @Nullable Object o) throws IgniteException { | |
var job = new ComputeJobAdapter() { | |
@IgniteInstanceResource | |
Ignite ignite; | |
@Override public Object execute() { | |
return ignite.getOrCreateCache("myCache").invoke(o, new EntryProcessor(){ | |
@Override | |
public Object process(MutableEntry mutableEntry, Object... objects) throws EntryProcessorException { | |
int newValue = (Integer) mutableEntry.getValue() + 1; | |
mutableEntry.setValue(newValue); | |
return newValue; | |
} | |
}); | |
} | |
}; | |
return Collections.singletonMap(job, subgrid.get(0)); | |
} | |
@Nullable | |
@Override | |
public Object reduce(List<ComputeJobResult> results) throws IgniteException { | |
return results.get(0).getData(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Apache.Ignite" Version="2.13.0" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.example</groupId> | |
<artifactId>IgniteMixedClusterInvoke</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<properties> | |
<maven.compiler.source>11</maven.compiler.source> | |
<maven.compiler.target>11</maven.compiler.target> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.apache.ignite</groupId> | |
<artifactId>ignite-core</artifactId> | |
<version>2.13.0</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Apache.Ignite.Core; | |
var cfg = new IgniteConfiguration | |
{ | |
JavaPeerClassLoadingEnabled = true, | |
JvmClasspath = "/home/pavel/w/tests/IgniteMixedClusterInvoke/target/classes", | |
ClientMode = true | |
}; | |
var ignite = Ignition.Start(cfg); | |
var cache = ignite.GetOrCreateCache<int, int>("myCache"); | |
cache[10] = 20; | |
var localNode = ignite.GetCluster().ForLocal(); | |
var res = localNode.GetCompute().ExecuteJavaTask<int>("EntryProcessorComputeTask", 10); | |
Console.WriteLine("Result = " + res); | |
Console.WriteLine("cache[10] = " + cache[10]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment