Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Snow committed Dec 9, 2024
1 parent 2fb0529 commit b447689
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion _sources/vast_datastore/kafka_connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,14 @@ finally:
```python
from kafka import KafkaProducer
import json
num_of_messages = # Complete - the amount of messages to topic (can be also 1)
data = {"name": "something"}
producer = KafkaProducer(bootstrap_servers='<YOUR_CLIENT_IP>:9092', value_serializer=lambda v: json.dumps(v).encode('utf-8'))
for i in range (1,num_of_messages):
producer.send('<YOUR_TOPIC_NAME>', data)
producer.send('<YOUR_TOPIC_NAME>', data)
producer.flush()
print("Message sent to Kafka topic")
Expand Down
5 changes: 4 additions & 1 deletion vast_datastore/kafka_connect.html
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,14 @@ <h2>Creating new topic<a class="headerlink" href="#creating-new-topic" title="Li
<h2>Produce messages to topic<a class="headerlink" href="#produce-messages-to-topic" title="Link to this heading">#</a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">kafka</span> <span class="kn">import</span> <span class="n">KafkaProducer</span>
<span class="kn">import</span> <span class="nn">json</span>

<span class="n">num_of_messages</span> <span class="o">=</span> <span class="c1"># Complete - the amount of messages to topic (can be also 1) </span>
<span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;something&quot;</span><span class="p">}</span>
<span class="n">producer</span> <span class="o">=</span> <span class="n">KafkaProducer</span><span class="p">(</span><span class="n">bootstrap_servers</span><span class="o">=</span><span class="s1">&#39;&lt;YOUR_CLIENT_IP&gt;:9092&#39;</span><span class="p">,</span> <span class="n">value_serializer</span><span class="o">=</span><span class="k">lambda</span> <span class="n">v</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>

<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">num_of_messages</span><span class="p">):</span>
<span class="n">producer</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="s1">&#39;&lt;YOUR_TOPIC_NAME&gt;&#39;</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
<span class="n">producer</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="s1">&#39;&lt;YOUR_TOPIC_NAME&gt;&#39;</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>

<span class="n">producer</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>

<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Message sent to Kafka topic&quot;</span><span class="p">)</span>
Expand Down

0 comments on commit b447689

Please sign in to comment.