我在SO和其他有类似问题的地方看过很多链接,但没有任何帮助。
这是我的代码:
import java.util.Queue;
import java.util.LinkedList;
import javax.xml.bind.DatatypeConverter;
import org.json.simple.JSONObject;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
public class ConsumerThread implements Runnable {
private KafkaStream<byte[], byte[]> stream;
public static Queue<JSONObject> message = (Queue<JSONObject>) new LinkedList<JSONObject>();
public ConsumerThread(KafkaStream<byte[], byte[]> stream){
this.stream = stream;
}
@Override
public void run() {
// TODO Auto-generated method stub
ConsumerIterator<byte[], byte[]> it = stream.iterator();
while (it.hasNext()){
byte[] msg = it.next().message();
System.out.println(new String(msg));
message.add(DatatypeConverter.printBase64Binary(msg));
}
}
}
这在最后一行给出错误:
message.add
我只想将此byte[]
转换为可以推送到队列的JSONObject
,我不确定是什么问题。
答案 0 :(得分:0)
怎么样
import java.util.Queue;
import java.util.LinkedList;
import javax.xml.bind.DatatypeConverter;
import org.json.simple.JSONObject;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
public class ConsumerThread implements Runnable {
public static final String JSON_MESSAGE_FIELD_NAME = "message_b64";
private KafkaStream<byte[], byte[]> stream;
public static Queue<JSONObject> message = (Queue<JSONObject>) new LinkedList<JSONObject>();
public ConsumerThread(KafkaStream<byte[], byte[]> stream){
this.stream = stream;
}
@Override
public void run() {
ConsumerIterator<byte[], byte[]> it = stream.iterator();
while (it.hasNext()){
byte[] msg = it.next().message();
System.out.println(new String(msg));
final JSONObject json = new JSONObject();
json.put(JSON_MESSAGE_FIELD_NAME, DatatypeConverter.printBase64Binary(msg));
message.add(json);
}
}
}