| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package kafkapubsub_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"log" |
| 20 |
|
| 21 |
"gocloud.dev/pubsub" |
| 22 |
"gocloud.dev/pubsub/kafkapubsub" |
| 23 |
) |
| 24 |
|
| 25 |
func ExampleOpenTopic() { |
| 26 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 27 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 28 |
ctx := context.Background() |
| 29 |
|
| 30 |
// The set of brokers in the Kafka cluster. |
| 31 |
addrs := []string{"1.2.3.4:9092"} |
| 32 |
// The Kafka client configuration to use. |
| 33 |
config := kafkapubsub.MinimalConfig() |
| 34 |
|
| 35 |
// Construct a *pubsub.Topic. |
| 36 |
topic, err := kafkapubsub.OpenTopic(addrs, config, "my-topic", nil) |
| 37 |
if err != nil { |
| 38 |
log.Fatal(err) |
| 39 |
} |
| 40 |
defer topic.Shutdown(ctx) |
| 41 |
} |
| 42 |
|
| 43 |
func ExampleOpenSubscription() { |
| 44 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 45 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 46 |
ctx := context.Background() |
| 47 |
|
| 48 |
// The set of brokers in the Kafka cluster. |
| 49 |
addrs := []string{"1.2.3.4:9092"} |
| 50 |
// The Kafka client configuration to use. |
| 51 |
config := kafkapubsub.MinimalConfig() |
| 52 |
|
| 53 |
// Construct a *pubsub.Subscription, joining the consumer group "my-group" |
| 54 |
// and receiving messages from "my-topic". |
| 55 |
subscription, err := kafkapubsub.OpenSubscription( |
| 56 |
addrs, config, "my-group", []string{"my-topic"}, nil) |
| 57 |
if err != nil { |
| 58 |
log.Fatal(err) |
| 59 |
} |
| 60 |
defer subscription.Shutdown(ctx) |
| 61 |
} |
| 62 |
|
| 63 |
func Example_openTopicFromURL() { |
| 64 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 65 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/kafkapubsub" |
| 66 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 67 |
ctx := context.Background() |
| 68 |
|
| 69 |
// pubsub.OpenTopic creates a *pubsub.Topic from a URL. |
| 70 |
// The host + path are the topic name to send to. |
| 71 |
// The set of brokers must be in an environment variable KAFKA_BROKERS. |
| 72 |
topic, err := pubsub.OpenTopic(ctx, "kafka://my-topic?key_name=x-partition-key") |
| 73 |
if err != nil { |
| 74 |
log.Fatal(err) |
| 75 |
} |
| 76 |
defer topic.Shutdown(ctx) |
| 77 |
} |
| 78 |
|
| 79 |
func Example_openSubscriptionFromURL() { |
| 80 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 81 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/kafkapubsub" |
| 82 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 83 |
ctx := context.Background() |
| 84 |
|
| 85 |
// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL. |
| 86 |
// The host + path are used as the consumer group name. |
| 87 |
// The "topic" query parameter sets one or more topics to subscribe to. |
| 88 |
// The set of brokers must be in an environment variable KAFKA_BROKERS. |
| 89 |
subscription, err := pubsub.OpenSubscription(ctx, |
| 90 |
"kafka://my-group?topic=my-topic") |
| 91 |
if err != nil { |
| 92 |
log.Fatal(err) |
| 93 |
} |
| 94 |
defer subscription.Shutdown(ctx) |
| 95 |
} |