You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streams.apache.org by sb...@apache.org on 2014/05/23 19:38:29 UTC
git commit: Resolves STREAMS-59
Repository: incubator-streams
Updated Branches:
refs/heads/STREAMS-59 [created] a230b963f
Resolves STREAMS-59
Project: http://git-wip-us.apache.org/repos/asf/incubator-streams/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-streams/commit/a230b963
Tree: http://git-wip-us.apache.org/repos/asf/incubator-streams/tree/a230b963
Diff: http://git-wip-us.apache.org/repos/asf/incubator-streams/diff/a230b963
Branch: refs/heads/STREAMS-59
Commit: a230b963fd6a5e6e40f78a355330d5c1da9097b5
Parents: a4573f2
Author: sblackmon <sb...@w2odigital.com>
Authored: Fri May 23 12:38:26 2014 -0500
Committer: sblackmon <sb...@w2odigital.com>
Committed: Fri May 23 12:38:26 2014 -0500
----------------------------------------------------------------------
.../ElasticsearchPersistWriter.java | 45 ++++++++++++++------
.../serializer/StreamsTwitterMapper.java | 12 ++++++
2 files changed, 44 insertions(+), 13 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/a230b963/streams-contrib/streams-persist-elasticsearch/src/main/java/org/apache/streams/elasticsearch/ElasticsearchPersistWriter.java
----------------------------------------------------------------------
diff --git a/streams-contrib/streams-persist-elasticsearch/src/main/java/org/apache/streams/elasticsearch/ElasticsearchPersistWriter.java b/streams-contrib/streams-persist-elasticsearch/src/main/java/org/apache/streams/elasticsearch/ElasticsearchPersistWriter.java
index 271521c..aacb61f 100644
--- a/streams-contrib/streams-persist-elasticsearch/src/main/java/org/apache/streams/elasticsearch/ElasticsearchPersistWriter.java
+++ b/streams-contrib/streams-persist-elasticsearch/src/main/java/org/apache/streams/elasticsearch/ElasticsearchPersistWriter.java
@@ -17,6 +17,7 @@ import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
+import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Client;
@@ -173,15 +174,22 @@ public class ElasticsearchPersistWriter implements StreamsPersistWriter, Flushab
public void write(StreamsDatum streamsDatum) {
String json;
+ String id = null;
+ String ts = null;
try {
- String id = streamsDatum.getId();
+ if( streamsDatum.getId() != null ) {
+ id = streamsDatum.getId();
+ }
+ if( streamsDatum.getTimestamp() != null ) {
+ ts = Long.toString(streamsDatum.getTimestamp().getMillis());
+ }
if (streamsDatum.getDocument() instanceof String)
json = streamsDatum.getDocument().toString();
else {
json = mapper.writeValueAsString(streamsDatum.getDocument());
}
- add(config.getIndex(), config.getType(), id, json);
+ add(config.getIndex(), config.getType(), id, ts, json);
} catch (Exception e) {
LOGGER.warn("{} {}", e.getMessage());
@@ -348,19 +356,28 @@ public class ElasticsearchPersistWriter implements StreamsPersistWriter, Flushab
}
public void add(String indexName, String type, String json) {
- add(indexName, type, null, json);
+ add(indexName, type, null, null, json);
}
public void add(String indexName, String type, String id, String json) {
- IndexRequest indexRequest;
+ add(indexName, type, id, null, json);
+ }
+
+ public void add(String indexName, String type, String id, String ts, String json)
+ {
+ IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(manager.getClient());
+
+ indexRequestBuilder.setIndex(indexName);
+ indexRequestBuilder.setType(type);
- // They didn't specify an ID, so we will create one for them.
- if (id == null)
- indexRequest = new IndexRequest(indexName, type);
- else
- indexRequest = new IndexRequest(indexName, type, id);
+ indexRequestBuilder.setSource(json);
- indexRequest.source(json);
+ // / They didn't specify an ID, so we will create one for them.
+ if(id != null)
+ indexRequestBuilder.setId(id);
+
+ if(ts != null)
+ indexRequestBuilder.setTimestamp(ts);
// If there is a parentID that is associated with this bulk, then we are
// going to have to parse the raw JSON and attempt to dereference
@@ -369,13 +386,15 @@ public class ElasticsearchPersistWriter implements StreamsPersistWriter, Flushab
try {
// The JSONObject constructor can throw an exception, it is called
// out explicitly here so we can catch it.
- indexRequest = indexRequest.parent(new JSONObject(json).getString(parentID));
- } catch (JSONException e) {
+ indexRequestBuilder.setParent(new JSONObject(json).getString(parentID));
+ }
+ catch(JSONException e)
+ {
LOGGER.warn("Malformed JSON, cannot grab parentID: {}@{}[{}]: {}", id, indexName, type, e.getMessage());
totalFailed++;
}
}
- add(indexRequest);
+ add(indexRequestBuilder.request());
}
public void add(UpdateRequest updateRequest) {
http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/a230b963/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/StreamsTwitterMapper.java
----------------------------------------------------------------------
diff --git a/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/StreamsTwitterMapper.java b/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/StreamsTwitterMapper.java
index 6b61036..da17af6 100644
--- a/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/StreamsTwitterMapper.java
+++ b/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/StreamsTwitterMapper.java
@@ -24,6 +24,18 @@ public class StreamsTwitterMapper extends StreamsJacksonMapper {
public static final DateTimeFormatter TWITTER_FORMAT = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss Z yyyy");
+ public static final Long getMillis(String dateTime) {
+
+ // this function is for pig which doesn't handle exceptions well
+ try {
+ Long result = TWITTER_FORMAT.parseMillis(dateTime);
+ return result;
+ } catch( Exception e ) {
+ return null;
+ }
+
+ }
+
private static final StreamsTwitterMapper INSTANCE = new StreamsTwitterMapper();
public static StreamsTwitterMapper getInstance(){