polar

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

Go Client for PolarStreams

Go Client for PolarStreams. PolarStreams is a lightweight, elastic, kubernetes-native event streaming system.

Installing

go get github.com/polarstreams/go-client

go build

Getting started

To start using the Go Client for PolarStreams, import the client package and set the PolarStreams "service Url" when creating a Producer or Consumer.

A service Url is composed by the polar:// scheme followed by the host name or Kubernetes service name, for example: polar://polar.streams refers to the service polar in the streams namespace.

Producing messages

In order to publish records to a topic, you need to create a Producer instance. Producer instances are designed to be long lived and thread safe, you usually need only one per application.

import (
	"fmt"
	"strings"

	"github.com/polarstreams/go-client"
)

// ...

producer, err := polar.NewProducer("polar://polar.streams")
if err != nil {
	panic(err)
}

fmt.Printf("Discovered a PolarStreams cluster with %d brokers\n", producer.BrokersLength())

topic := "my-first-topic" // The topic will be automatically created
message := strings.NewReader(`{"hello": "world"}`)
partitionKey := "" // Empty to use a random partition

if err := producer.Send(topic, message, partitionKey); err != nil {
	panic(err)
}
Consuming messages

To read messages from a topic, you need to create a Consumer instance and set the group name that the consumer belongs to.

When multiple consumers from a group are subscribed to a topic, each consumer in the group will receive messages from a different set of the partitions within the topic.

Consumer instances are designed to be long lived. You usually need only one per application.

import (
	"fmt"

	"github.com/polarstreams/go-client"
)


// ...

group := "group1"
consumer, err := polar.NewConsumer("polar://polar.streams", group, topic)
if err != nil {
	panic(err)
}

fmt.Printf("Discovered a cluster with %d brokers\n", consumer.BrokersLength())

for {
	pollResult := consumer.Poll()
	if pollResult.Error != nil {
		fmt.Printf("Found error while polling: %s", pollResult.Error)
		continue
	}

	// New records organized by topic
	for _, topicRecords := range pollResult.TopicRecords {
		for _, record := range topicRecords.Records {
			fmt.Println(string(record.Body), record.Timestamp)
		}
	}
}

License

© Jorge Bay.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Consumer

type Consumer interface {
	// Retrieves  data for the topics subscribed.
	// On each poll, consumer will try to use the last consumed offset as the starting offset and fetch sequentially.
	Poll() ConsumerPollResult

	// Gets a point-in-time value of the number of brokers in the cluster
	BrokersLength() int

	// Explicitly reports that the data polled has been consumed to all brokers in the cluster.
	//
	// When exiting the application cleanly, it's usually recommended to invoke ManualCommit() once before Close() to
	// make sure other consumer of the group can continue where this consumer left off.
	//
	// When polling in loop from a consumer, it's usually not necessary to call ManualCommit() per each pool
	// as the broker will automatically commit the consumer offsets periodically.
	ManualCommit() ConsumerCommitResult

	// Closes the consumer
	//
	// A Consumer instance is designed to be long-lived. Close() should only be called when no more messages
	// should be read in the application.
	Close()
}

Represents a PolarStreams client that reads records from a cluster.

func NewConsumer

func NewConsumer(serviceUrl string, consumerGroup string, topic string) (Consumer, error)

NewConsumer creates a new Consumer, discovers the PolarStreams cluster and subscribes to the topic provided.

func NewConsumerWithOpts

func NewConsumerWithOpts(serviceUrl string, options ConsumerOptions) (Consumer, error)

NewConsumer creates a new Consumer with the provided options.

It discovers the PolarStreams cluster and subscribes to the topics provided.

type Producer

type Producer interface {
	// Sends a message to a topic
	Send(topic string, message io.Reader, partitionKey string) error

	// Gets a point-in-time value of the number of brokers in the cluster
	BrokersLength() int

	// Closes the producer
	//
	// A Producer instance is designed to be long-lived. Close() should only be called when no more messages are
	// expected to be published in the cluster.
	Close()
}

Represents a PolarStreams client that publishes records in a cluster.

func NewProducer

func NewProducer(serviceUrl string) (Producer, error)

NewProducer creates a new Producer and discovers the PolarStreams cluster.

A Producer instance is designed to be long-lived and it should be reused across the application.

func NewProducerWithOpts

func NewProducerWithOpts(serviceUrl string, options types.ProducerOptions) (Producer, error)

NewProducer creates a new Producer with the provided options and discovers the PolarStreams cluster.

A Producer instance is designed to be long-lived and it should be reused across the application.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL