本文整理汇总了Java中com.twitter.hbc.core.endpoint.StatusesFilterEndpoint.trackTerms方法的典型用法代码示例。如果您正苦于以下问题:Java StatusesFilterEndpoint.trackTerms方法的具体用法?Java StatusesFilterEndpoint.trackTerms怎么用?Java StatusesFilterEndpoint.trackTerms使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.twitter.hbc.core.endpoint.StatusesFilterEndpoint
的用法示例。
在下文中一共展示了StatusesFilterEndpoint.trackTerms方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTwitterClient
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
private Client getTwitterClient(Properties props, BlockingQueue<String> messageQueue) {
String clientName = props.getProperty("clientName");
String consumerKey = props.getProperty("consumerKey");
String consumerSecret = props.getProperty("consumerSecret");
String token = props.getProperty("token");
String tokenSecret = props.getProperty("tokenSecret");
List<String> searchTerms = Arrays.asList(props.getProperty("searchTerms").split(","));
Authentication authentication = new OAuth1(consumerKey,consumerSecret,token,tokenSecret);
Hosts hosebirdHosts = new HttpHosts(Constants.STREAM_HOST);
StatusesFilterEndpoint hosebirdEndpoint = new StatusesFilterEndpoint();
hosebirdEndpoint.trackTerms(searchTerms);
ClientBuilder clientBuilder = new ClientBuilder();
clientBuilder.name(clientName)
.hosts(hosebirdHosts)
.authentication(authentication)
.endpoint(hosebirdEndpoint)
.processor(new StringDelimitedProcessor(messageQueue));
return clientBuilder.build();
}
示例2: setupHosebirdClient
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
public static void setupHosebirdClient() {
/** Declare the host you want to connect to, the endpoint, and authentication (basic auth or oauth) */
Hosts hosebirdHosts = new HttpHosts(Constants.STREAM_HOST);
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
// Optional: set up some followings and track terms
List<Long> followings = Lists.newArrayList(1234L, 566788L);
List<String> terms = Lists.newArrayList("twitter", "api");
endpoint.followings(followings);
endpoint.trackTerms(terms);
Authentication hosebirdAuth = new OAuth1(
Helper.properties().getProperty("consumerKey"),
Helper.properties().getProperty("consumerSecret"),
Helper.properties().getProperty("token"),
Helper.properties().getProperty("secret"));
ClientBuilder builder = new ClientBuilder()
.name("Hosebird-Client-01") // optional: mainly for the logs
.hosts(hosebirdHosts)
.authentication(hosebirdAuth)
.endpoint(endpoint)
.processor(new StringDelimitedProcessor(msgQueue));
hosebirdClient = builder.build();
}
示例3: startFiltering
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
public void startFiltering() {
List<Job> jobs = jobRepository.findAll();
log.info("There are " + jobs.size() + " jobs in the database.");
if (jobs.size() == 0) {
log.error("No jobs found at the database. Please define some jobs first.");
return;
}
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
List<String> keywords = new ArrayList<>();
for (Job j : jobs) {
keywords.addAll(j.getKeywords());
}
endpoint.trackTerms(keywords);
Authentication auth = new OAuth1(consumerKey, consumerSecret, accessToken, accessSecret);
this.client = new ClientBuilder().hosts(Constants.STREAM_HOST).endpoint(endpoint).authentication(auth)
.processor(new StringDelimitedProcessor(queue)).build();
int numProcessingThreads = 4;
ExecutorService service = Executors.newFixedThreadPool(numProcessingThreads);
List<StatusListener> listeners = new ArrayList<>();
listeners.add(statusStreamHandlerImpl.createListener());
Twitter4jStatusClient t4jClient = new Twitter4jStatusClient(client, queue, listeners, service);
t4jClient.connect();
for (int threads = 0; threads < numProcessingThreads; threads++) {
t4jClient.process();
}
}
示例4: subscribe
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
public void subscribe(final StatusStreamHandler listener, String... terms) {
/**
* Set up your blocking queues: Be sure to size these properly based on
* expected TPS of your stream
*/
BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>(100000);
BlockingQueue<Event> eventQueue = new LinkedBlockingQueue<Event>(1000);
/**
* Declare the host you want to connect to, the endpoint, and
* authentication (basic auth or oauth)
*/
Hosts hosebirdHosts = new HttpHosts(Constants.STREAM_HOST);
StatusesFilterEndpoint hosebirdEndpoint = new StatusesFilterEndpoint();
hosebirdEndpoint.trackTerms(Lists.newArrayList(terms));
Authentication hosebirdAuth = oAuth();
ClientBuilder builder = new ClientBuilder().name("Hosebird-Client-01")
// optional: mainly for the logs
.hosts(hosebirdHosts).authentication(hosebirdAuth).endpoint(hosebirdEndpoint)
.processor(new StringDelimitedProcessor(msgQueue)).eventMessageQueue(eventQueue);
Client client = builder.build();
final ExecutorService executorService = Executors.newFixedThreadPool(1);
final Twitter4jStatusClient t4jClient = new Twitter4jStatusClient(client, msgQueue,
Lists.newArrayList(listener), executorService);
t4jClient.connect();
// Call this once for every thread you want to spin off for processing
// the raw messages.
// This should be called at least once.
t4jClient.process(); // required to start processing the messages
}
示例5: start
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
public void start(String hashtag) {
BlockingQueue<String> queue = new LinkedBlockingQueue<>(10000);
StatusesSampleEndpoint endpoint = new StatusesSampleEndpoint();
endpoint.stallWarnings(false);
StatusesFilterEndpoint filterEndpoint = new StatusesFilterEndpoint();
List<String> terms = Lists.newArrayList(hashtag);
filterEndpoint.trackTerms(terms);
String consumerKey = System.getProperty("twitter.consumerKey", "");
String consumerSecret = System.getProperty("twitter.consumerSecret", "");
String token = System.getProperty("twitter.token", "");
String tokenSecret = System.getProperty("twitter.tokenSecret", "");
Authentication auth = new OAuth1(consumerKey,consumerSecret,token,tokenSecret);
client = new ClientBuilder()
.name("JDG #" + hashtag + " client")
.hosts(Constants.STREAM_HOST)
.endpoint(filterEndpoint)
.authentication(auth)
.processor(new StringDelimitedProcessor(queue))
.build();
TwitterReader reader = new TwitterReader(client, queue, cache, timeout);
executor = Executors.newSingleThreadExecutor();
executor.execute(reader);
}
示例6: activate
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
public void activate()
{
synchronized (_pauseSyncObject)
{
try
{
_tweetQueue = new LinkedBlockingQueue<String>(TWEETQUEUESIZE);
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
endpoint.trackTerms(Lists.newArrayList(_trackTerm));
Authentication authentication = new OAuth1(_consumerKey, _consumerSecret, _token, _tokenSecret);
ClientBuilder twitterClientBuilder = new ClientBuilder();
twitterClientBuilder.name("DataBrokerClient");
twitterClientBuilder.hosts(Constants.STREAM_HOST);
twitterClientBuilder.endpoint(endpoint);
twitterClientBuilder.authentication(authentication);
twitterClientBuilder.processor(new StringDelimitedProcessor(_tweetQueue));
_twitterClient = twitterClientBuilder.build();
_twitterClient.connect();
}
catch (Throwable throwable)
{
logger.log(Level.WARNING, "TwitterDataSource: Configuring problem \"" + _name + "\"", throwable);
_twitterClient = null;
_tweetQueue = null;
}
_fetch = true;
_pauseSyncObject.notify();
}
}
示例7: run
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
public static void run(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
// add some track terms
endpoint.trackTerms(Lists.newArrayList("twitterapi", "#yolo"));
Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
// Authentication auth = new BasicAuth(username, password);
// Create a new BasicClient. By default gzip is enabled.
Client client = new ClientBuilder()
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(auth)
.processor(new StringDelimitedProcessor(queue))
.build();
// Establish a connection
client.connect();
// Do whatever needs to be done with messages
for (int msgRead = 0; msgRead < 1000; msgRead++) {
String msg = queue.take();
System.out.println(msg);
}
client.stop();
}
示例8: init
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
@PostConstruct
public void init() {
BlockingQueue<String> queue = new LinkedBlockingQueue<>(100);
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
endpoint.trackTerms(ImmutableList.of("ExtJS", "#extjs", "Sencha", "#java",
"java8", "java9", "#websocket", "#SpringFramework", "html5", "javascript",
"#kotlin", "kotlin"));
endpoint.languages(ImmutableList.of("en", "de"));
String consumerKey = this.environment.getProperty("twitter4j.oauth.consumerKey");
String consumerSecret = this.environment
.getProperty("twitter4j.oauth.consumerSecret");
String accessToken = this.environment.getProperty("twitter4j.oauth.accessToken");
String accessTokenSecret = this.environment
.getProperty("twitter4j.oauth.accessTokenSecret");
Authentication auth = new OAuth1(consumerKey, consumerSecret, accessToken,
accessTokenSecret);
this.client = new ClientBuilder().hosts(Constants.STREAM_HOST).endpoint(endpoint)
.authentication(auth).processor(new StringDelimitedProcessor(queue))
.build();
this.executorService = Executors.newSingleThreadExecutor();
TwitterStatusListener statusListener = new TwitterStatusListener(
this.messagingTemplate, this.lastTweets);
this.t4jClient = new Twitter4jStatusClient(this.client, queue,
ImmutableList.of(statusListener), this.executorService);
this.t4jClient.connect();
this.t4jClient.process();
}
示例9: TwitterClient
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
public TwitterClient() {
/** Set up your blocking queues: Be sure to size these properly based on expected TPS of your stream */
BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>(100000);
BlockingQueue<Event> eventQueue = new LinkedBlockingQueue<Event>(1000);
/** Declare the host you want to connect to, the endpoint, and authentication (basic auth or oauth) */
Hosts hosebirdHosts = new HttpHosts(Constants.STREAM_HOST);
StatusesFilterEndpoint hosebirdEndpoint = new StatusesFilterEndpoint();
// Optional: set up some followings and track terms
List<Long> followings = Lists.newArrayList(1234L, 566788L);
List<String> terms = Lists.newArrayList("twitter", "api");
hosebirdEndpoint.followings(followings);
hosebirdEndpoint.trackTerms(terms);
// These secrets should be read from a config file
Authentication hosebirdAuth = new OAuth1("consumerKey", "consumerSecret", "token", "secret");
ClientBuilder builder = new ClientBuilder()
.name("Hosebird-Client-01") // optional: mainly for the logs
.hosts(hosebirdHosts)
.authentication(hosebirdAuth)
.endpoint(new StatusesSampleEndpoint())
.processor(new StringDelimitedProcessor(msgQueue))
.eventMessageQueue(eventQueue); // optional: use this if you want to process client events
Client hosebirdClient = builder.build();
// Attempts to establish a connection.
hosebirdClient.connect();
}
示例10: stream
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
public Stream<TweetHandler> stream() {
String myKey = "sl2WbCf4UnIr08xvHVitHJ99r";
String mySecret = "PE6yauvXjKLuvoQNXZAJo5C8N5U5piSFb3udwkoI76paK6KyqI";
String myToken = "1098376471-p6iWfxCLtyMvMutTb010w1D1xZ3UyJhcC2kkBjN";
String myAccess = "2o1uGcp4b2bFynOfu2cA1uz63n5aruV0RwNsUjRpjDBZS";
out.println("Creating Twitter Stream");
BlockingQueue<String> statusQueue = new LinkedBlockingQueue<>(1000);
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
endpoint.trackTerms(Lists.newArrayList("twitterapi", this.topic));
endpoint.stallWarnings(false);
Authentication twitterAuth = new OAuth1(myKey, mySecret, myToken, myAccess);
BasicClient twitterClient = new ClientBuilder()
.name("Twitter client")
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(twitterAuth)
.processor(new StringDelimitedProcessor(statusQueue))
.build();
twitterClient.connect();
List<TweetHandler> list = new ArrayList();
List<String> twitterList = new ArrayList();
statusQueue.drainTo(twitterList);
for(int i=0; i<numberOfTweets; i++) {
String message;
try {
message = statusQueue.take();
list.add(new TweetHandler(message));
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
// for (int msgRead = 0; msgRead < this.numberOfTweets; msgRead++) {
// try {
// if (twitterClient.isDone()) {
// // out.println(twitterClient.getExitEvent().getMessage());
// break;
// }
//
// String msg = statusQueue.poll(10, TimeUnit.SECONDS);
// if (msg == null) {
// out.println("Waited 10 seconds - no message received");
// } else {
// list.add(new TweetHandler(msg));
// out.println("Added message: " + msg.length());
// }
// } catch (InterruptedException ex) {
// ex.printStackTrace();
// }
// }
twitterClient.stop();
out.printf("%d messages processed!\n", twitterClient.getStatsTracker().getNumMessages());
return list.stream();
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:61,代码来源:TwitterStream.java
示例11: getHosebirdClient
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
/**
* Gets the Twitter client.
* @param buffer the queue which the client reads into
* @param readerConfig the Twitter reader configuration
* @return the built Twitter client
*/
@SuppressWarnings("checkstyle:designforextension")
@Override
protected Client getHosebirdClient(
final LinkedBlockingQueue<String> buffer,
final Config readerConfig) {
getLogger().info("Building Twitter client");
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
TwitterApiConfig config = ((TwitterApiReaderConfig) readerConfig).twitterapi;
// Term tracking
if (config.keywords != null) {
endpoint.trackTerms(config.keywords);
}
// User ID tracking
if (config.userIds != null) {
endpoint.followings(config.userIds);
}
// Authentication
Authentication auth = new OAuth1(
config.consumerKey,
config.consumerSecret,
config.accessToken,
config.accessSecret
);
// Processor
LineStringProcessor processor =
new LineStringProcessor(buffer, readerConfig.hosebird.bufferTimeout);
// Create a new BasicClient. By default gzip is enabled.
return this.getClientBuilder()
.name("Twitter Api Reader")
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(auth)
.retries(readerConfig.hosebird.retries)
.processor(processor)
.build();
}
示例12: collect
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
public static boolean collect(String configPath) {
boolean resultado = false;
logger.info("@@@ Starting tweet collecting process...");
try {
Config config = getConfig(configPath);
messages.clear();
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
// add some track terms
endpoint.trackTerms(Arrays.asList(config.terms));
Authentication auth = new OAuth1(config.consumerKey, config.consumerSecret,
config.token, config.secret);
// Authentication auth = new BasicAuth(username, password);
// Create a new BasicClient. By default gzip is enabled.
Client client = new ClientBuilder()
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(auth)
.processor(new StringDelimitedProcessor(queue))
.build();
// Establish a connection
client.connect();
Thread t = new Thread(new Background(queue,config,client));
t.start();
t.join();
/*
for (int msgRead = 0; msgRead < config.limit; msgRead++) {
String msg = queue.take();
System.out.println(msg);
messages.add(msg);
}
*/
resultado = true;
}
catch (Exception ex) {
logger.error(">>> Error getting config file. Path: "
+ configPath + ", error: " + ex.getMessage());
}
return resultado;
}
示例13: oauth
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
public static void oauth(String consumerKey, String consumerSecret, String token, String secret) {
// 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.
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
endpoint.trackTerms(Lists.newArrayList("storm", "precipitation", "tornado", "blizzard", "weather"));
endpoint.stallWarnings(false);
Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
//Authentication auth = new com.twitter.hbc.httpclient.auth.BasicAuth(username, password);
// Create a new BasicClient. By default gzip is enabled.
BasicClient client = new ClientBuilder()
.name("sampleExampleClient")
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(auth)
.processor(new StringDelimitedProcessor(queue))
.build();
// Establish a connection
client.connect();
JSONObjectParser parser = new JSONObjectParser();
// Do whatever needs to be done with messages
for (int msgRead = 0; msgRead < 200000; msgRead++) {
if (client.isDone()) {
System.out.println("Client connection closed unexpectedly: " + client.getExitEvent().getMessage());
break;
}
String msg;
JSONArray jsonArray = new JSONArray();
try {
msg = queue.poll(5, TimeUnit.SECONDS);
if (msg == null) {
System.out.println("Did not receive a message in 5 seconds");
} else {
// TODO: we create a jsonObject that only contains fields we care about.
// Right now we're just dumping the whole thing.
//System.out.println(msg);
//build a tweet from the message
buildTweetFromMessage(msg);
}
} catch (Exception e) {
//TODO: we need to be able to restart the connection if it happens to fail.
e.printStackTrace();
}
}
client.stop();
// Print some stats
System.out.printf("The client read %d messages!\n", client.getStatsTracker().getNumMessages());
}
示例14: start
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
@Start
public void start() {
queue = new LinkedBlockingQueue<>(100000);
BlockingQueue<Event> eventQueue = new LinkedBlockingQueue<>(1000);
// Declare the host you want to connect to, the endpoint, and authentication (basic auth or oauth)
Hosts hosebirdHosts = new HttpHosts(Constants.STREAM_HOST);
StatusesFilterEndpoint hosebirdEndpoint = new StatusesFilterEndpoint();
// set up some track terms
if (trackTerms != null && !trackTerms.isEmpty()) {
hosebirdEndpoint.trackTerms(Lists.newArrayList(trackTerms.split(" ")));
}
// set up some followings
if (followingIDs != null && !followingIDs.isEmpty()) {
Set<Long> followings = new HashSet<>();
for (String id: followingIDs.split(" ")) {
followings.add(Long.parseLong(id));
}
hosebirdEndpoint.followings(Lists.newArrayList(followings));
}
// These secrets should be read from a config file
Authentication hosebirdAuth = new OAuth1(consumerKey, consumerSecret, token, secret);
ClientBuilder builder = new ClientBuilder()
.name("twitter-client")
.hosts(hosebirdHosts)
.authentication(hosebirdAuth)
.endpoint(hosebirdEndpoint)
.processor(new StringDelimitedProcessor(queue))
.eventMessageQueue(eventQueue);
client = builder.build();
// Attempts to establish a connection.
client.connect();
executor.submit(() -> {
while (client != null && !client.isDone()) {
try {
String msg = queue.poll(5000, TimeUnit.MILLISECONDS);
if (msg != null) {
out.send(msg, null);
}
} catch (InterruptedException e) {
Log.warn("Twitter messages blocking queue interrupted while waiting.");
}
}
});
}
示例15: main
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> msgQueue = new LinkedBlockingDeque<>();
Hosts hosebirdHosts = new HttpHosts(Constants.STREAM_HOST);
StatusesFilterEndpoint hosebirdEndpoint = new StatusesFilterEndpoint();
List<String> terms = Lists.newArrayList("superman vs batman","#supermanvsbatman");
hosebirdEndpoint.trackTerms(terms);
Authentication hosebirdAuth = new OAuth1("18qydWMuiUohwCtQpp1MOFCFr",
"YrYhYd09LKZLbhsKT1o4XcEPl6HiAoNykiOxYBq0dAB8t0vRCo",
"16972669-KSvyDEMc7dussPfW6a9Ru65L4eWGj637ciHLHZLyn",
"ky53NE6cbBvtNLopto7o9gVyHDejSB2kPsRhHGKEd1MrS");
ClientBuilder clientBuilder = new ClientBuilder();
clientBuilder.name("bbejeck-hosebird")
.hosts(hosebirdHosts)
.authentication(hosebirdAuth)
.endpoint(hosebirdEndpoint)
.processor(new StringDelimitedProcessor(msgQueue));
Client hosebirdClient = clientBuilder.build();
hosebirdClient.connect();
for (int msgRead = 0; msgRead < 100; msgRead++) {
String msg = msgQueue.take();
System.out.println(msg);
}
hosebirdClient.stop();
}