Google cloud dataflow Truststore和Google云数据流

Google cloud dataflow Truststore和Google云数据流,google-cloud-dataflow,Google Cloud Dataflow,我需要使用一个信任存储来在Google云数据流中建立SSL Kafka连接。我可以从一个存储桶中提供,还是有办法将其存储在“本地文件系统”中?您可以使用它提供一个工厂函数,该函数将被调用以创建Kafka使用者。在该功能中,您可以自由地执行任何您喜欢的操作,例如,您可以从GCS存储桶下载信任存储文件(我建议使用),并将其保存到本地磁盘上的临时文件-AFAIK Kafka本身仅支持将此文件保存在本地磁盘上。然后手动创建一个KafkaConsumer并将其指向文件。感谢@jkff提供解决方案,下面是一

我需要使用一个信任存储来在Google云数据流中建立SSL Kafka连接。我可以从一个存储桶中提供,还是有办法将其存储在“本地文件系统”中?

您可以使用它提供一个工厂函数,该函数将被调用以创建Kafka使用者。在该功能中,您可以自由地执行任何您喜欢的操作,例如,您可以从GCS存储桶下载信任存储文件(我建议使用),并将其保存到本地磁盘上的临时文件-AFAIK Kafka本身仅支持将此文件保存在本地磁盘上。然后手动创建一个
KafkaConsumer
并将其指向文件。

感谢@jkff提供解决方案,下面是一个实现示例:

ConsumerFactoryFn实现示例:

    private static class ConsumerFactoryFn
        implements SerializableFunction<Map<String, Object>, Consumer<byte[], byte[]>> 
{



    public Consumer<byte[], byte[]> apply(Map<String, Object> config) 
    {
        try 
        {
            Storage storage = StorageOptions.newBuilder()
                    .setProjectId("prj-id-of-your-bucket")
                    .setCredentials(GoogleCredentials.getApplicationDefault())
                    .build()
                    .getService();
            Blob blob = storage.get("your-bucket-name", "pth.to.your.kafka.client.truststore.jks");
            ReadChannel readChannel = blob.reader();
            FileOutputStream fileOuputStream;
            fileOuputStream = new FileOutputStream("/tmp/kafka.client.truststore.jks"); //path where the jks file will be stored
            fileOuputStream.getChannel().transferFrom(readChannel, 0, Long.MAX_VALUE);
            fileOuputStream.close();
            File f = new File("/tmp/kafka.client.truststore.jks"); //assuring the store file exists
            if (f.exists())
            {
                LOG.debug("key exists");

            }
            else
            {
                LOG.error("key does not exist");

            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            LOG.error( e.getMessage());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            LOG.error( e.getMessage());
        }


        config.put("ssl.truststore.location",(Object) "/tmp/kafka.client.truststore.jks" );

        return new KafkaConsumer<byte[], byte[]>(config);
    }
}
私有静态类ConsumerFactoryFn
实现SerializableFunction
{
公共消费者应用(地图配置)
{
尝试
{
Storage Storage=StorageOptions.newBuilder()
.setProjectId(“您的存储桶的prj id”)
.setCredentials(GoogleCredentials.getApplicationDefault())
.build()
.getService();
Blob Blob=storage.get(“您的bucket名称”、“pth.to.your.kafka.client.truststore.jks”);
ReadChannel ReadChannel=blob.reader();
FileOutputStream FileOutputStream;
FileOutputStream=newfileoutputstream(“/tmp/kafka.client.truststore.jks”);//存储jks文件的路径
FileOutStream.getChannel().transferFrom(readChannel,0,Long.MAX_值);
fileOutStream.close();
File f=new File(“/tmp/kafka.client.truststore.jks”);//确保存储文件存在
如果(f.exists())
{
调试(“密钥存在”);
}
其他的
{
日志错误(“密钥不存在”);
}
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
LOG.error(例如getMessage());
}捕获(IOE异常){
//TODO自动生成的捕捉块
LOG.error(例如getMessage());
}
config.put(“ssl.truststore.location”,(Object)”/tmp/kafka.client.truststore.jks);
返回新的卡夫卡消费者(配置);
}
}
不要忘了在KafkaIO.read()调用中使用.withConsumerFactoryFn,应该类似于:

Map<String, Object> configMap = new HashMap<String, Object>();
configMap.put("security.protocol", (Object) "SSL");
configMap.put("ssl.truststore.password", (Object) "clientpass");

p.apply("ReadFromKafka", KafkaIO.<String, String>read()
            .withBootstrapServers("ip:9093")
            .withTopic("pageviews")
            .withKeyDeserializer(StringDeserializer.class)
            .withValueDeserializer(StringDeserializer.class)
            .updateConsumerProperties(configMap)
            .withConsumerFactoryFn(new ConsumerFactoryFn()) ... etc.
Map configMap=newhashmap();
configMap.put(“security.protocol”,(Object)“SSL”);
configMap.put(“ssl.truststore.password”,(对象)“clientpass”);
p、 apply(“ReadFromKafka”,KafkaIO.read()
.使用BootstrapServer(“ip:9093”)
.withTopic(“页面浏览量”)
.withKeyDeserializer(StringDeserializer.class)
.withValueDeserializer(StringDeserializer.class)
.updateConsumerProperties(配置映射)
.withConsumerFactoryFn(新ConsumerFactoryFn())…等。