对于 getCreatedAt 和 getCountry,Twitter4j 变得空白或为空
Twitter4j getting blank or null for getCreatedAt and getCountry
我正在使用 Twitter4J 流式传输一些 Twitter 数据,并且能够生成推文文本。
推文由 TwitterConsumer class 使用,将文本保存到 Kafka。
到目前为止,一切都按预期进行。
我的问题是我试图获取推文的日期和时间以及位置(国家代码就足够了)。我知道该位置可能为空,但就我而言,date/time 和位置始终为空或为空。我一定是做错了什么。
示例输出;
[Twitter4J Async Dispatcher[0]] INFO
uom_bigdataAss1.producer.TwitterProducer - Created on: [Thread-3] INFO
uom_bigdataAss1.consumer.TwitterConsumer - Location: No Location
[Thread-3] INFO uom_bigdataAss1.consumer.TwitterConsumer - delta plus
- a variant of concern. more transmissible, could even escape vaccines. we will continue to learn more about… [Thread-3] INFO
uom_bigdataAss1.consumer.TwitterConsumer - Created at: [Thread-3] INFO
uom_bigdataAss1.consumer.TwitterConsumer - User: [Thread-3] INFO
uom_bigdataAss1.consumer.TwitterConsumer - User Name: [Thread-3] INFO
uom_bigdataAss1.consumer.TwitterConsumer -
_________________
这是我用来制作 Twitter 流媒体端的代码;
public class TwitterProducer {
final Logger logger = LoggerFactory.getLogger(TwitterProducer.class);
Thread twitterConsumerThread = null;
//Queue for tweets
private LinkedBlockingQueue<Status> queue;
//stream of tweets
private TwitterStream twitterStream;
private BlockingQueue<String> msgQueue = new LinkedBlockingQueue<>(100);
public static void main(String[] args) {
new TwitterProducer().run();
}
// Twitter Client
public void setTwitterStream(BlockingQueue<String> msgQueue){
/** Setting up a connection */
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(TwitterConfig.CONSUMER_KEYS)
.setOAuthConsumerSecret(TwitterConfig.CONSUMER_SECRETS)
.setOAuthAccessToken(TwitterConfig.TOKEN)
.setOAuthAccessTokenSecret(TwitterConfig.SECRET);
this.twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
}
private void run(){
logger.info("Setting up");
// 1. Twitter Stream
setTwitterStream(msgQueue);
this.queue = new LinkedBlockingQueue<Status>();
StatusListener listener = new StatusListener() {
@Override
public void onStatus(Status status) {
try {
queue.offer(status, 5000, TimeUnit.MILLISECONDS);
logger.info("Created on:", status.getCreatedAt().getTime());
Place place = status.getPlace();
if (place != null ) {
logger.info("Created on:", place.getCountry());
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
logger.error("BlockinQueue Full", e);
}
}
@Override
public void onDeletionNotice(StatusDeletionNotice sdn) {
}
@Override
public void onTrackLimitationNotice(int i) {
}
@Override
public void onScrubGeo(long l, long l1) {
}
@Override
public void onException(Exception e) {
e.printStackTrace();
}
@Override
public void onStallWarning(StallWarning warning) {
}
};
twitterStream.addListener(listener);
FilterQuery query = new FilterQuery();
String keywords[] = {"covid", "vaccine"};
query.track(keywords);
query.language("en");
twitterStream.filter(query);
// Shutdown Hook
Runtime.getRuntime().addShutdownHook(new Thread( () -> {
logger.info("Application is now stopping!");
twitterStream.shutdown();
//logger.info("Closing Producer");
//producer.close();
logger.info("Closing twitterConsumer");
this.twitterConsumerThread.interrupt();
logger.info("Finished closing");
logger.info("\n Application End");
}));
logger.info("Running Twitter Consumer");
TwitterConsumer twitterConsumer = new TwitterConsumer(this.queue);
this.twitterConsumerThread = new Thread(twitterConsumer);
twitterConsumerThread.start();
}
}
有什么想法吗happening/cause?
像这样更改您的日志语句
logger.info("Created on: {}", status.getCreatedAt().getTime());
和
logger.info("Created on: {}", place.getCountry());
您缺少 {}-占位符
经过上述更改,日志将类似于
Created on: 1624430231000
Created on: India
检查http://www.slf4j.org/manual.html#typical_usage
此外,并非所有状态都带有国家/地区信息。
我正在使用 Twitter4J 流式传输一些 Twitter 数据,并且能够生成推文文本。 推文由 TwitterConsumer class 使用,将文本保存到 Kafka。 到目前为止,一切都按预期进行。 我的问题是我试图获取推文的日期和时间以及位置(国家代码就足够了)。我知道该位置可能为空,但就我而言,date/time 和位置始终为空或为空。我一定是做错了什么。
示例输出;
[Twitter4J Async Dispatcher[0]] INFO uom_bigdataAss1.producer.TwitterProducer - Created on: [Thread-3] INFO uom_bigdataAss1.consumer.TwitterConsumer - Location: No Location [Thread-3] INFO uom_bigdataAss1.consumer.TwitterConsumer - delta plus
- a variant of concern. more transmissible, could even escape vaccines. we will continue to learn more about… [Thread-3] INFO uom_bigdataAss1.consumer.TwitterConsumer - Created at: [Thread-3] INFO uom_bigdataAss1.consumer.TwitterConsumer - User: [Thread-3] INFO uom_bigdataAss1.consumer.TwitterConsumer - User Name: [Thread-3] INFO uom_bigdataAss1.consumer.TwitterConsumer - _________________
这是我用来制作 Twitter 流媒体端的代码;
public class TwitterProducer {
final Logger logger = LoggerFactory.getLogger(TwitterProducer.class);
Thread twitterConsumerThread = null;
//Queue for tweets
private LinkedBlockingQueue<Status> queue;
//stream of tweets
private TwitterStream twitterStream;
private BlockingQueue<String> msgQueue = new LinkedBlockingQueue<>(100);
public static void main(String[] args) {
new TwitterProducer().run();
}
// Twitter Client
public void setTwitterStream(BlockingQueue<String> msgQueue){
/** Setting up a connection */
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(TwitterConfig.CONSUMER_KEYS)
.setOAuthConsumerSecret(TwitterConfig.CONSUMER_SECRETS)
.setOAuthAccessToken(TwitterConfig.TOKEN)
.setOAuthAccessTokenSecret(TwitterConfig.SECRET);
this.twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
}
private void run(){
logger.info("Setting up");
// 1. Twitter Stream
setTwitterStream(msgQueue);
this.queue = new LinkedBlockingQueue<Status>();
StatusListener listener = new StatusListener() {
@Override
public void onStatus(Status status) {
try {
queue.offer(status, 5000, TimeUnit.MILLISECONDS);
logger.info("Created on:", status.getCreatedAt().getTime());
Place place = status.getPlace();
if (place != null ) {
logger.info("Created on:", place.getCountry());
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
logger.error("BlockinQueue Full", e);
}
}
@Override
public void onDeletionNotice(StatusDeletionNotice sdn) {
}
@Override
public void onTrackLimitationNotice(int i) {
}
@Override
public void onScrubGeo(long l, long l1) {
}
@Override
public void onException(Exception e) {
e.printStackTrace();
}
@Override
public void onStallWarning(StallWarning warning) {
}
};
twitterStream.addListener(listener);
FilterQuery query = new FilterQuery();
String keywords[] = {"covid", "vaccine"};
query.track(keywords);
query.language("en");
twitterStream.filter(query);
// Shutdown Hook
Runtime.getRuntime().addShutdownHook(new Thread( () -> {
logger.info("Application is now stopping!");
twitterStream.shutdown();
//logger.info("Closing Producer");
//producer.close();
logger.info("Closing twitterConsumer");
this.twitterConsumerThread.interrupt();
logger.info("Finished closing");
logger.info("\n Application End");
}));
logger.info("Running Twitter Consumer");
TwitterConsumer twitterConsumer = new TwitterConsumer(this.queue);
this.twitterConsumerThread = new Thread(twitterConsumer);
twitterConsumerThread.start();
}
}
有什么想法吗happening/cause?
像这样更改您的日志语句
logger.info("Created on: {}", status.getCreatedAt().getTime());
和
logger.info("Created on: {}", place.getCountry());
您缺少 {}-占位符
经过上述更改,日志将类似于
Created on: 1624430231000
Created on: India
检查http://www.slf4j.org/manual.html#typical_usage
此外,并非所有状态都带有国家/地区信息。