Creating a Leap Frame

Let’s create a leap frame to hold our data.

  1. import ml.combust.mleap.runtime._
  2. import ml.combust.mleap.core.types._
  3. import ml.combust.mleap.runtime.frame.{DefaultLeapFrame, Row}
  4. // Create a schema. Returned as a Try monad to ensure that there
  5. // Are no duplicate field names
  6. val schema: StructType = StructType(StructField("a_string", ScalarType.String),
  7. StructField("a_double", ScalarType.Double),
  8. StructField("a_float", ScalarType.Float),
  9. StructField("an_int", ScalarType.Int),
  10. StructField("a_long", ScalarType.Long)).get
  11. // Create a dataset to contain all of our values
  12. // This dataset has two rows
  13. val dataset = Seq(Row("Hello, MLeap!", 56.7d, 13.0f, 42, 67l),
  14. Row("Another row", 23.4d, 11.0f, 43, 88l))
  15. // Create a LeapFrame from the schema and dataset
  16. val leapFrame = DefaultLeapFrame(schema, dataset)
  17. // Make some assertions about the data in our leap frame
  18. assert(leapFrame.dataset(0).getString(0) == "Hello, MLeap!")
  19. assert(leapFrame.dataset(0).getDouble(1) == 56.7d)
  20. assert(leapFrame.dataset(1).getDouble(1) == 23.4d)

Programatically creating leap frames like this can be very useful forscoring data from, say, a web server or some other user input. It isalso useful to be able to load data from files and store data for lateruse. See our section on serializing leap frames for moreinformation.