0

I am able to push data from Kafka to Memsql.

I am trying to push using Transform. I have created Kafka Consumer in Python which is consuming data from Kafka Topic and converting to Json Format.

I don't know how to use this as Transform in Memsql.

from confluent_kafka import KafkaError
from confluent_kafka.avro import AvroConsumer
from confluent_kafka.avro.serializer import SerializerError
import sys

c = AvroConsumer({
    'bootstrap.servers': 'X.Y.Z.W:9092',
    'group.id': 'groupid1112',
    'schema.registry.url': 'http://X.Y.Z.W:8081',
    'default.topic.config': {
        'auto.offset.reset': 'smallest'
    }
    })

c.subscribe(['test_topic'])
count =0
while True:
    try:
        msg = c.poll(10)

    except SerializerError as e:
        print("Message deserialization failed for {}: {}".format(msg, e))
        break

    if msg is None:
        continue

    if msg.error():
        if msg.error().code() == KafkaError._PARTITION_EOF:
            continue
        else:
            print(msg.error())
            break
    valueList = list(msg.value().values())
    print(valueList)

c.close()

It's pritning

[1518776144187, 1, 2, 103,'asas',asas'eer',None]
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
cody123
  • 2,040
  • 24
  • 29
  • try `print(msg.value())`, otherwise MemSQL has a JDBC Driver, you can use with JDBC Kafka Connect to read Avro data and write to MemSQL – OneCricketeer Sep 01 '18 at 20:31

1 Answers1

0

check these docs https://docs.memsql.com/memsql-pipelines/v6.0/transforms/

stay tuned for native avro support in an upcoming MemSQL release.

youll want to do something like the following, but with me sketching over the avro-specific details since I don't know the avro library off the top of my head.

```

def input_stream():
    """
        Consume STDIN and yield each record that is received from MemSQL
    """
    while True:
        byte_len = sys.stdin.read(8)
        if len(byte_len) == 8:
            byte_len = struct.unpack("L", byte_len)[0]
            result = sys.stdin.read(byte_len)
            yield result
        else:
            assert len(byte_len) == 0, byte_len
            return

avro_context = WhateverYouNeed() # maybe connect to schema registry here if you need to

for msg in input_stream():
    object = DeserializeAvro(avro_context, msg) # this is your code
    sys.stdout.write(SerializeToTSV(object)) # also your code

```

Using the schema registry should be fine, but you shouldn't need to worry about the details of reading from kafka in your transform script. I can try to get you a more detailed script Monday, but this is how to structure the code.

Joseph Victor
  • 819
  • 6
  • 16
  • I have gone through docs man but there is not way to configure registry in pipeline so only I asked how to use transform in avro case. – cody123 Aug 05 '18 at 05:39