从 DB 到 MAP 数据结构的表格数据

Tabular data from DB to MAP Data structure

我正在从 DB 中获取一些值,并希望以此创建一个嵌套的地图数据结构。表格数据如下所示

+---------+--------------+----------------+------------------+----------------+-----------------------+
| Cube_ID | Dimension_ID | Dimension_Name | Partition_Column | Display_name   | Dimension_Description |
+---------+--------------+----------------+------------------+----------------+-----------------------+
|       1 |            1 | Reporting_Date | Y                | Reporting_Date | Reporting_Date        |
|       1 |            2 | Platform       | N                | Platform       | Platform              |
|       1 |            3 | Country        | N                | Country        | Country               |
|       1 |            4 | OS_Version     | N                | OS_Version     | OS_Version            |
|       1 |            5 | Device_Version | N                | Device_Version | Device_Version        |
+---------+--------------+----------------+------------------+----------------+-----------------------+

我想创建一个类似这样的嵌套结构

{
    CubeID = "1": {
        Dimension ID = "1": [
            {
                "Name": "Reporting_Date",
                "Partition_Column": "Y"
                "Display": "Reporting_Date"
            }
        ]
        Dimension ID = "2": [
            {
                "Name": "Platform",
                "Column": "N"
                "Display": "Platform"
            }
        ]
    },
    CubeID = "2": {
        Dimension ID = "1": [
            {
                "Name": "Reporting_Date",
                "Partition_Column": "Y"
                "Display": "Reporting_Date"
            }
        ]
        Dimension ID = "2": [
            {
                "Name": "Platform",
                "Column": "N"
                "Display": "Platform"
            }
        ]
    }
}        

我使用以下方法从数据库中获得了结果集。我可以填充单独的列,但不确定如何为以后的计算创建地图

while (rs.next()) {
      val Dimension_ID = rs.getInt("Dimension_ID")
      val Dimension_Name = rs.getString("Dimension_Name")
      val Partition_Column = rs.getString("Partition_Column")
      val Display_name = rs.getString("Display_name")
      val Dimension_Description = rs.getString("Dimension_Description")
}

我想我应该为此写一个案例 class,但我不确定如何创建案例 class 并将值加载到案例 class.

感谢您的帮助。我可以提供任何其他需要的信息。让我知道

背景

你可以定义数据 class 如下,

case class Dimension(
    dimensionId: Long,
    name: String,
    partitionColumn: String,
    display: String
)

case class Record(
    cubeId: Int,
    dimension: Dimension
)

case class Data(records: List[Record])

这就是构建数据的方式,

val data =
  Data(
    List(
      Record(
        cubeId = 1,
        dimension = Dimension(
          dimensionId = 1,
          name = "Reporting_Date",
          partitionColumn = "Y",
          display = "Reporting_Date"
        )
      ),
      Record(
        cubeId = 2,
        dimension = Dimension(
          dimensionId = 1,
          name = "Platform",
          partitionColumn = "N",
          display = "Platform"
        )
      )
    )
  )

现在回答你的问题,因为你正在使用 JDBC,你必须以可变方式构建记录列表或使用 scala Iterator。我将在下面写出构建上述数据的可变方式class,但您可以探索更多。

import scala.collection.mutable.ListBuffer
var mutableData = new ListBuffer[Record]()

while (rs.next()) {
  mutableData += Record(
    cubeId = rs.getIn("Cube_ID"),
    dimension = Dimension(
      dimensionId = rs.getInt("Dimension_ID"),
      name = rs.getString("Dimension_Name"),
      partitionColumn = rs.getString("Partition_Column"),
      display = rs.getString("Dimension_Description")
    )
  )
}

val data = Data(records = mutableData.toList)

另请阅读 - 将 SQL ResultSet 转换为 Scala 列表的更好方法