如何在gcp memorystore redis示例中安装RedSearch模块

k4emjkb1  于 2021-06-09  发布在  Redis
关注(0)|答案(2)|浏览(471)

我需要在gcp memorystore redis示例上安装redsearch模块。

我遵循以下步骤: docker run -p 6379:6379 redislabs/redisearch:latest 我将这个docker映像推送到kubernetes集群中,并公开了外部ip。我使用了这个外部ip和6379端口作为我的应用程序的配置,但我无法连接到重新搜索。

代码:

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.PipelineResult;
import org.apache.beam.sdk.options.Default;
import org.apache.beam.sdk.options.Description;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.ParDo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.redisearch.client.Client;
import io.redisearch.*;

public class RediSearch {

    static Client client = new Client("testdoc1", "clusteripaddress", 8097);
    private static final Logger LOG = LoggerFactory.getLogger(RediSearch.class);

    public interface Options extends PipelineOptions {

        @Description("gcp project id.")
        @Default.String("XXXX")
        String getProjectId();
        void setProjectId(String projectId);
    }

    public static PipelineResult run(Options options) throws IOException {

        Pipeline pipeline = Pipeline.create(options);

        pipeline.apply(Create.of("test"))
        .apply(ParDo.of(new DoFn<String, String>() {
            private static final long serialVersionUID = 1L;
            @ProcessElement
            public void processElement(ProcessContext c) throws Exception {     
                String pubsubmsg = c.element();                 

                Schema sc = new Schema()
                .addTextField("title", 5.0)
                .addTextField("body", 1.0)
                .addNumericField("price");

            client.createIndex(sc, Client.IndexOptions.Default());

            Map<String, Object> fields = new HashMap<String, Object>();
            fields.put("title", "hello world");
            fields.put("body", "lorem ipsum");
            fields.put("price", 800);
            fields.put("price", 1337);
            fields.put("price", 2000);

            client.addDocument("searchdoc3", fields);

            SearchResult[] res = client.searchBatch(new Query("hello world").limit(0, 5).setWithScores());      

            for (Document d : res[0].docs) {
                LOG.info("redisearchlog{}",d.getId().startsWith("search"));
                LOG.info("redisearchlog1{}",d.getProperties());
                LOG.info("redisearchlog2{}",d.toString());
            }

            }
        }));    

        return pipeline.run();
    }

    public static void main(String[] args) throws IOException {

        Options options = PipelineOptionsFactory.fromArgs(args).as(Options.class);
        run(options);

    }
}

错误:

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.jedis.util.Pool.getResource(Pool.java:59)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:234)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:15)
at io.redisearch.client.Client._conn(Client.java:137)
at io.redisearch.client.Client.getAllConfig(Client.java:275)
at com.testing.redisearch.RediSearch$1.processElement(RediSearch.java:59)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host xxxxxxxxxxx:6379
at redis.clients.jedis.Connection.connect(Connection.java:204)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:100)
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1894)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:117)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:889)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:424)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:349)
at redis.clients.jedis.util.Pool.getResource(Pool.java:50)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:234)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:15)
at io.redisearch.client.Client._conn(Client.java:137)
at io.redisearch.client.Client.getAllConfig(Client.java:275)
at com.testing.redisearch.RediSearch$1.processElement(RediSearch.java:59)
at com.testing.redisearch.RediSearch$1$DoFnInvoker.invokeProcessElement(Unknown Source)
at org.apache.beam.runners.dataflow.worker.repackaged.org.apache.beam.runners.core.SimpleDoFnRunner.invokeProcessElement(SimpleDoFnRunner.java:218)
at org.apache.beam.runners.dataflow.worker.repackaged.org.apache.beam.runners.core.SimpleDoFnRunner.processElement(SimpleDoFnRunner.java:183)
at org.apache.beam.runners.dataflow.worker.SimpleParDoFn.processElement(SimpleParDoFn.java:335)
at org.apache.beam.runners.dataflow.worker.util.common.worker.ParDoOperation.process(ParDoOperation.java:44)
at org.apache.beam.runners.dataflow.worker.util.common.worker.OutputReceiver.process(OutputReceiver.java:49)
at org.apache.beam.runners.dataflow.worker.util.common.worker.ReadOperation.runReadLoop(ReadOperation.java:201)
at org.apache.beam.runners.dataflow.worker.util.common.worker.ReadOperation.start(ReadOperation.java:159)
at org.apache.beam.runners.dataflow.worker.util.common.worker.MapTaskExecutor.execute(MapTaskExecutor.java:77)
at org.apache.beam.runners.dataflow.worker.BatchDataflowWorker.executeWork(BatchDataflowWorker.java:411)
at org.apache.beam.runners.dataflow.worker.BatchDataflowWorker.doWork(BatchDataflowWorker.java:380)
at org.apache.beam.runners.dataflow.worker.BatchDataflowWorker.getAndPerformWork(BatchDataflowWorker.java:305)
at org.apache.beam.runners.dataflow.worker.DataflowBatchWorkerHarness$WorkerThread.doWork(DataflowBatchWorkerHarness.java:140)
at org.apache.beam.runners.dataflow.worker.DataflowBatchWorkerHarness$WorkerThread.call(DataflowBatchWorkerHarness.java:120)
at org.apache.beam.runners.dataflow.worker.DataflowBatchWorkerHarness$WorkerThread.call(DataflowBatchWorkerHarness.java:107)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at redis.clients.jedis.Connection.connect(Connection.java:181)
... 31 more

任何解决方案都是值得赞赏的。

f0ofjuux

f0ofjuux1#

根据错误有多种原因 JedisConnectionException: Could not get a resource from the pool . 根据这个问题的答案,问题是无法建立到redisearch的连接,可能是因为redis没有运行、连接超时或无法分配。
不管怎样,我注意到即使您在6379端口部署redis,在您的代码中您也试图在8097端口访问它。请将客户端声明更改为以下内容,然后重试连接。

static Client client = new Client("testdoc1", "<cluster_ip_address>", 6379);
i86rm4rw

i86rm4rw2#

如果您希望在memorystore示例中使用RedSearch模块,那么似乎还不支持该模块。您可以在这里的googlecloud文档中看到,在撰写本文时,甚至对于版本5.0,redis模块也不受支持。

相关问题