Spark2 mongodb 连接器多态模式

Spark2 mongodb connector polymorphic schema

我有 collection col 包含

{
   '_id': ObjectId(...)
   'type': "a"
   'f1': data1
}

在同一个 collection 我有

{
   '_id': ObjectId(...)
   'f2': 222.234
   'type': "b"
}   

Spark MongoDB connector 工作不正常。它在错误的字段中重新排序数据

例如:

{
   '_id': ObjectId(...)
   'type': "a"
   'f1': data1
}


{
   '_id': ObjectId(...)
   'f1': data2
   'type': "a"
}

Rdd 将是:

------------------------
|  id  |  f1   | type  |
------------------------
| .... |  a    | data1 |
| .... | data2 | a     |
------------------------

是否有任何使用多态模式的建议

Is there any suggestions working with polymorphic schema

(意见提醒)最好的建议是一开始就不要。无法长期维护,极易出错,客户端补偿复杂

如果你有一个怎么办:

  • 例如,您可以尝试使用文档的 Aggregation Framework with $project to sanitize data before it is fetched to Spark. See Aggregation section
  • 不要试图将它与结构化格式结合起来。使用 RDDs,以普通方式获取数据 Python dict 并手动处理问题。