第 3 章 查询远程缓存
您可以在 Data Grid 服务器上索引和查询远程缓存。
3.1. 从 Hot Rod Java 客户端查询缓存
通过数据网格,您可以以编程方式通过 Hot Rod 端点从 Java 客户端查询远程缓存。此流程解释了如何索引查询存储 Book
实例的远程缓存。
先决条件
-
将 ProtoStream 处理器添加到
pom.xml
中。
Data Grid 为 @ProtoField
注释提供此处理器,以便您可以生成 Protobuf 模式并执行查询。
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>...</version> <configuration> <annotationProcessorPaths> <annotationProcessorPath> <groupId>org.infinispan.protostream</groupId> <artifactId>protostream-processor</artifactId> <version>...</version> </annotationProcessorPath> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
流程
在类中添加索引注解,如下例所示:
Book.java
import org.infinispan.api.annotations.indexing.Basic; import org.infinispan.api.annotations.indexing.Indexed; import org.infinispan.api.annotations.indexing.Text; import org.infinispan.protostream.annotations.ProtoFactory; import org.infinispan.protostream.annotations.ProtoField; @Indexed public class Book { @Text @ProtoField(number = 1) final String title; @Text @ProtoField(number = 2) final String description; @Basic @ProtoField(number = 3, defaultValue = "0") final int publicationYear; @ProtoFactory Book(String title, String description, int publicationYear) { this.title = title; this.description = description; this.publicationYear = publicationYear; } // public Getter methods omitted for brevity }
在新类中实施
SerializationContextInitializer
接口,然后添加@ProtoSchema
注释。-
使用
includeClasses
参数引用包含@ProtoField
注解的类。 -
定义 Protobuf 模式的名称,该模式使用
schemaFileName
和schemaFilePath
参数生成文件系统路径。 使用
schemaPackageName
参数指定 Protobuf 模式的软件包名称。RemoteQueryInitializer.java
import org.infinispan.protostream.SerializationContextInitializer; import org.infinispan.protostream.annotations.ProtoSchema; @ProtoSchema( includeClasses = { Book.class }, schemaFileName = "book.proto", schemaFilePath = "proto/", schemaPackageName = "book_sample") public interface RemoteQueryInitializer extends SerializationContextInitializer { }
-
使用
编译您的项目。
此流程中的代码示例生成
proto/book.proto
模式,以及 annotatedBook
类的RemoteQueryInitializerImpl.java
实现。
后续步骤
创建一个远程缓存,将 Data Grid 配置为索引您的实体。例如,以下远程缓存会在您在上一步中生成的 书.proto
模式中索引了 Book
实体:
<replicated-cache name="books"> <indexing> <indexed-entities> <indexed-entity>book_sample.Book</indexed-entity> </indexed-entities> </indexing> </replicated-cache>
以下 RemoteQuery
类执行以下操作:
-
使用 Hot Rod Java 客户端注册
RemoteQueryInitializerImpl
序列化上下文。 -
将 Protobuf 模式(
书.proto
)注册到 Data Grid Server。 -
将两个
Book
实例添加到远程缓存中。 - 根据标题中的关键字,执行与书书匹配的全文本查询。
RemoteQuery.java
package org.infinispan; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import org.infinispan.client.hotrod.RemoteCache; import org.infinispan.client.hotrod.RemoteCacheManager; import org.infinispan.client.hotrod.Search; import org.infinispan.client.hotrod.configuration.ConfigurationBuilder; import org.infinispan.query.dsl.Query; import org.infinispan.query.dsl.QueryFactory; import org.infinispan.query.remote.client.ProtobufMetadataManagerConstants; public class RemoteQuery { public static void main(String[] args) throws Exception { ConfigurationBuilder clientBuilder = new ConfigurationBuilder(); // RemoteQueryInitializerImpl is generated clientBuilder.addServer().host("127.0.0.1").port(11222) .security().authentication().username("user").password("user") .addContextInitializers(new RemoteQueryInitializerImpl()); RemoteCacheManager remoteCacheManager = new RemoteCacheManager(clientBuilder.build()); // Grab the generated protobuf schema and registers in the server. Path proto = Paths.get(RemoteQuery.class.getClassLoader() .getResource("proto/book.proto").toURI()); String protoBufCacheName = ProtobufMetadataManagerConstants.PROTOBUF_METADATA_CACHE_NAME; remoteCacheManager.getCache(protoBufCacheName).put("book.proto", Files.readString(proto)); // Obtain the 'books' remote cache RemoteCache<Object, Object> remoteCache = remoteCacheManager.getCache("books"); // Add some Books Book book1 = new Book("Infinispan in Action", "Learn Infinispan with using it", 2015); Book book2 = new Book("Cloud-Native Applications with Java and Quarkus", "Build robust and reliable cloud applications", 2019); remoteCache.put(1, book1); remoteCache.put(2, book2); // Execute a full-text query Query<Book> query = remoteCache.query("FROM book_sample.Book WHERE title:'java'"); List<Book> list = query.execute().list(); // Voila! We have our book back from the cache! } }