com.twitter.hbc.core.endpoint.StatusesSampleEndpoint Java Examples

The following examples show how to use com.twitter.hbc.core.endpoint.StatusesSampleEndpoint. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TwitterSource.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public StreamingEndpoint createEndpoint() {
	// this default endpoint initializer returns the sample endpoint: Returning a sample from the firehose (all tweets)
	StatusesSampleEndpoint endpoint = new StatusesSampleEndpoint();
	endpoint.stallWarnings(false);
	endpoint.delimited(false);
	return endpoint;
}
 
Example #2
Source File: TwitterSource.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public StreamingEndpoint createEndpoint() {
	// this default endpoint initializer returns the sample endpoint: Returning a sample from the firehose (all tweets)
	StatusesSampleEndpoint endpoint = new StatusesSampleEndpoint();
	endpoint.stallWarnings(false);
	endpoint.delimited(false);
	return endpoint;
}
 
Example #3
Source File: SampleStreamExample.java    From Java-for-Data-Science with MIT License 5 votes vote down vote up
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());
	}
 
Example #4
Source File: SampleStatusesEndpoint.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public StreamingEndpoint createEndpoint() {
    // Returns the sample endpoint: Returning a sample from the firehose (all tweets)
    StatusesSampleEndpoint endpoint = new StatusesSampleEndpoint();
    endpoint.stallWarnings(false);
    endpoint.delimited(false);
    return endpoint;
}
 
Example #5
Source File: TwitterSource.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public StreamingEndpoint createEndpoint() {
	// this default endpoint initializer returns the sample endpoint: Returning a sample from the firehose (all tweets)
	StatusesSampleEndpoint endpoint = new StatusesSampleEndpoint();
	endpoint.stallWarnings(false);
	endpoint.delimited(false);
	return endpoint;
}
 
Example #6
Source File: ClientBuilderTest.java    From hbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuilderSuccess() {
  new ClientBuilder()
          .hosts(new HttpHosts(Constants.STREAM_HOST))
          .endpoint(new StatusesSampleEndpoint())
          .processor(new NullProcessor())
          .authentication(new BasicAuth("username", "password"))
          .build();

}
 
Example #7
Source File: ClientBuilderTest.java    From hbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidHttpMethod() {
  try {
    new ClientBuilder()
            .hosts(new HttpHosts(Constants.STREAM_HOST))
            .endpoint(StatusesSampleEndpoint.PATH, "FAIL!")
            .processor(new NullProcessor())
            .authentication(new BasicAuth("username", "password"))
            .build();
    fail();
  } catch (Exception e) {
    // expected
  }
}
 
Example #8
Source File: ClientBuilderTest.java    From hbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidHttpMethod() {
  new ClientBuilder()
          .hosts(new HttpHosts(Constants.STREAM_HOST))
          .endpoint(StatusesSampleEndpoint.PATH, "gEt")
          .processor(new NullProcessor())
          .authentication(new BasicAuth("username", "password"))
          .build();

}
 
Example #9
Source File: Twitter4jSampleStreamExample.java    From hbc with Apache License 2.0 5 votes vote down vote up
public void oauth(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();

  Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
  // Authentication auth = new BasicAuth(username, password);

  // Create a new BasicClient. By default gzip is enabled.
  BasicClient client = new ClientBuilder()
    .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);

  // Wrap our BasicClient with the twitter4j client
  Twitter4jStatusClient t4jClient = new Twitter4jStatusClient(
    client, queue, Lists.newArrayList(listener1, listener2), service);

  // Establish a connection
  t4jClient.connect();
  for (int threads = 0; threads < numProcessingThreads; threads++) {
    // This must be called once per processing thread
    t4jClient.process();
  }

  Thread.sleep(5000);

  client.stop();
}
 
Example #10
Source File: SampleStreamExample.java    From hbc with Apache License 2.0 4 votes vote down vote up
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());
}