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

Internal API for User-Defined Types

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • 1.2.0
    • SQL
    • None

    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

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

              Dates

                Created:
                Updated:
                Resolved: