Subversion Repositories SmartDukaan

Rev

Rev 2620 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2620 vikas 1
/*
2
 * Copyright 2010 Google Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package in.shop2020.web.mappers;
18
 
2699 vikas 19
import java.util.List;
2620 vikas 20
import java.util.logging.Logger;
21
 
22
import org.apache.hadoop.io.NullWritable;
23
 
24
import com.google.appengine.api.datastore.Entity;
25
import com.google.appengine.api.datastore.Key;
26
import com.google.appengine.tools.mapreduce.AppEngineMapper;
27
 
28
/**
29
 * A sample AppEngine mapper.
30
 * 
31
 * @author frew@google.com (Fred Wulff)
32
 */
33
public class TestMapper extends
34
        AppEngineMapper<Key, Entity, NullWritable, NullWritable> {
35
    private static final Logger log = Logger.getLogger(TestMapper.class
36
            .getName());
37
 
38
    public TestMapper() {
39
    }
40
 
41
    @Override
42
    public void taskSetup(Context context) {
43
        log.warning("Doing per-task setup");
44
    }
45
 
46
    @Override
47
    public void taskCleanup(Context context) {
48
        log.warning("Doing per-task cleanup");
49
    }
50
 
51
    @Override
52
    public void setup(Context context) {
53
        log.warning("Doing per-worker setup");
54
    }
55
 
56
    @Override
57
    public void cleanup(Context context) {
58
        log.warning("Doing per-worker cleanup");
59
    }
60
 
61
    // This is a silly mapper that's intended to show some of the capabilities
62
    // of the API.
63
    @Override
64
    public void map(Key key, Entity value, Context context) {
2699 vikas 65
        countItemEvent("ADD_TO_CART", key, value, context);
66
        countItemEvent("DELETE_FROM_CART", key, value, context);
67
        countProductEvent("PRODUCT_VIEW", key, value, context);
68
        countItemEvent("PRODUCT_COMPARE", key, value, context);
69
        countItemEvent("RESEARCH_ADD", key, value, context);
70
        countItemEvent("RESEARCH_DELETE", key, value, context);
71
    }
72
 
73
    private void countProductEvent(String eventType, Key key, Entity value, Context context) {
2620 vikas 74
        if (value.hasProperty("eventType") && value.getProperty("eventType")!=null) {
2699 vikas 75
            if (value.getProperty("eventType").toString().equals(eventType)) {
76
                    String productName = value.getProperty("productName").toString();
77
                    context.getCounter(productName, eventType).increment(1);
78
            }
2620 vikas 79
        }
80
    }
2699 vikas 81
 
82
    @SuppressWarnings("unchecked")
83
    private void countItemEvent(String eventType, Key key, Entity value,
84
            Context context) {
85
        if (value.hasProperty("eventType") && value.getProperty("eventType")!=null) {
86
            if (value.getProperty("eventType").toString().equals(eventType)) {
87
                try {
88
                    List<Long> entityIds = (List<Long>)value.getProperty("itemIds");
89
                    for (Long entityId : entityIds) {
90
                        context.getCounter(entityId.toString(), eventType).increment(1);
91
                    }
92
                }
93
                catch (ClassCastException e) {
94
                    try {
95
                        Long entityId = (Long) value.getProperty("itemIds");
96
                        context.getCounter(entityId.toString(), eventType).increment(1);
97
                    }
98
                    catch (ClassCastException e1) {
99
                        log.warning("Unable to cast entityIds to List of long or long.");
100
                    }
101
                }
102
            }
103
        }
104
    }
2620 vikas 105
}