Leap Frame Operations

The basic operations of a leap frame are:

  1. Creating new columns and inserting data
  2. Dropping columns
  3. Reading row data
  4. Selecting a set of columns into a new leap frame

Insert

Generating new values from existing fields is a simple task. Justspecify the name of the output field, a list of input fields, and aScala function to generate the new output value.

  1. // Insert some values into the leap frame
  2. val leapFrame2 = leapFrame.withOutput("generated_field", "a_string", "an_int") {
  3. (str: String, i: Int) => s"$str: $i"
  4. }
  5. // Extract our new data from the leap frame
  6. val generatedStrings: Seq[String] = (for(lf <- leapFrame2;
  7. lf2 <- lf.select("generated_field", "a_string", "an_int")) yield {
  8. val str = lf2.dataset(0).getString(1) // get value of "a_string"
  9. val i = lf2.dataset(0).getInt(2) // get value of "an_int"
  10. assert(lf2.dataset(0).getString(0) == s"$str: $i")
  11. lf2.dataset.map(_.getString(0))
  12. }).get.toSeq
  13. // Print out our generated strings
  14. // > "Hello, MLeap!: 42"
  15. // > "Another row: 43"
  16. println(generatedStrings.mkString("\n"))

Insert Optional Value

Null values in MLeap are supported with the Scala Option monad. Let’soutput some optionally null values in our leap frame.

  1. // Insert some values into the leap frame
  2. val leapFrame3 = leapFrame.withOutput("optional_int", "a_double", "an_int") {
  3. (d: Double, i: Int) =>
  4. if(i > 42) {
  5. Some(777)
  6. } else { None }
  7. }
  8. // Extract our new data from the leap frame
  9. val optionalInts: Seq[Option[Int]] = (for(lf <- leapFrame3;
  10. lf2 <- lf.select("optional_int")) yield {
  11. lf2.dataset.map(_.optionInt(0))
  12. }).get.toSeq
  13. // Print out our optional ints
  14. // > Some(777)
  15. // > None
  16. println(optionalInts.mkString("\n"))

Drop

Drop a field from the leap frame.

  1. assert(leapFrame.schema.hasField("a_double"))
  2. for(lf <- leapFrame.dropField("a_double")) {
  3. assert(!lf.schema.hasField("a_double"))
  4. }

Read

Gain access to the rows in the leap frame.

  1. val data = leapFrame.dataset
  2. assert(data.head == Row("Hello, MLeap!", 56.7d, 13.0f, 42, 67l))
  3. assert(data(1) == Row("Another row", 23.4d, 11.0f, 43, 88l))
  4. // Datasets are iterable over their rows
  5. assert(data.toSeq.size == 2)

Select

Construct a new leap frame by selecting fields.

  1. assert(leapFrame.schema.hasField("a_double"))
  2. assert(leapFrame.schema.hasField("a_string"))
  3. assert(leapFrame.schema.hasField("an_int"))
  4. assert(leapFrame.schema.fields.size == 5)
  5. for(lf <- leapFrame.select("a_double", "a_string")) {
  6. assert(lf.schema.hasField("a_double"))
  7. assert(lf.schema.hasField("a_string"))
  8. assert(!lf.schema.hasField("an_int"))
  9. assert(lf.schema.fields.size == 2)
  10. }