• 创建 Leap Frame

    创建 Leap Frame

    让我们创建一帧 Leap Frame 来存储数据。

    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)

    对于预测来自例如 Web 服务器或者其他用户输入等数据源的数据来说,类似这种通过代码来创建 Leap Frames 的方法是非常有用的。此外,它还可以从文件中加载,或者把数据存储到文件中供后期使用。可以参见我们的序列化 Leap Frame (译者注:文档已被原作者删除)章节以了解更多的细节。