public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { CompositeKey temp = new CompositeKey(); String url = value.toString().split("\\*\\*\\^\\*\\*")[0]; String users = value.toString().split("\\*\\*\\^\\*\\*")[3]; String tagweightpair = value.toString().split("\\*\\*\\^\\*\\*")[4]; String tag = ""; String weight = ""; StringTokenizer pair = new StringTokenizer(tagweightpair); while (pair.hasMoreTokens()) { String token = pair.nextToken(); tag = token.toString().split("==")[0]; System.out.println(tag); weight = token.toString().split("==")[1]; char c; c = tag.toString().charAt(0); if ((c >= 'a' && c <= 'z') || (c >= 'A' || c <= 'Z')) { temp.settag(tag + "\t" + url); System.out.println("temp:" + temp.gettag()); temp.setweight(Double.parseDouble(weight) * Double.parseDouble(users)); context.write(temp, new Text("LAX")); } } }
public void reduce(CompositeKey key, Iterable<Text> values, Context context) throws IOException, InterruptedException { int count = 0; for (Text val : values) { context.write(new Text(key.gettag() + "," + key.getweight()), new Text("")); count++; } }
public void reduce( CompositeKey key, Iterator<PairOfLongInt> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException { // note that values are sorted (by using MR's secondary sort) // below, builder will generate: // CustoerID,Date1,Amount1,Date2,Amount2,...,DateN,AmountN // where Date1 <= Date2 <= ... <= DateN StringBuilder builder = new StringBuilder(); builder.append(key.toString()); while (values.hasNext()) { builder.append(","); PairOfLongInt pair = values.next(); long timestamp = pair.getLeftElement(); // date as milliseconds String date = DateUtil.getDateAsString(timestamp); builder.append(date); // date as String builder.append(","); builder.append(pair.getRightElement()); // amount } output.collect(null, new Text(builder.toString())); } // reduce
@Override protected void reduce(CompositeKey key, Iterable<Text> values, Context context) throws IOException, InterruptedException { String lastIp = null; long pages = 0; for (Text t : values) { String ip = t.toString(); if (lastIp == null) { lastIp = ip; pages++; } else if (!lastIp.equals(ip)) { lastIp = ip; pages++; } else if (lastIp.compareTo(ip) > 0) { throw new IOException("secondary sort failed"); } } pageViews.set(pages); context.write(key.getFirst(), pageViews); }
/** * This reflects the POJO and builds the SQL strings for insert, select, update and delete * commands * * @throws LoaderException */ void createSQLStrings() { if (insertSQL != null) return; Table t = (Table) theClass.getAnnotation(Table.class); if (t != null) { tableName = t.name(); if (tableName.isEmpty()) { logger.log(Level.CONFIG, "Table annotation name cannot be empty for " + theClass.getName()); throw new ObjectGridRuntimeException( "Table name cannot be empty for " + theClass.getName()); } } else { logger.log(Level.CONFIG, "Table annotation require for " + theClass.getName()); throw new ObjectGridRuntimeException( "@Table annotation must be present for " + theClass.getName()); } keyFieldNames = new ArrayList<String>(); keyFields = new ArrayList<Field>(); normalFieldNames = new ArrayList<String>(); normalFields = new ArrayList<Field>(); allFieldNames = new ArrayList<String>(); allFields = new ArrayList<Field>(); CompositeKey ckey = (CompositeKey) theClass.getAnnotation(CompositeKey.class); SimpleKey skey = (SimpleKey) theClass.getAnnotation(SimpleKey.class); if (ckey == null && skey == null) { String s = "Pojo " + theClass.getName() + " needs either SimpleKey or CompositeKey annotations"; logger.log(Level.SEVERE, s); throw new ObjectGridRuntimeException(s); } if (ckey != null && skey != null) { String s = "Pojo " + theClass.getName() + " cannot have both SimpleKey or CompositeKey annotations"; logger.log(Level.SEVERE, s); throw new ObjectGridRuntimeException(s); } if (ckey != null) { keyClass = ckey.clazz(); Field[] fields = keyClass.getDeclaredFields(); for (Field f : fields) { if ((f.getModifiers() & Modifier.STATIC) == 0) { keyFieldNames.add(f.getName()); keyFields.add(f); allFieldNames.add(f.getName()); allFields.add(f); } } } if (skey != null) { try { keyClass = skey.clazz(); keyAttributeColumn = skey.name(); Field f = ValueHolder.class.getDeclaredField("value"); keyFieldNames.add(VALUE_FIELD); keyFields.add(f); allFieldNames.add(VALUE_FIELD); allFields.add(f); } catch (NoSuchFieldException e) { logger.log(Level.SEVERE, "_wxsutil_value property not found on ValueHolder class"); throw new ObjectGridRuntimeException( "_wxsutil_value property not found on ValueHolder class"); } } Field[] fields = theClass.getDeclaredFields(); for (Field f : fields) { // ignore static fields if ((f.getModifiers() & Modifier.STATIC) == 0) { // is it a key field? if (f.getAnnotation(Id.class) != null) { throw new ObjectGridRuntimeException("Id annotation not allowed"); } else { normalFieldNames.add(f.getName()); normalFields.add(f); } allFieldNames.add(f.getName()); allFields.add(f); } } insertSQL = "INSERT INTO " + tableName + " ("; for (int i = 0; i < allFieldNames.size(); ++i) { insertSQL += getSQLColumnName(allFields.get(i)) + ((i != allFieldNames.size() - 1) ? "," : ""); } insertSQL += ") VALUES ("; for (int i = 0; i < allFieldNames.size(); ++i) { insertSQL += ":" + getSQLColumnName(allFields.get(i)) + ((i != allFieldNames.size() - 1) ? "," : ""); } insertSQL += ")"; deleteSQL = "DELETE FROM " + tableName; String whereSQL = " WHERE "; for (int i = 0; i < keyFieldNames.size(); ++i) { whereSQL += getSQLColumnName(keyFields.get(i)) + "=:" + keyFieldNames.get(i) + ((i != keyFieldNames.size() - 1) ? " AND " : ""); } deleteSQL += whereSQL; updateSQL = "UPDATE " + tableName + " SET "; for (int i = 0; i < normalFieldNames.size(); ++i) { updateSQL += getSQLColumnName(normalFields.get(i)) + "=:" + normalFieldNames.get(i) + ((i != normalFieldNames.size() - 1) ? "," : ""); } updateSQL += whereSQL; selectSQL = "SELECT "; for (int i = 0; i < allFieldNames.size(); ++i) { selectSQL += getSQLColumnName(allFields.get(i)) + ((i != allFieldNames.size() - 1) ? "," : ""); } selectSQL += " FROM " + tableName + whereSQL; logger.fine("ISQL: " + insertSQL); logger.fine("USQL: " + updateSQL); logger.fine("DSQL: " + deleteSQL); }