Created
September 10, 2014 14:20
-
-
Save richcaudle/c1fbd6546ec309c56389 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.datasift.client.DataSiftClient; | |
import com.datasift.client.DataSiftConfig; | |
import com.datasift.client.core.Stream; | |
import com.datasift.client.stream.*; | |
public class Main { | |
public static void main(String[] args) { | |
// TODO: Enter your username and API key | |
DataSiftConfig config = new DataSiftConfig("YOUR_USERNAME", "YOUR_APIKEY"); | |
DataSiftClient datasift = new DataSiftClient(config); | |
try { | |
// Compile filter | |
String csdl = "interaction.content contains \"music\" AND interaction.type == \"twitter\" "; | |
Stream stream = datasift.compile(csdl).sync(); | |
datasift.liveStream().onError(new ErrorHandler()); // handles stream errors | |
datasift.liveStream().onStreamEvent(new DeleteHandler()); // handles data deletes | |
// Subscribe to the stream | |
datasift.liveStream().subscribe(new Subscription(stream)); | |
} | |
catch(Exception ex) | |
{ | |
// TODO: Your exception handling here | |
} | |
} | |
// Subscription handler | |
public static class Subscription extends StreamSubscription { | |
public Subscription(Stream stream) { | |
super(stream); | |
} | |
public void onDataSiftLogMessage(DataSiftMessage di) { | |
System.out.println((di.isError() ? "Error" : di.isInfo() ? "Info" : "Warning") + ":\n" + di); | |
} | |
public void onMessage(Interaction i) { | |
System.out.println("INTERACTION:\n" + i); | |
} | |
} | |
// Delete handler | |
public static class DeleteHandler extends StreamEventListener { | |
public void onDelete(DeletedInteraction di) { | |
// You must delete the interaction to stay compliant | |
System.out.println("DELETED:\n " + di); | |
} | |
} | |
// Error handler | |
public static class ErrorHandler extends ErrorListener { | |
public void exceptionCaught(Throwable t) { | |
t.printStackTrace(); | |
// TODO: do something useful..! | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment