简体   繁体   English

无法使用 Apache Beam Java sdk 连接到 PulsarIO

[英]Not able to connect to PulsarIO using Apache Beam Java sdk

while executing below code to connect to apache pulsar using apache beam PulsarIO in java sdk. Getting below error while adding pulsar client in beam pipeline.在执行以下代码时使用 apache pulsar 在 java sdk 中使用 apache beam PulsarIO 连接到 apache pulsar。在 beam 管道中添加 pulsar 客户端时出现以下错误。

Beam version 2.40, 2.41 javaSE 1.8光束版本 2.40、2.41 javaSE 1.8

import java.io.*;
import org.apache.beam.sdk.*;
import org.apache.beam.io.pulsar.*;
import org.apache.beam.sdk.options.*;
import org.apache.beam.sdk.transforms.*;
import org.apache.beam.sdk.values.*;
import org.apache.beam.transforms.SerializableFunction;
import org.apache.pulsar.client.api.*;
import org.apache.pulsar.client.impl.PulsarClientImpl;
import org.apache.pulsar.client.impl.auth.AuthenticationTls;
import java.util.*;

public class BeamPulsar{
 public static void main(String args[]) throws PulsarClientException{
 PipelineOptions options=PipelineOptionsFactory.as(PipelineOptions.class);
 Pipeline p= Pipeline.create(options=options);
 PulsarClient pulsarClient;

 String TLSCERTFILE='filepath'
 String TLSKEYFILE='filepath'
 String CACERTFILE='filepath'
 String BROKER_URL='pulsar+ssl://'
 String TOPIC_NAME='topic'
 Map<String, String> authParams=new HashMap<>();
 authParams.put("tlsCertFile",TLSCERTFILE);
 authParams.put("tlsKeytFile",TLSKEYFILE);
 Authentication tlsAuth =AuthenticationFactory.create(AuthenticationTls.class.getName(),authParams);
 pulsarClient=PulsarClient.builder().serviceUrl(BROKER_URL).tlsTrustCertsFilePath(CACERTFILE).authentication(tlsAuth).build()
PCollection<PulsarMessage> records=p.apply("read from pulsar", PulsarIO.read()
                                    .withTopic(TOPIC_NAME)
                                    .withPulsarClient((SerializableFunction<String, PulsarClient>)pulsarClient)
                                    .withPublishTime()
                                    .withClientUrl(BROKER_URL)
                                    .withAdminUrl(BROKER_URL));
p.run().waitUntilFinish();
}}

Error: Exception in thread "main" java.lang.ClassException: class org.apache.pulsar.client.impl.PulsarClientImpl cannot be cast to class org.apache.beam.sdk.transform.SerializableFunction(org.apache.pulsar.client.impl.PulsarClientImpl and org.apache.beam.sdk.transforms.SerializableFunction are in unnamed module of loader app) Error: Exception in thread "main" java.lang.ClassException: class org.apache.pulsar.client.impl.PulsarClientImpl cannot be cast to class org.apache.beam.sdk.transform.SerializableFunction(org.apache.pulsar.client .impl.PulsarClientImpl 和 org.apache.beam.sdk.transforms.SerializableFunction 在加载器应用程序的未命名模块中)

You will want to put the construction of the PulsarClient inside of an implementation of SerializableFunction and not in your main.您将希望将 PulsarClient 的构造放在 SerializableFunction 的实现中,而不是放在您的 main 中。 The client needs to be constructed in the remote process actually running the IO code, not the machine that submits the pipeline.客户端需要构建在实际运行IO代码的远程进程中,而不是提交管道的机器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM