### Create Kafka Topic Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/apache-kafka.md Use this command to create a new topic in Kafka. Ensure Kafka is installed and running. ```bash kafka-topics --create --zookeeper localhost:2181 --partitions 3 --topic test ``` -------------------------------- ### SQS Integration Example Source: https://github.com/elixir-broadway/broadway/blob/main/README.md A basic example demonstrating how to configure Broadway to consume messages from Amazon SQS and process them. Ensure broadway_sqs is a dependency and credentials are set up. ```elixir defmodule MyBroadway do use Broadway alias Broadway.Message def start_link(_opts) do Broadway.start_link(__MODULE__, name: __MODULE__, producer: [ module: {BroadwaySQS.Producer, queue_url: "https://us-east-2.queue.amazonaws.com/100000000001/my_queue"} ], processors: [ default: [concurrency: 50] ], batchers: [ s3: [concurrency: 5, batch_size: 10, batch_timeout: 1000] ] ) end def handle_message(_processor_name, message, _context) do message |> Message.update_data(&process_data/1) |> Message.put_batcher(:s3) end def handle_batch(:s3, messages, _batch_info, _context) do # Send batch of messages to S3 end defp process_data(data) do # Do some calculations, generate a JSON representation, process images. end end ``` -------------------------------- ### Tune Broadway Pipeline Configuration Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/google-cloud-pubsub.md Example of how to set concurrency, max_demand, batch_size, and batch_timeout for producers, processors, and batchers in a Broadway pipeline. ```elixir defmodule MyBroadway do use Broadway def start_link(_opts) do Broadway.start_link(__MODULE__, name: __MODULE__, producer: [ ... concurrency: 10, ], processors: [ default: [ concurrency: 100, max_demand: 1, ] ], batchers: [ default: [ batch_size: 10, concurrency: 10, ] ]) end ...callbacks... end ``` -------------------------------- ### Tuning Broadway Pipeline Configuration Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/amazon-sqs.md Example of setting concurrency, max_demand, and batch_size for producers, processors, and batchers in a Broadway pipeline. ```elixir defmodule MyBroadway do use Broadway def start_link(_opts) do Broadway.start_link(__MODULE__, name: __MODULE__, producer: [ ... concurrency: 10, ], processors: [ default: [ concurrency: 100, max_demand: 1, ] ], batchers: [ default: [ batch_size: 10, concurrency: 10, ] ] ) end ...callbacks... end ``` -------------------------------- ### Implement Broadway Callbacks Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/amazon-sqs.md Implement `handle_message/3` to process individual messages and `handle_batch/4` to process batches of messages. This example updates message data and inspects batches. ```elixir defmodule MyBroadway do use Broadway alias Broadway.Message ...start_link... @impl true def handle_message(_, %Message{data: data} = message, _) do message |> Message.update_data(fn data -> data * data end) end @impl true def handle_batch(_, messages, _, _) do list = messages |> Enum.map(fn e -> e.data end) IO.inspect(list, label: "Got batch of finished jobs from processors, sending ACKs to SQS as a batch.") messages end end ``` -------------------------------- ### Implement Broadway Callbacks Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/google-cloud-pubsub.md Implement `handle_message/3` to process individual messages and `handle_batch/4` to process batches of messages. This example converts message data to uppercase. ```elixir defmodule MyBroadway do use Broadway alias Broadway.Message ...start_link... def handle_message(_, %Message{data: data} = message, _) do message |> Message.update_data(fn data -> String.upcase(data) end) end def handle_batch(_, messages, _, _) do list = messages |> Enum.map(fn e -> e.data end) IO.inspect(list, label: "Got batch of finished jobs from processors, sending ACKs to Pub/Sub as a batch.") messages end end ``` -------------------------------- ### Define Broadway Pipeline Configuration Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/rabbitmq.md Configure the Broadway pipeline with producers, processors, and batchers. This example sets up a producer for 'my_queue' with specific QoS settings and defines concurrency and batching parameters. ```elixir defmodule MyBroadway do use Broadway alias Broadway.Message def start_link(_opts) do Broadway.start_link(__MODULE__, name: MyBroadway, producer: [ module: {BroadwayRabbitMQ.Producer, queue: "my_queue", qos: [ prefetch_count: 50, ] }, concurrency: 1 ], processors: [ default: [ concurrency: 50 ] ], batchers: [ default: [ batch_size: 10, batch_timeout: 1500, concurrency: 5 ] ] ) end ...callbacks... end ``` -------------------------------- ### Send Messages to Kafka using Brod Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/apache-kafka.md Start a Brod client and producer, then send messages synchronously to a specified Kafka topic and partition. ```elixir topic = "test" client_id = :my_client hosts = [localhost: 9092] :ok = :brod.start_client(hosts, client_id, _client_config=[]) :ok = :brod.start_producer(client_id, topic, _producer_config = []) Enum.each(1..1000, fn i -> partition = rem(i, 3) :ok = :brod.produce_sync(client_id, topic, partition, _key="", "#{i}") end) ``` -------------------------------- ### Define Broadway Pipeline Configuration Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/google-cloud-pubsub.md Configure the Broadway pipeline by defining producers, processors, and batchers. This example sets up a producer for a specific Pub/Sub subscription and configures batching. ```elixir defmodule MyBroadway do use Broadway alias Broadway.Message def start_link(_opts) do Broadway.start_link(__MODULE__, name: __MODULE__, producer: [ module: {BroadwayCloudPubSub.Producer, subscription: "projects/test-pubsub/subscriptions/test-subscription"} ], processors: [ default: [] ], batchers: [ default: [ batch_size: 10, batch_timeout: 2_000 ] ]) end ...callbacks... end ``` -------------------------------- ### GenStage Producer Example Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/custom-producers.md A basic GenStage producer that generates a sequence of numbers. This producer generates plain events that require transformation for Broadway. ```elixir defmodule Counter do use GenStage def start_link(number) do GenStage.start_link(Counter, number) end def init(counter) do {:producer, counter} end def handle_demand(demand, counter) when demand > 0 do events = Enum.to_list(counter..counter+demand-1) {:noreply, events, counter + demand} end end ``` -------------------------------- ### Enable Pub/Sub API for the project Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/google-cloud-pubsub.md Enable the Google Cloud Pub/Sub API for the specified project. ```bash gcloud services enable pubsub --project test-pubsub ``` -------------------------------- ### Create a new Google Cloud Pub/Sub project Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/google-cloud-pubsub.md Use the gcloud CLI to create a new Google Cloud project for Pub/Sub integration. ```bash gcloud projects create test-pubsub ``` -------------------------------- ### Create a service account for Pub/Sub Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/google-cloud-pubsub.md Create a service account within the Google Cloud project to allow programmatic access to Pub/Sub. ```bash gcloud iam service-accounts create test-account --project test-pubsub ``` -------------------------------- ### Create a new Pub/Sub subscription Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/google-cloud-pubsub.md Create a new Pub/Sub subscription linked to a topic in the specified Google Cloud project. ```bash gcloud pubsub subscriptions create test-subscription --project test-pubsub --topic test-topic ``` -------------------------------- ### Create a new Pub/Sub topic Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/google-cloud-pubsub.md Create a new Pub/Sub topic within the specified Google Cloud project. ```bash gcloud pubsub topics create test-topic --project test-pubsub ``` -------------------------------- ### Generate service account credentials Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/google-cloud-pubsub.md Create a JSON key file for the service account, which will be used for authentication. ```bash gcloud iam service-accounts keys create credentials.json --iam-account=test-account@test-pubsub.iam.gserviceaccount.com ``` -------------------------------- ### Create New Elixir Project with Supervisor Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/apache-kafka.md Generates a new Elixir project with a supervision tree, essential for robust applications. ```bash mix new my_app --sup ``` -------------------------------- ### Add Broadway Cloud Pub/Sub Dependency Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/google-cloud-pubsub.md Add the :broadway_cloud_pub_sub and :goth dependencies to your mix.exs file. Ensure you check for the latest dependency versions. ```elixir defp deps() do [ ... {:broadway_cloud_pub_sub, "~> 0.7"}, {:goth, "~> 1.0"} ] end ``` -------------------------------- ### List RabbitMQ Queues Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/rabbitmq.md Use rabbitmqctl to list all declared queues in the RabbitMQ instance. This command helps verify if the queue was successfully created. ```bash $ rabbitmqctl list_queues Timeout: 60.0 seconds ... Listing queues for vhost / ... name messages my_queue 0 ``` -------------------------------- ### Add BroadwaySQS and HTTP Client Dependencies Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/amazon-sqs.md Add the BroadwaySQS connector and your chosen HTTP client (e.g., hackney) to your project's dependencies in `mix.exs`. Ensure you use the latest compatible versions. ```elixir def deps do [ ..., {:broadway_sqs, "~> 0.7"}, {:hackney, "~> 1.9"}, ] end ``` -------------------------------- ### Configure RabbitMQ Connection with Credentials Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/rabbitmq.md When consuming from a broker that requires authorization, provide credentials using the 'connection' option within the producer configuration. ```elixir producer: [ module: {BroadwayRabbitMQ.Producer, queue: "my_queue", connection: [ username: "user", password: "password", ] ... } ] ``` -------------------------------- ### Publish Test Messages to Pub/Sub Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/google-cloud-pubsub.md Publish test messages to a Google Cloud Pub/Sub topic using the `gcloud` command-line tool. This is used to test the Broadway pipeline. ```bash $ gcloud pubsub topics publish projects/test-pubsub/topics/test-topic --message "test 1" ``` ```bash gcloud pubsub topics publish projects/test-pubsub/topics/test-topic --message "test 2" ``` -------------------------------- ### Declare RabbitMQ Queue Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/rabbitmq.md Use rabbitmqadmin to declare a new durable queue named 'my_queue'. Ensure the Management plugin is enabled for rabbitmqadmin. ```bash $ rabbitmqadmin declare queue name=my_queue durable=true ``` -------------------------------- ### Publish Messages to RabbitMQ Queue Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/rabbitmq.md Generates and publishes 5000 sample messages to a RabbitMQ queue named 'my_queue' for consumption by the Broadway pipeline. Requires the `amqp` library. ```elixir {:ok, connection} = AMQP.Connection.open {:ok, channel} = AMQP.Channel.open(connection) AMQP.Queue.declare(channel, "my_queue", durable: true) Enum.each(1..5000, fn i -> AMQP.Basic.publish(channel, "", "my_queue", "#{i}") end) AMQP.Connection.close(connection) ``` -------------------------------- ### Add Broadway RabbitMQ Dependency Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/rabbitmq.md Add the :broadway_rabbitmq dependency to your project's mix.exs file. ```elixir def deps do [ ..., {:broadway_rabbitmq, "~> 0.7"}, ] end ``` -------------------------------- ### Implement Broadway Callbacks Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/rabbitmq.md Defines `handle_message` for individual message processing and `handle_batch` for batch processing. Use `handle_message` for simple transformations and `handle_batch` when batch-level operations are needed. Batching is optional since Broadway v0.2. ```elixir defmodule MyBroadway do use Broadway alias Broadway.Message ...start_link... @impl true def handle_message(_, message, _) do message |> Message.update_data(fn data -> {data, String.to_integer(data) * 2} end) end @impl true def handle_batch(_, messages, _, _) do list = messages |> Enum.map(fn e -> e.data end) IO.inspect(list, label: "Got batch") messages end end ``` -------------------------------- ### Basic Broadway SQS Producer Configuration Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/amazon-sqs.md Defines a minimal Broadway pipeline with an Amazon SQS producer. Assumes AWS credentials are set in the environment. ```elixir defmodule MyBroadway do use Broadway alias Broadway.Message def start_link(_opts) do Broadway.start_link(__MODULE__, name: __MODULE__, producer: [ module: {BroadwaySQS.Producer, queue_url: "https://us-east-2.queue.amazonaws.com/100000000001/my_queue"} ], processors: [ default: [] ], batchers: [ default: [ batch_size: 10, batch_timeout: 2000 ] ]) end ...callbacks... end ``` -------------------------------- ### Add Broadway to Supervision Tree Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/rabbitmq.md Integrates the `MyBroadway` pipeline as a child process in the application's supervision tree. Ensure Broadway is listed after its dependencies. ```elixir children = [ {MyBroadway, []} ] Supervisor.start_link(children, strategy: :one_for_one) ``` -------------------------------- ### Add IAM policy binding for service account Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/google-cloud-pubsub.md Grant the 'roles/editor' role to the service account for the Google Cloud project. Review available roles for specific needs. ```bash gcloud projects add-iam-policy-binding test-pubsub \ --member serviceAccount:test-account@test-pubsub.iam.gserviceaccount.com \ --role roles/editor ``` -------------------------------- ### Add Broadway to Mix Dependencies Source: https://github.com/elixir-broadway/broadway/blob/main/README.md Add the Broadway package to your project's dependencies in the mix.exs file. ```elixir def deps do [ {:broadway, "~> 1.0"} ] end ``` -------------------------------- ### Define Broadway Kafka Producer Configuration Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/apache-kafka.md This snippet shows how to configure a Broadway pipeline to use BroadwayKafka as a producer. It specifies Kafka connection details, topics, group ID, and concurrency settings for the producer, processors, and batchers. ```elixir defmodule MyBroadway do use Broadway alias Broadway.Message def start_link(_opts) do Broadway.start_link(__MODULE__, name: __MODULE__, producer: [ module: {BroadwayKafka.Producer, [ hosts: [localhost: 9092], group_id: "group_1", topics: ["test"] ]}, concurrency: 1 ], processors: [ default: [ concurrency: 10 ] ], batchers: [ default: [ batch_size: 100, batch_timeout: 200, concurrency: 10 ] ] ) end ...callbacks... end ``` -------------------------------- ### Broadway Pipeline with Custom Producer Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/custom-producers.md Configures a Broadway pipeline to use the custom `Counter` GenStage producer. A transformer function is specified to convert the producer's events into `Broadway.Message` structs. ```elixir defmodule MyBroadway do use Broadway @behaviour Broadway.Acknowledger alias Broadway.Message def start_link(_opts) do Broadway.start_link(__MODULE__, name: __MODULE__, producer: [ module: {Counter, 1}, transformer: {__MODULE__, :transform, []} ], processors: [ default: [concurrency: 10] ], batchers: [ default: [concurrency: 2, batch_size: 5] ]) end ...callbacks... def transform(event, _opts) do %Message{ data: event, acknowledger: {__MODULE__, :ack_id, :ack_data} } end @impl Broadway.Acknowledger def ack(:ack_id, successful, failed) do # Write ack code here :ok end end ``` -------------------------------- ### Add BroadwayKafka Dependency Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/apache-kafka.md Integrates the BroadwayKafka connector into your Elixir project by adding it to the dependencies in mix.exs. Always check for the latest version. ```elixir def deps do [ ... {:broadway_kafka, "~> 0.3"} ] end ``` -------------------------------- ### Broadway SQS Producer with Explicit AWS Credentials Source: https://github.com/elixir-broadway/broadway/blob/main/guides/examples/amazon-sqs.md Configures the Broadway SQS producer to explicitly pass AWS access key ID and secret access key. Useful when environment variables are not set. ```elixir producer: [ module: {BroadwaySQS.Producer, queue_url: "https://us-east-2.queue.amazonaws.com/100000000001/my_queue", config: [ access_key_id: "YOUR_AWS_ACCESS_KEY_ID", secret_access_key: "YOUR_AWS_SECRET_ACCESS_KEY" ]} ] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.