【问题标题】:Tabular data from DB to MAP Data structure从 DB 到 MAP 的表格数据 数据结构
【发布时间】:2019-12-06 08:25:32
【问题描述】:

我正在从 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"
            }
        ]
    }
}        

我使用以下方法从 DB 获得结果集。我能够填充各个列,但不确定如何创建地图以供以后计算

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")
}

我认为我应该为此编写一个案例类,但我不确定如何创建案例类并将值加载到案例类中。

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

【问题讨论】:

  • 你上次sn-p上所有变量的类型是什么?你是什​​么意思你不确定如何创建案例类? - PS:看起来您使用的是普通的 JDBC,您是否看过像 DoobieSlick 这样的 scala 框架?
  • 感谢您的回复。表格中的大多数变量都是字符串。我不确定哪种数据结构最适合。我不喜欢使用列表,因为列将是未命名的。我还没有尝试过这些框架。
  • 如果你想要命名事物,最好的选择是嵌套案例类。将 List 用于分组值。

标签: scala case-class


【解决方案1】:

背景

你可以定义如下的数据类,

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。我将在下面编写可变方式来构造上述数据类,但您可以探索更多。

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)

另请阅读 - Any better way to convert SQL ResultSet to Scala List

【讨论】:

  • 感谢您的回复。如果有任何疑问,我会试试这个并回复你
  • 谢谢。我可以用这个。但是,我现在卡住了,有一个尴尬的问题。我可以创建记录组,但不确定如何构建数据组。
  • 我收到如下错误:类型不匹配:预期数据实际 ListBuffer[Record]
  • 我做了一些改变,它正在工作。案例类 Data(records: ListBuffer[Record]) 和 val data: DimensionData= DimensionData(mutableData)。如果您在此处发现任何问题,请告诉我
  • 啊,那是因为你必须将mutable.ListBuffer 转换为List,所以如果你只是做mutableData.toList 应该可以工作。首选不可变而不是可变。所以你的代码看起来像val data: DimensionData= DimensionData(mutableData.toList)
猜你喜欢
  • 2018-07-31
  • 2016-01-31
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
  • 1970-01-01
相关资源
最近更新 更多