Skip to content

Instantly share code, notes, and snippets.

@yusuke
Created August 7, 2014 07:54
Show Gist options
  • Select an option

  • Save yusuke/850909563dc7cda9427d to your computer and use it in GitHub Desktop.

Select an option

Save yusuke/850909563dc7cda9427d to your computer and use it in GitHub Desktop.
/*
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