我通过创建Map将我的数据写入Spark中的Kibana。我的数据类型为List[Seq[String]]
。但是,我不仅仅将键映射到值,而是将键和多个值分配给它们。因此,最终不是一个对应于键和多个记录的值,而是获得与键和多个记录对应的多个值,因为这个过程是重复的。所以数据看起来像这样:
event_name:"Sketch-Up at Field Museum (DRY MEDIA ONLY)", "Watercolor Basics - Part 2!", "Watercolor Basics (Intro Class)", "Jan: How about a FREE day trip to Mexico? Did I say FREE?", "Dia de Los muertos Day of the dead"
venue_name:"The Field Museum", "filmfront", "filmfront", "National Museum of Mexican Art", "Dvorak Park (Pilsen)"
distance:3116.147997575436, 3896.341529919854, 3896.341529919854, 3974.197798056245, 2615.3793888968457
虽然我想为一条记录提供类似的内容:
event_name:"Sketch-Up at Field Museum (DRY MEDIA ONLY)"
venue_name:"The Field Museum"
distance:3116.147997575436
以下是我创建地图的代码:
val resultsMap = Map(
"group_name" -> resultsList.map(sublist => sublist(0)),
"event_name" -> resultsList.map(sublist => sublist(1)),
"venue_name" -> resultsList.map(sublist => sublist(2)),
"distance" -> resultsList.map(sublist => sublist(3))
)
resultsList: List[Seq[String]]
将列表转换为地图时我做错了什么?
答案 0 :(得分:2)
请注意,您并不完全清楚您期望的结果类型,以及实际输入的内容,因此我必须做出一些假设。请在下次提问时更具体。
目前您创建了一个Map
并将序列的每个元素作为列表放在同一个键下:
val resultsMap = Map(...)
换句话说,所有序列元素都是通过它们在单个键下的位置进行聚合的。
你提到过你希望它们是分开的,所以我假设你想要一个List
(或Seq
)条记录,每条记录都是Map
。因此,您无法获得单个地图,因为无法在地图中复制密钥。
您可以通过将您的序列列表映射到地图列表来轻松获得此类结果:
val resultsList = List(
Seq("A", "Sketch-Up at Field Museum (DRY MEDIA ONLY)", "The Field Museum", "3116.147997575436"),
Seq("A", "Watercolor Basics - Part 2!", "filmfront", "3896.341529919854"),
Seq("B", "Watercolor Basics (Intro Class)", "filmfront", "3896.341529919854"),
Seq("B", "Jan: How about a FREE day trip to Mexico? Did I say FREE?", "National Museum of Mexican Art", "3974.197798056245"),
Seq("A", "Dia de Los muertos Day of the dead", "Dvorak Park (Pilsen)", "2615.3793888968457")
)
val finalResult = resultsList map { s => Map(
"group_name" -> s(0),
"event_name" -> s(1),
"venue_name" -> s(2),
"distance" -> s(3)
)}
结果:
List(
Map(group_name -> A, event_name -> Sketch-Up at Field Museum (DRY MEDIA ONLY), venue_name -> The Field Museum, distance -> 3116.147997575436),
Map(group_name -> A, event_name -> Watercolor Basics - Part 2!, venue_name -> filmfront, distance -> 3896.341529919854),
Map(group_name -> B, event_name -> Watercolor Basics (Intro Class), venue_name -> filmfront, distance -> 3896.341529919854),
Map(group_name -> B, event_name -> Jan: How about a FREE day trip to Mexico? Did I say FREE?, venue_name -> National Museum of Mexican Art, distance -> 3974.197798056245),
Map(group_name -> A, event_name -> Dia de Los muertos Day of the dead, venue_name -> Dvorak Park (Pilsen), distance -> 2615.3793888968457)
)