I'm a beginner in scala, I tried to producing event fruits sales in Scala and store the result in json file. For that I tried to generate random object (Fruits with the name and origin) but I have difficulty and I don't see how to do it with Random class.
Here is my code:
import scala.util.Random
import org.apache.spark.sql.SparkSession
val spark = SparkSession
.builder
.appName("FruitSales")
.getOrCreate()
object FruitSales extends Enumeration {
object Fruit extends Enumeration {
type Fruit= Value
val apple, banana , orange, strawberry = Value
}
object Origin extends Enumeration {
type Origin = Value
val USA, espagne, france, sweden, mexico= Value
}
class Fruits(Fruit: String, Origin: String) extends Enumeration {}
def producing_events (fruit: Fruits): Fruits = {
val rand = new scala.util.Random
rand.nextString(200)
}
val df = spark.read.json("/tmp/file.json")
df.show(false)
}
I would advise avoiding Enumeration on this occasion, no the best practice. Here is my approach, not perfect but will provide you with a general idea. Code below generating 10 random objects, if you really want, you can even customise probability distribution in ScalaCheck, but I would guess this is too much in your case.
import org.scalacheck._
final case class Fruit(name: String, origin: String)
val originGen = Gen.oneOf( List("apple", "banana", "orange", "strawberry") )
val fruitNameGen = Gen.oneOf( List("USA", "Espagne", "France", "Sweden", "Mexico") )
val fruits = {
for {
_ <- 0 to 10
origin <- originGen.sample.take(1)
fruit <- fruitNameGen.sample.take(1)
} yield Fruit(fruit, origin)
}
println( fruits )
You can also add some limitations and dependencies on object generations. Just dig out the documentation for more details.
https://github.com/typelevel/scalacheck/blob/master/doc/UserGuide.md
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.