streams-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfranklin <...@git.apache.org>
Subject [GitHub] incubator-streams pull request: Streams 41
Date Wed, 21 May 2014 19:09:53 GMT
Github user mfranklin commented on a diff in the pull request:

    https://github.com/apache/incubator-streams/pull/23#discussion_r12916054
  
    --- Diff: streams-contrib/streams-provider-datasift/src/main/java/org/apache/streams/datasift/provider/DatasiftEventProcessor.java
---
    @@ -1,105 +1,102 @@
    +/*
    +Licensed to the Apache Software Foundation (ASF) under one
    +or more contributor license agreements.  See the NOTICE file
    +distributed with this work for additional information
    +regarding copyright ownership.  The ASF licenses this file
    +to you 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 org.apache.streams.datasift.provider;
     
    -import com.datasift.client.stream.Interaction;
    +import com.fasterxml.jackson.core.JsonProcessingException;
     import com.fasterxml.jackson.databind.ObjectMapper;
    +import com.google.common.collect.Lists;
    +import org.apache.commons.lang.NotImplementedException;
     import org.apache.streams.core.StreamsDatum;
    +import org.apache.streams.core.StreamsProcessor;
     import org.apache.streams.datasift.Datasift;
     import org.apache.streams.datasift.serializer.DatasiftActivitySerializer;
    -import org.apache.streams.datasift.twitter.Twitter;
    +import org.apache.streams.jackson.StreamsJacksonMapper;
     import org.apache.streams.pojo.json.Activity;
    -import org.apache.streams.twitter.pojo.Tweet;
     import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     
    -import java.util.Queue;
    -import java.util.Random;
    +import java.util.List;
     
     /**
      * Created by sblackmon on 12/10/13.
      */
    -public class DatasiftEventProcessor implements Runnable {
    +public class DatasiftEventProcessor implements StreamsProcessor {
     
         private final static Logger LOGGER = LoggerFactory.getLogger(DatasiftEventProcessor.class);
     
    -    private ObjectMapper mapper = new ObjectMapper();
    -
    -    private Queue<Interaction> inQueue;
    -    private Queue<StreamsDatum> outQueue;
    -
    -    private Class inClass;
    +    private ObjectMapper mapper;
         private Class outClass;
    -
    -    private DatasiftActivitySerializer datasiftInteractionActivitySerializer = new DatasiftActivitySerializer();
    +    private DatasiftActivitySerializer datasiftInteractionActivitySerializer;
    +    private DatasiftTypeConverter converter;
     
         public final static String TERMINATE = new String("TERMINATE");
     
    -    public DatasiftEventProcessor(Queue<Interaction> inQueue, Queue<StreamsDatum>
outQueue, Class inClass, Class outClass) {
    -        this.inQueue = inQueue;
    -        this.outQueue = outQueue;
    -        this.inClass = inClass;
    +    public DatasiftEventProcessor(Class outClass) {
             this.outClass = outClass;
         }
     
    -    public DatasiftEventProcessor(Queue<Interaction> inQueue, Queue<StreamsDatum>
outQueue, Class outClass) {
    -        this.inQueue = inQueue;
    -        this.outQueue = outQueue;
    -        this.outClass = outClass;
    +    @Override
    +    public List<StreamsDatum> process(StreamsDatum entry) {
    +        List<StreamsDatum> result = Lists.newLinkedList();
    +        Object item = entry.getDocument();
    +        try {
    +            Datasift datasift = mapper.convertValue(item, Datasift.class);
    +            result.add(this.converter.convert(datasift));
    +        } catch (Exception e) {
    +            e.printStackTrace();
    +        }
    +        return result;
         }
     
         @Override
    -    public void run() {
    -
    -        while(true) {
    -            Object item;
    -            try {
    -                item = inQueue.poll();
    -                if(item instanceof String && item.equals(TERMINATE)) {
    -                    LOGGER.info("Terminating!");
    -                    break;
    -                }
    -
    -                Thread.sleep(new Random().nextInt(100));
    -
    -                org.apache.streams.datasift.Datasift datasift = mapper.convertValue(item,
Datasift.class);
    -
    -                // if the target is string, just pass-through
    -                if( String.class.equals(outClass)) {
    -                    outQueue.offer(new StreamsDatum(datasift.toString()));
    -
    -                }
    -                else if( Interaction.class.equals(outClass))
    -                {
    -                    outQueue.offer(new StreamsDatum(item));
    -                }
    -                else if( Tweet.class.equals(outClass))
    -                {
    -                    // convert to desired format
    -                    Twitter twitter = datasift.getTwitter();
    -
    -                    Tweet tweet = mapper.convertValue(twitter, Tweet.class);
    -
    -                    if( tweet != null ) {
    +    public void prepare(Object configurationObject) {
    +        this.mapper = new StreamsJacksonMapper();
    --- End diff --
    
    I think there is a StreamsJacksonMapper.getInstance() method you can use to not create
a new object.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message