com.twitter.hbc.core.Client Java Examples

The following examples show how to use com.twitter.hbc.core.Client. 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: TwitterDataSource.java    From kafka-streams with Apache License 2.0 6 votes vote down vote up
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();

    }
 
Example #2
Source File: EnterpriseStreamExample.java    From hbc with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #3
Source File: FilterStreamExample.java    From hbc with Apache License 2.0 5 votes vote down vote up
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();

}
 
Example #4
Source File: TwitterProducer.java    From aws-big-data-blog with Apache License 2.0 4 votes vote down vote up
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();
	}
}
 
Example #5
Source File: Twitter4jUserstreamClient.java    From hbc with Apache License 2.0 4 votes vote down vote up
public Twitter4jUserstreamClient(Client client, BlockingQueue<String> blockingQueue, List<UserStreamListener> listeners, ExecutorService executorService) {
  super(client, blockingQueue, executorService);
  Preconditions.checkNotNull(listeners);
  this.userstreamListeners = ImmutableList.copyOf(listeners);
}
 
Example #6
Source File: Twitter4jSitestreamClient.java    From hbc with Apache License 2.0 4 votes vote down vote up
public Twitter4jSitestreamClient(Client client, BlockingQueue<String> blockingQueue, List<SiteStreamsListener> listeners, ExecutorService executorService) {
  super(client, blockingQueue, executorService);
  Preconditions.checkNotNull(listeners);
  this.sitestreamListeners = ImmutableList.copyOf(listeners);
  this.streamId = new AtomicReference<String>();
}
 
Example #7
Source File: Twitter4jStatusClient.java    From hbc with Apache License 2.0 4 votes vote down vote up
public Twitter4jStatusClient(Client client, BlockingQueue<String> blockingQueue, List<? extends StatusListener> listeners, ExecutorService executorService) {
  super(client, blockingQueue, executorService);
  Preconditions.checkNotNull(listeners);
  this.statusListeners = ImmutableList.copyOf(listeners);
}
 
Example #8
Source File: BaseTwitter4jClient.java    From hbc with Apache License 2.0 4 votes vote down vote up
protected BaseTwitter4jClient(Client client, BlockingQueue<String> blockingQueue, ExecutorService executorService) {
  this.client = Preconditions.checkNotNull(client);
  this.messageQueue = Preconditions.checkNotNull(blockingQueue);
  this.executorService = Preconditions.checkNotNull(executorService);
  this.factory = new PublicObjectFactory(new ConfigurationBuilder().build());
}
 
Example #9
Source File: HoseBirdTester.java    From kafka-streams with Apache License 2.0 3 votes vote down vote up
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();

    }