本文整理汇总了Java中com.twitter.hbc.core.Client.connect方法的典型用法代码示例。如果您正苦于以下问题:Java Client.connect方法的具体用法?Java Client.connect怎么用?Java Client.connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.twitter.hbc.core.Client
的用法示例。
在下文中一共展示了Client.connect方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: collect
import com.twitter.hbc.core.Client; //导入方法依赖的package包/类
public void collect() throws IOException {
final BlockingQueue<String> queue = new LinkedBlockingQueue<>(QUEUE_SIZE);
final StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
// Set location to collect on
endpoint.locations(Lists.newArrayList(COLLECTION_LOCATION));
// add some track terms with this code:
// endpoint.trackTerms(Lists.newArrayList("twitterapi", "#yolo"));
final Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
// Create a new BasicClient. By default gzip is enabled.
final Client client = new ClientBuilder()
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(auth)
.processor(new StringDelimitedProcessor(queue))
.build();
// Establish a connection
client.connect();
while (!client.isDone()) {
writeTweetsToFile(getFileToWrite(), queue);
}
client.stop();
log.info("Client stopped, restart needed");
}
示例2: run
import com.twitter.hbc.core.Client; //导入方法依赖的package包/类
public static void run(String username,
String password,
String account,
String label,
String product) throws InterruptedException {
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
BasicAuth auth = new BasicAuth(username, password);
RealTimeEnterpriseStreamingEndpoint endpoint = new RealTimeEnterpriseStreamingEndpoint(account, product, label);
// Create a new BasicClient. By default gzip is enabled.
Client client = new ClientBuilder()
.name("PowerTrackClient-01")
.hosts(Constants.ENTERPRISE_STREAM_HOST)
.endpoint(endpoint)
.authentication(auth)
.processor(new LineStringProcessor(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();
}
示例3: run
import com.twitter.hbc.core.Client; //导入方法依赖的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();
}
示例4: TwitterClient
import com.twitter.hbc.core.Client; //导入方法依赖的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();
}
示例5: run
import com.twitter.hbc.core.Client; //导入方法依赖的package包/类
/**
* Connects to Hosebird then loops until the client says it is done, reading
* the message from the buffer and sending it onto the onward queue.
* @param args command line arguments
*/
public final void run(final String[] args) {
// The configuration file is always required as an argument
if (args.length == 0) {
log.error(Messages.EXIT_ARGUMENTS_EMPTY);
exit(1);
return;
}
// Parse the config file provided
Config config = parseConfigFile(args[0]);
if (config == null) {
log.error(Messages.EXIT_CONFIG);
exit(1);
return;
}
// Create the Hosebird client and buffer queue
LinkedBlockingQueue<String> buffer =
getBufferQueue(config.hosebird.bufferSize);
final Client client = getHosebirdClient(
buffer,
config);
// Initialise the metrics
this.metrics = getMetrics(client.getStatsTracker());
this.metrics.createAndStartStatsDReporter(
metrics.getRegistry(),
config.metrics.host,
config.metrics.port,
config.metrics.prefix,
config.metrics.reportingTime);
// Client to send messages to the onward queue
Producer<String, String> producer = getKafkaProducer(config.kafka);
// Ensure that on shutdown we tidy up gracefully
addShutdownHook(
client,
buffer,
config.kafka.topic,
producer);
log.info("Starting external retry loop");
while (this.getRetry()) {
// If the Hosebird client cannot connect it will retry with
// a linear back-off.
client.connect();
readAndSend(
buffer,
config.kafka.topic,
producer,
new ReadAndSendPredicate() {
@Override
public boolean process() {
return !client.isDone();
}
});
// If we get here it means that the Hosebird client has said
// that it has finished any retries and stopped. Log the reason,
// dump the stats and try connecting again. This will restart
// the back-off of the Hosebird client.
logClientExitReason(client);
metrics.disconnected.mark();
// TODO send the statistics in client.getStatsTracker() to statsd
}
}
示例6: collect
import com.twitter.hbc.core.Client; //导入方法依赖的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;
}
示例7: run
import com.twitter.hbc.core.Client; //导入方法依赖的package包/类
public void run(String propFilePath) {
loadFileProperties(propFilePath, DEFAULT_PROP_FILE_NAME);
String consumerKey = System.getProperty(TWIT_CONSUMER_KEY);
String consumerSecret = System.getProperty(TWIT_CONSUMER_SECRET);
String token = System.getProperty(TWIT_TOKEN);
String secret = System.getProperty(TWIT_SECRET);
String streamName = System.getProperty(STREAM_NAME);
String regionName = System.getProperty(REGION_NAME);
while (true) {
/**
* Set up your blocking queues: Be sure to size these properly based
* on expected TPS of your stream
*/
BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>(
10000);
/**
* Declare the host you want to connect to, the endpoint, and
* authentication (basic auth or oauth)
*/
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
// Track anything that is geo-tagged
endpoint.addQueryParameter("locations", "-180,-90,180,90");
// These secrets should be read from a config file
Authentication hosebirdAuth = new OAuth1(consumerKey,
consumerSecret, token, secret);
// create a new basic client - by default gzip is enabled
Client client = new ClientBuilder().hosts(Constants.STREAM_HOST)
.endpoint(endpoint).authentication(hosebirdAuth)
.processor(new StringDelimitedProcessor(msgQueue)).build();
client.connect();
LOG.info("Got connection to Twitter");
// create producer
ProducerClient producer = new ProducerBuilder()
.withName("Twitter")
.withStreamName(streamName)
.withRegion(regionName)
.withThreads(10)
.build();
producer.connect();
LOG.info("Got connection to Kinesis");
try {
if (process(msgQueue, producer)) {
break;
}
} catch (Exception e) {
// if we get here, our client has broken, throw away and
// recreate
e.printStackTrace();
}
// also, if we make it here, we have had a problem, so start again
client.stop();
}
}
示例8: main
import com.twitter.hbc.core.Client; //导入方法依赖的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();
}