twitter hosebird client maven usage -
twitter hosebird client maven usage -
hi i'm noob in want larn how utilize hosebird client, downloaded readme don't understand how utilize that. installed eclipse java ee , maven in pc readme file in hbc don't see how connect eclipse. can help me list of thing have do? readme, have never used maven before. thanks
class="snippet-code-html lang-html prettyprint-override">## getting started hosebird client broken downwards 2 modules: hbc-core , hbc-twitter4j. hbc-core module uses message queue, consumer can poll raw string messages, while hbc-twitter4j module uses [twitter4j](http://twitter4j.org) listeners , info model on top of message queue provide parsing layer. latest hbc artifacts published maven central. bringing hbc project should simple adding next maven pom.xml file: ```xml <dependencies> <dependency> <groupid>com.twitter</groupid> <artifactid>hbc-core</artifactid> <!-- or hbc-twitter4j --> <version>2.2.0</version> <!-- or whatever latest version --> </dependency> </dependencies> ``` ### quickstart declaring connection information: ```java /** set blocking queues: sure size these based on expected tps of stream */ blockingqueue<string> msgqueue = new linkedblockingqueue<string>(100000); blockingqueue<event> eventqueue = new linkedblockingqueue<event>(1000); /** declare host want connect to, endpoint, , authentication (basic auth or oauth) */ hosts hosebirdhosts = new httphosts(constants.stream_host); statusesfilterendpoint hosebirdendpoint = new statusesfilterendpoint(); // optional: set followings , 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 read config file authentication hosebirdauth = new oauth1("consumerkey", "consumersecret", "token", "secret"); ``` creating client: ```java clientbuilder builder = new clientbuilder() .name("hosebird-client-01") // optional: logs .hosts(hosebirdhosts) .authentication(hosebirdauth) .endpoint(hosebirdendpoint) .processor(new stringdelimitedprocessor(msgqueue)) .eventmessagequeue(eventqueue); // optional: utilize if want process client events client hosebirdclient = builder.build(); // attempts found connection. hosebirdclient.connect(); ``` now, msgqueue , eventqueue start beingness filled messages/events. read these queues like. ```java // on different thread, or multiple different threads.... while (!hosebirdclient.isdone()) { string msg = msgqueue.take(); something(msg); profit(); } ``` can close connection ```java hosebirdclient.shutdown(); ``` ### quick start illustration run sample stream example: ``` mvn install && mvn exec:java -pl hbc-example -dconsumer.key=xyz -dconsumer.secret=secret -daccess.token=abc -daccess.token.secret=abcsecret ``` can find these values on http://dev.twitter.com , navigating 1 of applications api keys tab. api key , secrets values on page correspond hbc's `-dconsumer.*` properties. alternatively can set properties in hbc-examples/pom.xml ## details ### authentication: declaring oauth1 credentials in client (preferred): ```java new oauth1("consumerkey", "consumersecret", "token", "tokensecret") ``` declaring basic auth credentials in client: ```java new basicauth("username", "password") ``` sure not pass tokens/passwords strings straight initializers. should read configuration file isn't checked in code or similar. safety first. ### specifying endpoint declare streamingendpoint connect to. these classes reside in bundle com.twitter.hbc.core.endpoint, , correspond of our endpoints. default, http parameter "delimited=length" set of our streamingendpoints compatibility our processor (next section). if using our stringdelimitedprocessor parameter must set. list of available public endpoints , http parameters support, see [twitter's streaming api docs](https://dev.twitter.com/docs/streaming-apis/streams/public). #### filter streams: ```java statusesfilterendpoint endpoint = new statusesfilterendpoint(); // optional: set followings , track terms list<long> followings = lists.newarraylist(1234l, 566788l); list<string> terms = lists.newarraylist("twitter", "api"); endpoint.followings(followings); endpoint.trackterms(terms); ``` #### firehose streams: ```java streamingendpoint endpoint = new statusesfirehoseendpoint(); // optional: set partitions want connect list<integer> partitions = lists.newarraylist(0,1,2,3); endpoint.partitions(partitions); // default, delimited=length set utilize our stringdelimitedprocessor // unset (be sure want this) // endpoint.delimited(false); ``` #### setting processor: hosebird client uses notion of "processor" processes stream , set individual messages provided blockingqueue. provide stringdelimitedprocessor class should used in conjunction streamingendpoints provided. processor takes parameter blockingqueue, client set string messages streams them. setting stringdelimitedprocessor easy as: ```java new stringdelimitedprocessor(msgqueue); ``` ### command streams sitestream connections hosebird provides [control stream back upwards sitestreams](https://dev.twitter.com/docs/streaming-apis/streams/site/control). create command stream calls hosebird client, first create client. when calling connect() create connection stream command stream support, first message receive streamid. you'll want hold on when processing messages if plan on using command streams, after calling connect(), sure maintain track of streamid of connection. note due reconnections, streamid change, utilize latest one. if you're using our twitter4j layer, keeping track of command messages/streamids taken care of you. ```java sitestreamcontroller controlstreams = client.getsitestreamcontroller(); // when making connection stream command stream back upwards 1 of response messages include streamid. // you'll want hold on when processing messages if plan on using command streams // add together userid our command stream controlstreams.adduser(streamid, userid); // remove userid our command stream controlstreams.removeuser(streamid, userid); ``` ### hbc-twitter4j module hbc-twitter4j module uses twitter4j listeners , models. utilize it, create normal client object before using clientbuilder, depending on type of stream reading from, create appropriate twitter4jclient. twitter4jclient wraps around client passed, , calls callback methods in twitter4j listeners whenever retrieves message message queue. actual work of polling message queue, parsing, , executing callback method done forking threads executor service client passed. if connecting status stream (filter, firehose, sample), utilize twitter4jstatusclient: ```java // client our client object // msgqueue our blockingqueue<string> of messages handlers receive // listeners list<statuslistener> of t4j statuslisteners // executorservice twitter4jclient t4jclient = new twitter4jstatusclient(client, msgqueue, listeners, executorservice); t4jclient.connect(); // phone call 1 time every thread want spin off processing raw messages. // should called @ to the lowest degree once. t4jclient.process(); // required start processing messages t4jclient.process(); // optional: runnable submitted executorservice process msgqueue t4jclient.process(); // optional ``` if connecting userstream, utilize twitter4juserstreamclient. if making sitestream connection, utilize twitter4jsitestreamclient. #### using handlers, twitter4j listener add-on twitter4jclients back upwards handlers, extend respective twitter4j listeners: statusstreamhandler extends statuseslistener, userstreamhandler extends userstreamlistener, sitestreamhandler extends sitestreamhandler. these handlers have callback menthods may helpful parsing messages twitter4j listeners not yet back upwards ```java userstreamlistener listener = new userstreamhandler() { /** * <userstreamlistener methods here> */ @override public void ondisconnectmessage(disconnectmessage disconnectmessage) { // method called when disconnect message received } @override public void onunfollow(user source, user target) { // } @override public void onretweet(user source, user target, status retweetedstatus) { // thing } @override public void onunknownmessagetype(string msg) { // msg message isn't handled of our other callbacks } } listeners.append(listener); twitter4jclient t4jclient = new twitter4juserstreamclient(client, msgqueue, listeners, executorservice); ``` ## building / testing build locally (you must utilize java 1.7 compiling, though produce 1.6 compatible classes): ``` mvn compile ``` run tests: ``` mvn test ```
you can install eclipse maven plugin compile eclipse, or can simple , straightforward me. start command prompt folder of unzipped hbc-master, e.g. c:\downloads\hbc-master, run next command:
c:\downloads\hbc-master>c:\downloads\apache-maven-3.2.3-bin\apache-maven -3.2.3\bin\mvn compile
this compile hbc classes. can set generated class files including hbc-master\hbc-core\target\classes\ , hbc-master\hbc-twitter4j\target\classes\ 1 folder utilize jar command zip them 1 jar file can add together eclipse project, e.g.
c:\downloads\hbc-master\mybuild>jar cvf twitter-api.jar com twitter4j
this provide classes allow run simple examples example\filterstreamexample. however, more complex examples enterprisestreamexample , twitter4jsamplestreamexample, need more grab more libraries including twitter4j-core, twitter4j-stream, , guava.jar (google collections).
hope helps.
maven twitter twitter-hbc
Comments
Post a Comment