Created
December 2, 2010 22:59
-
-
Save samuel/726278 to your computer and use it in GitHub Desktop.
Simple consumer example in Jython for Kafka
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env jython | |
import os | |
import sys | |
sys.path.extend(["lib/"+x for x in os.listdir("lib") if x.endswith('.jar')]) | |
sys.path.extend(["dist/"+x for x in os.listdir("dist") if x.endswith('.jar')]) | |
import jarray | |
from kafka.api import FetchRequest | |
from kafka.consumer import SimpleConsumer | |
consumer = SimpleConsumer("localhost", 9092, 10000, 1024000) | |
req = FetchRequest("test", 0, 0, 1000000) | |
messageset = consumer.fetch(req) # ByteBufferMessageSet | |
msgs = list(messageset.elements()) # [kafka.message.Message] | |
for msg in msgs: | |
buf = msg.payload() # java.nio.HeapByteBuffer | |
barray = jarray.zeros(buf.remaining(), 'b') | |
buf.get(barray) | |
print barray.tostring() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment