我試圖將來自Sample Stream的推文存儲到數據庫中,並同時存儲原始json。我在hbc Github存儲庫中的example之後使用Twitter4jStatusClient
。由於我只是實時將一部分信息存儲到數據庫中,因此我希望能夠存儲推文的原始json,以便在需要時可以檢索其他信息。然而,使用Twitter4jStatusClient
意味着偵聽器在不同的線程上執行,並且在here中,它表示爲了獲取json對象,它必須從檢索json對象的相同線程執行。使用Twitter4JStatusClient
時,有沒有方法可以保存json字符串?我選擇不使用這個example,因爲我只想執行某些操作並保存json字符串(如果它是狀態的話)。謝謝!用hbc保存原始json字符串Twitter4jStatusClient
// 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);