Apache-Ignite入門實戰之一

來源:互聯網
上載者:User

Apache-Ignite入門實戰之一 簡介

Apache Ignite 記憶體資料群組織架構是一個高效能、整合化和分布式的記憶體計算和事務平台,用於大規模的資料集處理,比傳統的基於磁碟或快閃記憶體的技術具有更高的效能,同時他還為應用和不同的資料來源之間提供高效能、分布式記憶體中資料群組織管理的功能。 安裝

從 https://ignite.apache.org/download.cgi#binaries 下載最新的安裝包,這裡我下載的是 apache-ignite-fabric-2.3.0-bin.zip 包。下載後解壓就可以直接使用了。 運行

進入到 ${IGNITE_HOME}/bin 目錄,然後運行

./ignite.sh...[00:24:04] To start Console Management & Monitoring run ignitevisorcmd.{sh|bat}[00:24:04][00:24:04] Ignite node started OK (id=01af1a02)[00:24:04] Topology snapshot [ver=1, servers=1, clients=0, CPUs=2, heap=1.0GB]...

其中:
- servers=1 表示當前 Ignite 叢集中只有一個節點。
- clients=0 表示當前沒有用戶端串連到此叢集。

此時,我們可以在另外一台機器上運行同樣的命令來再啟動一個 Ignite,此時我們就可以看到

...[00:41:21] Topology snapshot [ver=2, servers=2, clients=0, CPUs=2, ......

可以看到 servers=2,說明有一個新節點加入了叢集。 測試

Ignite 叢集已經有了,下面我們來看看怎樣使用 Ignite 作為分布式緩衝系統使用。

首先建立一個 Maven 工程,pom.xml 檔案內容如下:

<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/maven-v4_0_0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>my.ignitestudy</groupId>    <artifactId>ignitestudy</artifactId>    <packaging>jar</packaging>    <version>1.0-SNAPSHOT</version>    <name>ignitestudy</name>    <url>http://maven.apache.org</url>    <properties>        <ignite.version>2.3.0</ignite.version>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>    <dependencies>        <dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-lang3</artifactId>            <version>3.1</version>        </dependency>        <dependency>            <groupId>net.sf.json-lib</groupId>            <artifactId>json-lib</artifactId>            <version>2.4</version>            <classifier>jdk15</classifier>        </dependency>        <dependency>            <groupId>org.apache.ignite</groupId>            <artifactId>ignite-core</artifactId>            <version>${ignite.version}</version>        </dependency>        <dependency>            <groupId>org.apache.ignite</groupId>            <artifactId>ignite-spring</artifactId>            <version>${ignite.version}</version>        </dependency>        <dependency>            <groupId>org.apache.ignite</groupId>            <artifactId>ignite-indexing</artifactId>            <version>${ignite.version}</version>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>3.8.1</version>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>2.3.2</version>                <configuration>                    <source>1.8</source>                    <target>1.8</target>                    <encoding>UTF-8</encoding>                    <compilerArguments>                        <extdirs>${project.basedir}/lib</extdirs>                    </compilerArguments>                </configuration>            </plugin>            <plugin>                <artifactId>maven-assembly-plugin</artifactId>                <configuration>                    <descriptorRefs>                        <descriptorRef>jar-with-dependencies</descriptorRef>                    </descriptorRefs>                </configuration>                <executions>                    <execution>                        <id>make-assembly</id>                        <phase>package</phase>                        <goals>                            <goal>single</goal>                        </goals>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>

下面是 Java 中怎樣使用 Cache 的例子

package my.ignitestudy.datagrid;import org.apache.ignite.Ignite;import org.apache.ignite.IgniteCache;import org.apache.ignite.Ignition;import org.apache.ignite.configuration.IgniteConfiguration;import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;import org.apache.ignite.transactions.Transaction;import java.util.Arrays;public class Test {    public static void main( String[] args ) throws Exception {        Ignite ignite = getIgnite();        testGetPut(ignite);        testAtomOperation(ignite);    }    private static Ignite getIgnite() {        TcpDiscoverySpi spi = new TcpDiscoverySpi();        TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();        ipFinder.setAddresses(Arrays.asList("192.168.0.192:47500..47509"));        spi.setIpFinder(ipFinder);        IgniteConfiguration cfg = new IgniteConfiguration();        cfg.setDiscoverySpi(spi);        cfg.setClientMode(true);        Ignite ignite = Ignition.start(cfg);        return ignite;    }    private static void testGetPut(Ignite ignite) {        IgniteCache<String, String> cache = ignite.getOrCreateCache("myCache");        for (int i = 0; i < 10; i++) {            cache.put("mykey_" + i, "myvalue_" + i);        }        for (int i = 0; i < 10; i++) {            String key = "mykey_" + i;            System.out.println("Got [key=" + key + ", val=" + cache.get(key) + ']');        }    }    private static void testAtomOperation(Ignite ignite) {        IgniteCache<String, Integer> cache = ignite.getOrCreateCache("myCache");        Integer oldValue = cache.getAndPutIfAbsent("MyKey", 11);        System.out.println("MyKey: " + oldValue);        boolean success = cache.putIfAbsent("MyKey", 22);        System.out.println("MyKey: " + success);        oldValue = cache.getAndReplace("MyKey", 11);        System.out.println("MyKey replace: " + oldValue);        success = cache.replace("MyKey", 22);        System.out.println("MyKey replace: " + success);        success = cache.replace("MyKey", 2, 22);        System.out.println("MyKey replace: " + success);        success = cache.remove("MyKey", 1);        System.out.println("MyKey remove: " + success);    }}

上面 Java 代碼中我們是使用編程的方式來串連叢集,如下:

TcpDiscoverySpi spi = new TcpDiscoverySpi();TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();ipFinder.setAddresses(Arrays.asList("192.168.0.192:47500..47509"));spi.setIpFinder(ipFinder);IgniteConfiguration cfg = new IgniteConfiguration();cfg.setDiscoverySpi(spi);cfg.setClientMode(true);Ignite ignite = Ignition.start(cfg);

也可以使用指定的設定檔來擷取到叢集的串連,比如:

Ignite ignite = Ignition.start("... config file path ...");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.