Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-3572

Internal API for User-Defined Types

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.2.0
    • Component/s: SQL
    • Labels:
      None
    • Target Version/s:

      Description

      If a user knows how to map a class to a struct type in Spark SQL, he should be able to register this mapping through sqlContext and hence SQL can figure out the schema automatically.

      trait RowSerializer[T] {
        def dataType: StructType
        def serialize(obj: T): Row
        def deserialize(row: Row): T
      }
      
      sqlContext.registerUserType[T](clazz: classOf[T], serializer: classOf[RowSerializer[T]])
      

      In sqlContext, we can maintain a class-to-serializer map and use it for conversion. The serializer class can be embedded into the metadata, so when `select` is called, we know we want to deserialize the result.

      sqlContext.registerUserType(classOf[Vector], classOf[VectorRowSerializer])
      val points: RDD[LabeledPoint] = ...
      val features: RDD[Vector] = points.select('features).map { case Row(v: Vector) => v }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                josephkb Joseph K. Bradley
                Reporter:
                mengxr Xiangrui Meng
                Shepherd:
                Michael Armbrust
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: