|
| 1 | +/* |
| 2 | + * Copyright 2018, Oath Inc. |
| 3 | + * Licensed under the terms of the Apache License, Version 2.0. |
| 4 | + * See the LICENSE file associated with the project for terms. |
| 5 | + */ |
| 6 | +package com.yahoo.bullet.spark.examples.receiver |
| 7 | + |
| 8 | +import java.util.UUID |
| 9 | + |
| 10 | +import scala.collection.JavaConverters._ |
| 11 | +import scala.util.Random |
| 12 | + |
| 13 | +import com.yahoo.bullet.record.BulletRecord |
| 14 | +import com.yahoo.bullet.spark.utils.{BulletSparkConfig, BulletSparkLogger} |
| 15 | +import org.apache.spark.storage.StorageLevel |
| 16 | +import org.apache.spark.streaming.receiver.Receiver |
| 17 | + |
| 18 | + |
| 19 | +object RandomReceiver { |
| 20 | + // Fields in BulletRecord |
| 21 | + private val STRING = "uuid" |
| 22 | + private val LONG = "tuple_number" |
| 23 | + private val DOUBLE = "probability" |
| 24 | + private val BOOLEAN_MAP = "tags" |
| 25 | + private val STATS_MAP = "stats" |
| 26 | + private val LIST = "classifiers" |
| 27 | + private val DURATION = "duration" |
| 28 | + private val TYPE = "type" |
| 29 | + private val RANDOM_MAP_KEY_A = "field_A" |
| 30 | + private val RANDOM_MAP_KEY_B = "field_B" |
| 31 | + private val PERIOD_COUNT = "period_count" |
| 32 | + private val RECORD_NUMBER = "record_number" |
| 33 | + private val NANO_TIME = "nano_time" |
| 34 | + private val TIMESTAMP = "timestamp" |
| 35 | + // Some static values in BulletRecord for the fields |
| 36 | + private val STRING_POOL = Array("foo", "bar", "baz", "qux", "quux", "norf") |
| 37 | + private val INTEGER_POOL = Array(2057, 13, 10051, 2, 1059, 187) |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Constructor that takes a configuration to use. |
| 42 | + * |
| 43 | + * @param config The BulletSparkConfig to load settings from. |
| 44 | + */ |
| 45 | +class RandomReceiver(val config: BulletSparkConfig) |
| 46 | + extends Receiver[BulletRecord](StorageLevel.MEMORY_AND_DISK_SER) with BulletSparkLogger { |
| 47 | + // Number of tuples to emit |
| 48 | + private val maxPerPeriod = 100L |
| 49 | + // Period in milliseconds. Default 1000 ms |
| 50 | + private val period = 1000 |
| 51 | + private var periodCount = 0L |
| 52 | + private var generatedThisPeriod = 0L |
| 53 | + private var nextIntervalStart = 0L |
| 54 | + |
| 55 | + override def onStart(): Unit = { |
| 56 | + new Thread() { |
| 57 | + override def run(): Unit = { |
| 58 | + receive() |
| 59 | + } |
| 60 | + }.start() |
| 61 | + logger.info("Random receiver started.") |
| 62 | + } |
| 63 | + |
| 64 | + override def onStop(): Unit = { |
| 65 | + logger.info("Random receiver stopped.") |
| 66 | + } |
| 67 | + |
| 68 | + private def receive(): Unit = { |
| 69 | + nextIntervalStart = System.currentTimeMillis() |
| 70 | + while (!isStopped) { |
| 71 | + val timeNow = System.currentTimeMillis() |
| 72 | + // Only emit if we are still in the interval and haven't gone over our per period max |
| 73 | + if (timeNow <= nextIntervalStart && generatedThisPeriod < maxPerPeriod) { |
| 74 | + store(generateRecord()) |
| 75 | + generatedThisPeriod += 1 |
| 76 | + } |
| 77 | + if (timeNow > nextIntervalStart) { |
| 78 | + logger.info("Generated {} tuples out of {}", generatedThisPeriod, maxPerPeriod) |
| 79 | + nextIntervalStart = timeNow + period |
| 80 | + generatedThisPeriod = 0 |
| 81 | + periodCount += 1 |
| 82 | + } |
| 83 | + // It is courteous to sleep for a short time. |
| 84 | + try { |
| 85 | + Thread.sleep(1) |
| 86 | + } catch { |
| 87 | + case e: InterruptedException => logger.error("Error: ", e) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private def generateRecord(): BulletRecord = { |
| 93 | + val record = new BulletRecord() |
| 94 | + val uuid = UUID.randomUUID().toString |
| 95 | + record.setString(RandomReceiver.STRING, uuid) |
| 96 | + record.setLong(RandomReceiver.LONG, generatedThisPeriod) |
| 97 | + record.setDouble(RandomReceiver.DOUBLE, Random.nextDouble()) |
| 98 | + record.setString(RandomReceiver.TYPE, RandomReceiver.STRING_POOL(Random.nextInt(RandomReceiver.STRING_POOL.length))) |
| 99 | + record.setLong(RandomReceiver.DURATION, System.nanoTime() % RandomReceiver.INTEGER_POOL(Random.nextInt(RandomReceiver.INTEGER_POOL.length))) |
| 100 | + val booleanMap = Map[java.lang.String, java.lang.Boolean]( |
| 101 | + uuid.substring(0, 8) -> Random.nextBoolean(), |
| 102 | + uuid.substring(9, 13) -> Random.nextBoolean(), |
| 103 | + uuid.substring(14, 18) -> Random.nextBoolean(), |
| 104 | + uuid.substring(19, 23) -> Random.nextBoolean() |
| 105 | + ) |
| 106 | + record.setBooleanMap(RandomReceiver.BOOLEAN_MAP, booleanMap.asJava) |
| 107 | + val statsMap = Map[java.lang.String, java.lang.Long]( |
| 108 | + RandomReceiver.PERIOD_COUNT -> periodCount, |
| 109 | + RandomReceiver.RECORD_NUMBER -> (periodCount * maxPerPeriod + generatedThisPeriod), |
| 110 | + RandomReceiver.NANO_TIME -> System.nanoTime(), |
| 111 | + RandomReceiver.TIMESTAMP -> System.nanoTime() |
| 112 | + ) |
| 113 | + record.setLongMap(RandomReceiver.STATS_MAP, statsMap.asJava) |
| 114 | + val randomMapA = Map[java.lang.String, java.lang.String]( |
| 115 | + RandomReceiver.RANDOM_MAP_KEY_A -> RandomReceiver.STRING_POOL(Random.nextInt(RandomReceiver.STRING_POOL.length)), |
| 116 | + RandomReceiver.RANDOM_MAP_KEY_B -> RandomReceiver.STRING_POOL(Random.nextInt(RandomReceiver.STRING_POOL.length)) |
| 117 | + ) |
| 118 | + val randomMapB = Map[java.lang.String, java.lang.String]( |
| 119 | + RandomReceiver.RANDOM_MAP_KEY_A -> RandomReceiver.STRING_POOL(Random.nextInt(RandomReceiver.STRING_POOL.length)), |
| 120 | + RandomReceiver.RANDOM_MAP_KEY_B -> RandomReceiver.STRING_POOL(Random.nextInt(RandomReceiver.STRING_POOL.length)) |
| 121 | + ) |
| 122 | + record.setListOfStringMap(RandomReceiver.LIST, List(randomMapA.asJava, randomMapB.asJava).asJava) |
| 123 | + record |
| 124 | + } |
| 125 | +} |
| 126 | + |
0 commit comments