|
6 | 6 | package com.yahoo.bullet.spark.examples.receiver |
7 | 7 |
|
8 | 8 | import java.util.UUID |
| 9 | +import java.util.HashMap |
| 10 | +import java.util.Arrays.asList |
9 | 11 |
|
10 | | -import scala.collection.JavaConverters._ |
11 | 12 | import scala.util.Random |
12 | 13 |
|
13 | | -import com.yahoo.bullet.record.BulletRecord |
| 14 | +import com.yahoo.bullet.record.{BulletRecord, SimpleBulletRecord} |
14 | 15 | import com.yahoo.bullet.spark.utils.{BulletSparkConfig, BulletSparkLogger} |
15 | 16 | import org.apache.spark.storage.StorageLevel |
16 | 17 | import org.apache.spark.streaming.receiver.Receiver |
@@ -90,36 +91,37 @@ class RandomReceiver(val config: BulletSparkConfig) |
90 | 91 | } |
91 | 92 |
|
92 | 93 | private def generateRecord(): BulletRecord = { |
93 | | - val record = new BulletRecord() |
| 94 | + val record = new SimpleBulletRecord() |
94 | 95 | val uuid = UUID.randomUUID().toString |
95 | 96 | record.setString(RandomReceiver.STRING, uuid) |
96 | 97 | record.setLong(RandomReceiver.LONG, generatedThisPeriod) |
97 | 98 | record.setDouble(RandomReceiver.DOUBLE, Random.nextDouble()) |
98 | 99 | record.setString(RandomReceiver.TYPE, RandomReceiver.STRING_POOL(Random.nextInt(RandomReceiver.STRING_POOL.length))) |
99 | 100 | 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) |
| 101 | + |
| 102 | + // Don't use Scala Map and convert it by asJava when calling setxxxMap method in BulletRecord. |
| 103 | + // It converts Scala Map to scala.collection.convert.Wrappers$MapWrapper which is not serializable in scala 2.11.x (https://issues.scala-lang.org/browse/SI-8911). |
| 104 | + val booleanMap = new HashMap[java.lang.String, java.lang.Boolean](4) |
| 105 | + booleanMap.put(uuid.substring(0, 8), Random.nextBoolean()) |
| 106 | + booleanMap.put(uuid.substring(9, 13), Random.nextBoolean()) |
| 107 | + booleanMap.put(uuid.substring(14, 18), Random.nextBoolean()) |
| 108 | + booleanMap.put(uuid.substring(19, 23), Random.nextBoolean()) |
| 109 | + record.setBooleanMap(RandomReceiver.BOOLEAN_MAP, booleanMap) |
| 110 | + |
| 111 | + val statsMap = new HashMap[java.lang.String, java.lang.Long](4) |
| 112 | + statsMap.put(RandomReceiver.PERIOD_COUNT, periodCount) |
| 113 | + statsMap.put(RandomReceiver.RECORD_NUMBER, periodCount * maxPerPeriod + generatedThisPeriod) |
| 114 | + statsMap.put(RandomReceiver.NANO_TIME, System.nanoTime()) |
| 115 | + statsMap.put(RandomReceiver.TIMESTAMP, System.nanoTime()) |
| 116 | + record.setLongMap(RandomReceiver.STATS_MAP, statsMap) |
| 117 | + |
| 118 | + val randomMapA = new HashMap[java.lang.String, java.lang.String](2) |
| 119 | + randomMapA.put(RandomReceiver.RANDOM_MAP_KEY_A, RandomReceiver.STRING_POOL(Random.nextInt(RandomReceiver.STRING_POOL.length))) |
| 120 | + randomMapA.put(RandomReceiver.RANDOM_MAP_KEY_B, RandomReceiver.STRING_POOL(Random.nextInt(RandomReceiver.STRING_POOL.length))) |
| 121 | + val randomMapB = new HashMap[java.lang.String, java.lang.String](2) |
| 122 | + randomMapB.put(RandomReceiver.RANDOM_MAP_KEY_A, RandomReceiver.STRING_POOL(Random.nextInt(RandomReceiver.STRING_POOL.length))) |
| 123 | + randomMapB.put(RandomReceiver.RANDOM_MAP_KEY_B, RandomReceiver.STRING_POOL(Random.nextInt(RandomReceiver.STRING_POOL.length))) |
| 124 | + record.setListOfStringMap(RandomReceiver.LIST, asList(randomMapA, randomMapB)) |
123 | 125 | record |
124 | 126 | } |
125 | 127 | } |
|
0 commit comments