本文整理汇总了Java中com.twitter.hbc.httpclient.BasicClient.connect方法的典型用法代码示例。如果您正苦于以下问题:Java BasicClient.connect方法的具体用法?Java BasicClient.connect怎么用?Java BasicClient.connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.twitter.hbc.httpclient.BasicClient
的用法示例。
在下文中一共展示了BasicClient.connect方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: streamTwitter
import com.twitter.hbc.httpclient.BasicClient; //导入方法依赖的package包/类
public static void streamTwitter(String consumerKey, String consumerSecret, String accessToken, String accessSecret) throws InterruptedException {
BlockingQueue<String> statusQueue = new LinkedBlockingQueue<String>(10000);
StatusesSampleEndpoint ending = new StatusesSampleEndpoint();
ending.stallWarnings(false);
Authentication twitterAuth = new OAuth1(consumerKey, consumerSecret, accessToken, accessSecret);
BasicClient twitterClient = new ClientBuilder()
.name("Twitter client")
.hosts(Constants.STREAM_HOST)
.endpoint(ending)
.authentication(twitterAuth)
.processor(new StringDelimitedProcessor(statusQueue))
.build();
twitterClient.connect();
for (int msgRead = 0; msgRead < 1000; msgRead++) {
if (twitterClient.isDone()) {
System.out.println(twitterClient.getExitEvent().getMessage());
break;
}
String msg = statusQueue.poll(10, TimeUnit.SECONDS);
if (msg == null) {
System.out.println("Waited 10 seconds - no message received");
} else {
System.out.println(msg);
}
}
twitterClient.stop();
System.out.printf("%d messages processed!\n", twitterClient.getStatsTracker().getNumMessages());
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:40,代码来源:SampleStreamExample.java
示例2: main
import com.twitter.hbc.httpclient.BasicClient; //导入方法依赖的package包/类
public static void main(String... args) throws InterruptedException, MalformedURLException, URISyntaxException {
int maxReads = 1000000;
BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>(10000);
List<Long> userIds = asList();
String searchTerms = getenv("TWITTER_TERMS") != null ? getenv("TWITTER_TERMS") : "happy";
List<String> terms = asList(searchTerms.split(","));
BasicClient client = configureStreamClient(msgQueue, getenv("TWITTER_KEYS"), userIds, terms);
TwitterNeo4jWriter writer = new TwitterNeo4jWriter(getenv("NEO4J_URL"));
writer.init();
int numProcessingThreads = Math.max(1,Runtime.getRuntime().availableProcessors() - 1);
ExecutorService service = Executors.newFixedThreadPool(numProcessingThreads);
client.connect();
List<String> buffer = new ArrayList<>(BATCH);
for (int msgRead = 0; msgRead < maxReads; msgRead++) {
if (client.isDone()) {
System.err.println("Client connection closed unexpectedly: " + client.getExitEvent().getMessage());
break;
}
String msg = msgQueue.poll(5, TimeUnit.SECONDS);
if (msg == null) System.out.println("Did not receive a message in 5 seconds");
else buffer.add(msg);
if (buffer.size() < BATCH) continue;
List<String> tweets = buffer;
service.submit(() -> writer.insert(tweets,3));
buffer = new ArrayList<>(BATCH);
}
client.stop();
writer.close();
}
示例3: stream
import com.twitter.hbc.httpclient.BasicClient; //导入方法依赖的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
示例4: oauth
import com.twitter.hbc.httpclient.BasicClient; //导入方法依赖的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());
}
示例5: run
import com.twitter.hbc.httpclient.BasicClient; //导入方法依赖的package包/类
public static void run(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
// 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();
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();
// Do whatever needs to be done with messages
for (int msgRead = 0; msgRead < 1000; msgRead++) {
if (client.isDone()) {
System.out.println("Client connection closed unexpectedly: " + client.getExitEvent().getMessage());
break;
}
String msg = queue.poll(5, TimeUnit.SECONDS);
if (msg == null) {
System.out.println("Did not receive a message in 5 seconds");
} else {
System.out.println(msg);
}
}
client.stop();
// Print some stats
System.out.printf("The client read %d messages!\n", client.getStatsTracker().getNumMessages());
}