Created
August 7, 2014 07:54
-
-
Save yusuke/850909563dc7cda9427d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| Copyright 2013 Yusuke Yamamoto | |
| Licensed under the Apache License, Version 2.0 (the "License"); | |
| you may not use this file except in compliance with the License. | |
| You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software | |
| Distributed under the License is distributed on an "AS IS" BASIS, | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | |
| limitations under the License. | |
| */ | |
| package elastict4j; | |
| import org.elasticsearch.action.index.IndexRequest; | |
| import org.elasticsearch.action.index.IndexResponse; | |
| import org.elasticsearch.client.Client; | |
| import org.elasticsearch.node.Node; | |
| import static org.elasticsearch.node.NodeBuilder.*; | |
| import twitter4j.*; | |
| import twitter4j.internal.org.json.JSONException; | |
| import twitter4j.internal.org.json.JSONObject; | |
| public class Main { | |
| public static void main(String... args) { | |
| TwitterStream twitter = TwitterStreamFactory.getSingleton(); | |
| Node node = nodeBuilder().node(); | |
| final Client client = node.client(); | |
| twitter.addListener(new StatusAdapter() { | |
| @Override | |
| public void onStatus(Status status) { | |
| System.out.println(status.getText()); | |
| try { | |
| JSONObject json = new JSONObject(); | |
| json.put("screen_name", status.getUser().getScreenName()); | |
| json.put("text", status.getText()); | |
| IndexResponse response = client.prepareIndex("twitter", "tweet") | |
| .setSource(json.toString()) | |
| .execute() | |
| .actionGet(); | |
| System.out.println(response); | |
| } catch (JSONException ignore) { | |
| } | |
| } | |
| }); | |
| twitter.sample(); | |
| // on shutdown | |
| // node.close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment