Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

java - StringType issue: Exception in thread "main" scala.MatchError: org.apache.spark.sql.types.StringType@

@Override
public Option<DataType> getCatalystType(int sqlType, String typeName, int size, MetadataBuilder md) {
    switch (sqlType) {
    case java.sql.Types.JAVA_OBJECT:
        switch (typeName) {
        case "map(varchar(2147483647),varchar(2147483647))":
            return Option.apply(DataTypes.createMapType(new StringType(), new StringType()));               
        }
        break;
    }
    return super.getCatalystType(sqlType, typeName, size, md);      
}

Code is used to support complex data type when return JAVA_OBJECT. This same code, I have written in Scala which is working fine. But when use the above code in Java, It is giving:

Exception in thread "main" scala.MatchError: org.apache.spark.sql.types.StringType@582b14e2 (of class org.apache.spark.sql.types.StringType).


Scala code for reference:

override def getCatalystType(sqlType: Int, typeName: String, size: Int, md: MetadataBuilder): Option[DataType] = sqlType match {
    case java.sql.Types.JAVA_OBJECT =>
      typeName match {
        case "map(varchar(2147483647),varchar(2147483647))" => Option(DataTypes.createMapType(StringType, StringType))
        case "BIT" => Option(BooleanType)
        case _ => None
      }
    case _ => None
  }
question from:https://stackoverflow.com/questions/65931030/stringtype-issue-exception-in-thread-main-scala-matcherror-org-apache-spark

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the singleton DataTypes.StringType instead (as recommended):

...
...(DataTypes.createMapType(DataTypes.StringType, DataTypes.StringType))          

The data type representing String values. Please use the singleton DataTypes.StringType.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...