构建现代数据基础设施一直是当今企业的难题。当今的企业需要管理全天候生成和交付的大量异构数据。然而,由于企业对数据的数量和速度等等有多种要求,没有“一刀切”的解决方案。相反,企业需在不同系统之间移动数据,以便存储、处理和提供数据。
粗看搭建基础设施的历史,企业使用了许多不同的工具来尝试移动数据,例如用于流式工作负载的 Apache Kafka 和用于消息工作负载的 RabbitMQ。现在,Apache Pulsar 的诞生为企业简化了这个过程。
Apache Pulsar 是一个云原生的分布式消息流平台。Pulsar 旨在满足现代数据需求,支持灵活的消息传递语义、分层存储、多租户和异地复制(跨区域数据复制)。自 2018 年毕业成为 Apache 软件基金会顶级项目以来,Pulsar 项目经历了快速的社区增长、周边生态的发展和全球用户的增长。将 Pulsar 用作数据基础设施的支柱,公司能够以快速且可扩展的方式移动数据。在这篇博文中,我们将介绍如何使用 Pulsar IO 在 Pulsar 和外部系统之间轻松导入和导出数据。
Pulsar IO 是一个完整的工具包,用于创建、部署和管理与外部系统(如键/值存储、分布式文件系统、搜索索引、数据库、数据仓库、其他消息传递系统等)集成的 Pulsar 连接器。由于 Pulsar IO 构建在 Pulsar 的无服务器计算层(称为Pulsar Function)之上,因此编写 Pulsar IO 连接器就像编写 Pulsar Function 一样简单。
借助 Pulsar IO,用户可以使用现有的 Pulsar 连接器或编写自己的自定义连接器,轻松地将数据移入和移出 Pulsar。Pulsar IO 拥有以下优势:
由于 Pulsar IO 建立在 Pulsar Function 之上,因此 Pulsar IO 和 Pulsar Function 具有相同的运行时选项。部署 Pulsar IO 连接器时,用户有以下选择:
如前所述,Pulsar IO 减少了生成和消费应用程序所需的样板代码。它通过提供不同的基本接口来实现这一点,这些接口抽象出样板代码并允许我们专注于业务逻辑。
Pulsar IO 支持 Source 和 Sink 的基本接口。Source 连接器(Source connector)允许用户将数据从外部系统带入 Pulsar,而 Sink 连接器(Sink Connector)可用于将数据移出 Pulsar 并移入外部系统,例如数据库。
还有一种特殊类型的 Source 连接器,称为 Push Source。Push Source 连接器可以轻松实现某些需要推送数据的集成。举例来说,Push Source 可以是变更数据捕获源系统,它在接收到新变更后,会自动将该变更推送到 Pulsar。
public interface Source<T> extends AutoCloseable {
/**
* Open connector with configuration.
*
* @param config initialization config
* @param sourceContext environment where the source connector is running
* @throws Exception IO type exceptions when opening a connector
*/
void open(final Map<String, Object> config, SourceContext sourceContext) throws Exception;
/**
* Reads the next message from source.
* If source does not have any new messages, this call should block.
* @return next message from source. The return result should never be null
* @throws Exception
*/
Record<T> read() throws Exception;
}
public interface BatchSource<T> extends AutoCloseable {
/**
* Open connector with configuration.
*
* @param config config that's supplied for source
* @param context environment where the source connector is running
* @throws Exception IO type exceptions when opening a connector
*/
void open(final Map<String, Object> config, SourceContext context) throws Exception;
/**
* Discovery phase of a connector. This phase will only be run on one instance, i.e. instance 0, of the connector.
* Implementations use the taskEater consumer to output serialized representation of tasks as they are discovered.
*
* @param taskEater function to notify the framework about the new task received.
* @throws Exception during discover
*/
void discover(Consumer<byte[]> taskEater) throws Exception;
/**
* Called when a new task appears for this connector instance.
*
* @param task the serialized representation of the task
*/
void prepare(byte[] task) throws Exception;
/**
* Read data and return a record
* Return null if no more records are present for this task
* @return a record
*/
Record<T> readNext() throws Exception;
}
public interface Sink<T> extends AutoCloseable {
/**
* Open connector with configuration.
*
* @param config initialization config
* @param sinkContext environment where the sink connector is running
* @throws Exception IO type exceptions when opening a connector
*/
void open(final Map<String, Object> config, SinkContext sinkContext) throws Exception;
/**
* Write a message to Sink.
*
* @param record record to write to sink
* @throws Exception
*/
void write(Record<T> record) throws Exception;
}
Apache Pulsar 能够作为现代数据基础设施的支柱,它使企业能够以快速且可扩展的方式搬运数据。Pulsar IO 是一个连接器框架,它为开发人员提供了所有必要的工具来创建、部署和管理与不同系统集成的 Pulsar 连接器。Pulsar IO 抽象掉所有样板代码,使开发人员可以专注于应用程序逻辑。
如果您有兴趣了解更多信息并构建自己的连接器,请查看以下资源:
宋博,就职于北京百观科技有限公司,高级开发工程师,专注于微服务、云计算、大数据领域。特别感谢宋博的翻译,才让我们看到本篇文章的中文版本。