使用hbc Twitter4jStatusClient保存原始json字符串
我试图将示例流中的tweet存储到数据库中,同时存储原始json。我正在hbc Github存储库中使用Twitter4jStatusClient。由于我只是实时将一部分信息存储到数据库中,所以我希望也存储tweet的原始json,以便在需要时检索其他信息。但是,使用Twitter4jStatusClient意味着监听器是在不同的线程上执行的,在中,它表示为了获取json对象,它必须从检索json对象的同一线程执行。使用Twitter4JStatusClient时是否有保存json字符串的方法?我选择不使用它,因为我只想执行某些操作并保存json字符串(如果它是状态)。谢谢使用hbc Twitter4jStatusClient保存原始json字符串,json,twitter4j,twitter-hbc,Json,Twitter4j,Twitter Hbc,我试图将示例流中的tweet存储到数据库中,同时存储原始json。我正在hbc Github存储库中使用Twitter4jStatusClient。由于我只是实时将一部分信息存储到数据库中,所以我希望也存储tweet的原始json,以便在需要时检索其他信息。但是,使用Twitter4jStatusClient意味着监听器是在不同的线程上执行的,在中,它表示为了获取json对象,它必须从检索json对象的同一线程执行。使用Twitter4JStatusClient时是否有保存json字符串的方法?
// Create an appropriately sized blocking queue
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
// Define our endpoint: By default, delimited=length is set (we need this for our processor)
// and stall warnings are on.
StatusesSampleEndpoint endpoint = new StatusesSampleEndpoint();
// Specify the language filter for the endpoint
endpoint.addQueryParameter(Constants.LANGUAGE_PARAM, Joiner.on(',').join(Lists.newArrayList("en")));
endpoint.stallWarnings(false);
Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
// Create a new BasicClient. By default gzip is enabled.
BasicClient client = new ClientBuilder()
.name("sampleStreamClient")
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(auth)
.processor(new StringDelimitedProcessor(queue))
.build();
// Create an executor service which will spawn threads to do the actual work of parsing the incoming messages and
// calling the listeners on each message
int numProcessingThreads = 4;
ExecutorService service = Executors.newFixedThreadPool(numProcessingThreads);
StatusListener listener = new SampleStreamStatusListener(jsonInserter);
// Wrap our BasicClient with the twitter4j client
t4jClient = new Twitter4jStatusClient(
client, queue, Lists.newArrayList(listener), service);
//创建大小适当的阻塞队列
BlockingQueue=新的LinkedBlockingQueue(10000);
//定义我们的端点:默认情况下,设置了delimited=length(我们的处理器需要这个)
//和失速警告已打开。
StatuseSampleEndpoint=新的StatuseSampleEndpoint();
//指定终结点的语言筛选器
addQueryParameter(Constants.LANGUAGE_PARAM,Joiner.on(',).join(Lists.newArrayList(“en”));
endpoint.stallWarnings(false);
Authentication auth=new OAuth1(consumerKey、ConsumerCret、token、secret);
//创建一个新的基本客户端。默认情况下,gzip处于启用状态。
BasicClient客户端=新客户端生成器()
.名称(“sampleStreamClient”)
.hosts(常数.STREAM\u主机)
.endpoint(端点)
.身份验证(auth)
.processor(新的StringDelimitedProcessor(队列))
.build();
//创建一个executor服务,该服务将生成线程来执行解析传入消息和消息的实际工作
//调用每条消息的侦听器
int numProcessingThreads=4;
ExecutorService=Executors.newFixedThreadPool(numProcessingThreads);
StatusListener listener=新的SampleStreamStatusListener(jsonInserter);
//用twitter4j客户端包装我们的基本客户端
t4jClient=新Twitter4jStatusClient(
客户端、队列、列表。newArrayList(侦听器)、服务);
我在Twitter4jStatusClient上遇到了类似的问题,这里有一些想法
中间队列
您可以有一个单独的线程池,从队列
变量读取原始消息,将它们存储在某个位置,然后将它们放入一个新队列中,我们将调用hbcQueue
,您将其传递到Twitter4jStatusClient
构造函数中,而不是队列
BlockingQueue<String> hbcQueue = new LinkedBlockingQueue<>(10000);
ExecutorService rawJsonSaver = Executors.newFixedThreadPool(numProcessingThreads);
for (int i = 0; i < numProcessingThreads; i++) {
rawJsonSaver.execute(() -> {
for (;;) {
try {
String msg = queue.take();
JSONObject jobj = new JSONObject(msg);
if (JSONObjectType.determine(jobj) == JSONObjectType.Type.STATUS) {
System.out.println(msg); // Save it
hbcQueue.add(msg);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); break;
} catch (JSONException e) {
continue;
}
}
});
}
Twitter4jStatusClient t4jClient = new Twitter4jStatusClient(
client, hbcQueue, Lists.newArrayList(listener), service);
我在Twitter4jStatusClient上也遇到了类似的问题,这里有一些想法 中间队列 您可以有一个单独的线程池,从
队列
变量读取原始消息,将它们存储在某个位置,然后将它们放入一个新队列中,我们将调用hbcQueue
,您将其传递到Twitter4jStatusClient
构造函数中,而不是队列
BlockingQueue<String> hbcQueue = new LinkedBlockingQueue<>(10000);
ExecutorService rawJsonSaver = Executors.newFixedThreadPool(numProcessingThreads);
for (int i = 0; i < numProcessingThreads; i++) {
rawJsonSaver.execute(() -> {
for (;;) {
try {
String msg = queue.take();
JSONObject jobj = new JSONObject(msg);
if (JSONObjectType.determine(jobj) == JSONObjectType.Type.STATUS) {
System.out.println(msg); // Save it
hbcQueue.add(msg);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); break;
} catch (JSONException e) {
continue;
}
}
});
}
Twitter4jStatusClient t4jClient = new Twitter4jStatusClient(
client, hbcQueue, Lists.newArrayList(listener), service);