Apache Ignite:缓存的简单使用

Apache Ignite是什么

官方文档:一个以内存为中心的分布式数据库、缓存和处理平台,可以在PB级数据中,以内存级的速度进行事务性、分析性以及流式负载的处理。

现在的关注点是分布式缓存数据库:redis也是一个分布式缓存,但是只支持键值对的储存方式;关系型数据库或文档数据库,有完善的查询语法,但是相对于缓存数据库来说速度较慢;如果有需要经常条件查询的数据,可以用到Apache Ignite的缓存数据库。

Apache Ignite:缓存的简单使用_第1张图片

Apache Ignite的安装与启动

  • 安装

1.到官网下载zip格式文件解压即可(除了二进制发行版,Ignite还支持源代码安装、docker、云镜像以及RPM格式)

  • 启动

1.在启动之前,你可以先设置设置client模式,默认的Ignite实例以server模式加入集群。可以启动任意多个节点,它们之间会自动发现;

在这个文件配置

vi /data/ignite/apache-ignite-fabric-2.6.0-bin/config/default-config.xml

 其实就是用spring注入的方式修改配置类

 
		

 2.然后是关于集群的。不同服务器的集群发现机制。Ignite支持很多种集群发现机制,这里配置的是静态的,同样在这个文件,继续配置。 


		
		
			
				
					
						
						
						  xxx.xxx.xxx.xxx:47500..47509
                          xxx.xxx.xxx.xxx:47500..47509
						
						
					
				
			
		
	

$IGNITE_HOME/bin/ignite.sh,启动即可。

客户端

 

  • REST客户端。Restful风格的请求方式。键值对的存储方式

1.需要拷贝$IGNITE_HOME/libs/options/ignite-rest-httpjarlibs中。用curl命令(直接在linux命令行输入)

2.创建一个缓存

curl 'http://localhost:8080/ignite?cmd=getorcreate&cacheName=myfirstcache'

3.存值到缓存

curl 'http://localhost:8080/ignite?cmd=put&key=name&val=jey&cacheName=myfirstcache'

4.根据key从缓存中获取值

curl 'http://localhost:8080/ignite?cmd=get&key=name&cacheName=myfirstcache'
  • SQL客户端 

1.下载连接工具Dbeaver

2.配置ignite驱动

3.创建表及一个索引(1,如果libs目录下没有ignite-indexing,需要从libs/options拷贝到libs,否则索引不生效;2,不支持二级索引。)

CREATE TABLE city (id LONG PRIMARY KEY, name VARCHAR)
  WITH "template=replicated"

CREATE TABLE weather (id LONG, survey VARCHAR, city_id LONG, PRIMARY KEY (id, city_id))
  WITH "backups=1, affinityKey=city_id"

CREATE INDEX idx_city_id ON weather (city_id)

然后后面的增删改查都是使用SQl语法。与关系型数据库不同的是,数据是存储到缓存里面。 

  • JAVA客户端

 简单搭建一个spring-boot的项目,引入apache-ignite的maven


		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.apache.ignite
			ignite-spring-data
			2.6.0
		
		
			org.apache.ignite
			ignite-indexing
			2.6.0
		
		
			org.apache.ignite
			ignite-core
			2.6.0
		

		
			org.apache.ignite
			ignite-spring
			2.6.0
		

		
			org.apache.ignite
			ignite-indexing
			2.6.0
		
		
			junit
			junit
			test
		

 写一个测试类,连接ignite缓存数据库。和mysql或oracle一样,也需要一个驱动类org.apache.ignite.IgniteJdbcThinDriver及连接地址,至于账号和密码,如果不设置可以忽略。

代码如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Before;
import org.junit.Test;

public class SqlDemo {
	private Connection conn;
	private Statement stmt;
	private PreparedStatement PStmt;

	@Before
	public void init() throws Exception {
		Class.forName("org.apache.ignite.IgniteJdbcThinDriver");
		conn = DriverManager.getConnection("jdbc:ignite:thin://10.10.102.183/");
		stmt = conn.createStatement();
	}
	
	
	@Test
	public void igniteAdd() {
		try {
			PStmt = conn.prepareStatement("INSERT INTO City (id, name) VALUES (?, ?)");
			PStmt.setLong(1, 4L);
			PStmt.setString(2, "深圳");
			PStmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void igniteQuery() throws SQLException {
		ResultSet executeQuery = stmt.executeQuery("SELECT * FROM CITY");
		while(executeQuery.next()) {
			System.out.println("id:"+executeQuery.getLong("id")+".name:"+executeQuery.getString("name"));
		}
	}
}

最后

apache-ignite的缓存也可以像redis一样进行持久化,当然你也根据自己的需要持久化(存到mysql还是mongodb).另外apache-ignite的功能远不止这些,更多亮点请看李玉珏翻译的apache-ignite中文文档

你可能感兴趣的:(java类)