Advertisement
Guest User

Untitled

a guest
May 29th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.93 KB | None | 0 0
  1. /**
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  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. package org.apache.bigtop.bigpetstore.generator;
  17.  
  18. import java.util.Date
  19. import org.apache.bigtop.bigpetstore.generator.util.State
  20. import org.apache.bigtop.bigpetstore.util.Pair
  21. import org.apache.bigtop.bigpetstore.util.StringUtils
  22. import org.apache.commons.lang3.{StringUtils => commonsStringUtils}
  23. import java.util.Arrays.asList
  24. import java.util.Random
  25. import scala.collection.Iterator
  26. import com.sun.org.apache.xml.internal.serializer.ToStream
  27. import java.util.{Iterator => JavaIterator}
  28. import scala.collection.JavaConversions.asJavaIterator
  29.  
  30. /**
  31.  * This class generates our data. Over time we will use it to embed bias which
  32.  * can then be teased out, i.e. by clustering/classifiers. For example:
  33.  *
  34.  * certain products <--> certain years or days
  35.  */
  36. class TransactionIteratorFactory(private val records: Int, private val state: State) {
  37.   assert(records > 0, "Number of records must be greater than 0 to generate a data iterator!")
  38.   val MINUTES_IN_DAY = 60 * 24
  39.   val MAX_PRICE = 10000
  40.   private val random = new Random(state.hashCode)
  41.   // Note: This behavior is changed from the earlier java version in that the iterator
  42.   // is recreated every-time this method is called. In the java version, calling this
  43.   // method again would result in getting a spent iterator.
  44.   def data: JavaIterator[TransactionIteratorFactory.KeyVal[String, String]] = {
  45.     new TransactionIteratorFactory.DataIterator(records, state, random)
  46.   }
  47. }
  48.  
  49. object TransactionIteratorFactory {
  50.   class KeyVal[K, V](val key: K, val value: V)
  51.  
  52.   private class DataIterator(records: Int, state: State, r: Random)
  53.       extends Iterator[KeyVal[String, String]] {
  54.     /** Add some decimals to the price */
  55.     private def fudgePrice(product: String, i: Int) = {
  56.       if (product.contains("dog")) {
  57.         i + .50f;
  58.       } else if (product.contains("cat")) {
  59.         i - .50f;
  60.       } else if (product.contains("fish")) {
  61.         i - .25f;
  62.       } else {
  63.         i + .10f;
  64.       }
  65.     }
  66.    
  67.     private var elementsProcducedCount = 0
  68.     private var repeatCount = 0
  69.    
  70.     def hasNext = elementsProcducedCount < records
  71.    
  72.     def next(): TransactionIteratorFactory.KeyVal[String,String] = {
  73.       var firstName: String = null
  74.       var lastName: String = null
  75.       val date = DataForger.randomDateInPastYears(50);
  76.      
  77.       /** Some customers come back for more :) We repeat a name up to ten times */
  78.       if (repeatCount > 0) {
  79.         repeatCount -= 1
  80.       } else {
  81.         firstName = DataForger.firstName(r)
  82.         lastName = DataForger.lastName(r)
  83.         repeatCount = (r.nextGaussian * 10f) toInt
  84.       }
  85.      
  86.       val productPrice = state.randProduct();
  87.       val fudgedPrice = fudgePrice(productPrice.getFirst(), productPrice.getSecond()).toString
  88.       val key = commonsStringUtils.join(asList("BigPetStore", "storeCode_" + state.name(),
  89.               elementsProcducedCount.toString), ",")
  90.       val value = commonsStringUtils.join(asList(firstName, lastName, date, fudgedPrice,
  91.               productPrice.getFirst()), ",")
  92.      
  93.       elementsProcducedCount += 1
  94.       new TransactionIteratorFactory.KeyVal(key, value)
  95.     }
  96.   }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement