Ignite实践一:配置集群及API开发

中文文档:https://liyuj.gitee.io/doc/java/

一、部署

1、官网下载二进制包并解压,这里使用2.7.0版本;

2、修改~/.bash_profile.sh文件,指定IGNITE_HOME路径,并立即生效(source ~/.bash_profile.sh);

3、配置成集群形式启动,这里list属性填写了多个IP地址,修改conf路径下的配置文件:default-config.xml。如下:

<?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 Spring configuration file to startup Ignite cache.

    This file demonstrates how to configure cache using Spring. Provided cache
    will be created on node startup.

    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/example-cache.xml

    When starting Ignite from Java IDE, pass path to this file to Ignition:
    Ignition.start("examples/config/example-cache.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">
    <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
        <property name="cacheConfiguration">
            <list>
                <!-- Partitioned cache example configuration (Atomic mode). -->
                <bean class="org.apache.ignite.configuration.CacheConfiguration">
                    <property name="name" value="default"/>
                    <property name="atomicityMode" value="ATOMIC"/>
                    <property name="backups" value="1"/>
                </bean>
            </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>0.0.0.155:47500..47509</value>
                                <value>0.0.0.156:47500..47509</value>
                                <value>0.0.0.157:47500..47509</value>
                            </list>
                        </property>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
</beans>

4、启动:在bin目录执行nohub ./ignite.sh ../config/default-config.xml,可以看见随着多个节点的启动,节点将逐个加入集群,其中一个运行日志打印的日志如下:

一、API开发

1、Ignite的官方文档中描述的其特性比较全面,当前研究其分布式数据库特性,使用IDEA+Maven搭建开发环境,maven的pom.xml文件如下:

<?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>cmos</groupId>
  <artifactId>IgniteThree</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>IgniteThree</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <ignite.version>2.7.0</ignite.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </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>

  </dependencies>


  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5.5</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>cmos.IgniteDB</mainClass>
            </manifest>
          </archive>
          <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>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>8</source>
          <target>8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

2、构建班级实体类Class和学生类Student,并进行查询操作;

package xx;

import org.apache.ignite.cache.query.annotations.QuerySqlField;

public class Class
{
    @QuerySqlField(index = true)
    private int id;

    @QuerySqlField
    private String name;

    private int level;

    public Class(int id, String name)
    {
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public int getLevel() {
        return level;
    }
    public void setLevel(int level) {
        this.level = level;
    }
    @Override
    public String toString() {
        return "Class [id=" + id + ", name=" + name + ", level=" + level + "]";
    }
}
package xx;

import org.apache.ignite.cache.query.annotations.QuerySqlField;

public class Student
{
    @QuerySqlField
    private int classId;

    @QuerySqlField
    private String name;

    @QuerySqlField
    private int age;

    public Student(int classId, String name, int age)
    {
        this.classId = classId;
        this.name = name;
        this.age = age;
    }

    public int getClassId() {
        return classId;
    }

    public void setClassId(int classId) {
        this.classId = classId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [classId=" + classId + ", name=" + name + ", age="
                + age + "]";
    }
}

2、主类

package xxx;
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.query.QueryCursor;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.configuration.CacheConfiguration;
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 java.util.Arrays;
import java.util.List;
public class IgniteDB
{
    public static void main(String[] args)
    {

       // 使用编程的方式来连接集群
        TcpDiscoverySpi spi = new TcpDiscoverySpi();
        TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
        ipFinder.setAddresses(Arrays.asList("172.19.72.155:47500..47509","172.19.72.156:47500..47509","172.19.72.157:47500..47509"));
        spi.setIpFinder(ipFinder);
        IgniteConfiguration cfg = new IgniteConfiguration();
        cfg.setDiscoverySpi(spi);
        cfg.setClientMode(true);

        //进行数据开发
        Ignite ignite = Ignition.start(cfg);
        CacheConfiguration<Integer, Class> classCfg = new CacheConfiguration<Integer, Class>();
        classCfg.setName("CLA");
        classCfg.setCacheMode(CacheMode.PARTITIONED);
        classCfg.setIndexedTypes(Integer.class, Class.class);
        IgniteCache<Integer, Class> classCache = ignite.getOrCreateCache(classCfg);
        classCache.put(1, new Class(1, "五年级一班"));
        classCache.put(2, new Class(2, "五年级二班"));

        CacheConfiguration<Integer, Student> stuCfg = new CacheConfiguration<Integer, Student>();
        stuCfg.setName("STU");
        stuCfg.setCacheMode(CacheMode.PARTITIONED);
        stuCfg.setIndexedTypes(Integer.class, Student.class);
        IgniteCache<Integer, Student> stuCache = ignite.getOrCreateCache(stuCfg);
        stuCache.put(1, new Student(1, "张三", 10));
        stuCache.put(2, new Student(1, "李四", 11));
        stuCache.put(3, new Student(2, "王五", 11));
        stuCache.put(4, new Student(2, "胜七", 10));

        SqlFieldsQuery sql = new SqlFieldsQuery(
                "select concat(stu.classId, '----', stu.name) as stuinfo"
                        + " "
                        + "from Student as stu "
                        + "");

        QueryCursor<List<?>> cursor = stuCache.query(sql);
        for (List<?> row : cursor)
        {
            System.out.println("学生信息:" + row.get(0));
        }

        SqlFieldsQuery sql1 = new SqlFieldsQuery(
                "select concat(cla.id, '----', cla.name) as clainfo"
                        + ", concat(stu.name, '----', stu.age) as stuinfo "
                        + "from Class as cla, STU.Student as stu "
                        + "where cla.id = stu.classId");

        QueryCursor<List<?>> cursor1 = classCache.query(sql1);
        for (List<?> row : cursor1)
        {
            System.out.println("班级信息:" + row.get(0) + ", 学生信息:" + row.get(1));
        }

    }
}

3、直接jar包形式运行:java -jar IgniteDB.jar ;

发布了62 篇原创文章 · 获赞 158 · 访问量 32万+

猜你喜欢

转载自blog.csdn.net/yezonggang/article/details/97680396