第 3 章 查询远程缓存
您可以在 Data Grid 服务器上索引和查询远程缓存。
3.1. 从 Hot Rod Java 客户端查询缓存
Data Grid 可让您通过 Hot Rod 端点,以编程方式从 Java 客户端查询远程缓存。此流程解释了如何索引查询存储书签实例的远程缓存。
先决条件
-
将 ProtoStream 处理器添加到
pom.xml
中。
Data Grid 为 @ProtoField
和 @ProtoDoc
注解提供此处理器,以便您可以生成 Protobuf 模式并执行查询。
<dependencyManagement> <dependencies> <dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-bom</artifactId> <version>${version.infinispan}</version> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.infinispan.protostream</groupId> <artifactId>protostream-processor</artifactId> <scope>provided</scope> </dependency> </dependencies>
流程
在您的类中添加索引注解,如下例所示:
Book.java
import org.infinispan.protostream.annotations.ProtoDoc; import org.infinispan.protostream.annotations.ProtoFactory; import org.infinispan.protostream.annotations.ProtoField; @ProtoDoc("@Indexed") public class Book { @ProtoDoc("@Field(index=Index.YES, analyze = Analyze.YES, store = Store.NO)") @ProtoField(number = 1) final String title; @ProtoDoc("@Field(index=Index.YES, analyze = Analyze.YES, store = Store.NO)") @ProtoField(number = 2) final String description; @ProtoDoc("@Field(index=Index.YES, analyze = Analyze.YES, store = Store.NO)") @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
接口,然后添加@AutoProtoSchemaBuilder
注释。-
使用
includeClasses
参数引用包含@ProtoField
和@ProtoDoc
注解的类。 -
使用
schemaFileName
和schemaFilePath
参数定义您生成的和文件系统路径的 Protobuf 模式的名称。 使用
schemaPackageName
参数指定 Protobuf 模式的软件包名称。RemoteQueryInitializer.java
import org.infinispan.protostream.SerializationContextInitializer; import org.infinispan.protostream.annotations.AutoProtoSchemaBuilder; @AutoProtoSchemaBuilder( includeClasses = { Book.class }, schemaFileName = "book.proto", schemaFilePath = "proto/", schemaPackageName = "book_sample") public interface RemoteQueryInitializer extends SerializationContextInitializer { }
-
使用
编译您的项目。
此流程中的代码示例生成
proto/book.proto
模式和注释Book
类的RemoteQueryInitializerImpl.java
实现。
后续步骤
创建一个远程缓存,将数据仓库配置为索引您的实体。例如,以下远程缓存会索引您在上一步中生成的 book.proto
模式中的书签实体:
<replicated-cache name="books"> <indexing> <indexed-entities> <indexed-entity>book_sample.Book</indexed-entity> </indexed-entities> </indexing> </replicated-cache>
以下 RemoteQuery
类执行以下操作:
-
将
RemoteQueryInitializerImpl
serialization 上下文注册到 Hot Rod Java 客户端。 -
将 Protobuf 模式
书.proto
注册到 Data Grid Server。 -
将两个书签
实例
添加到远程缓存中。 - 执行与标题中的关键字匹配的全文本查询。
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 QueryFactory queryFactory = Search.getQueryFactory(remoteCache); Query<Book> query = queryFactory.create("FROM book_sample.Book WHERE title:'java'"); List<Book> list = query.execute().list(); // Voila! We have our book back from the cache! } }
其他资源
- Marshalling 和 Encoding Data,以了解有关创建序列化上下文和注册 Protobuf 模式的更多信息。
-
ProtoStream 注解,以了解有关
@ProtoField
、@ProtoDoc
和@AutoProtoSchemaBuilder
注解的更多信息。