|
16 | 16 |
|
17 | 17 | package com._4paradigm.openmldb.batch.utils
|
18 | 18 |
|
19 |
| -import com._4paradigm.hybridse.sdk.{HybridSeException, UnsupportedHybridSeException} |
| 19 | +import com._4paradigm.hybridse.sdk.HybridSeException |
20 | 20 | import com._4paradigm.openmldb.proto.Type
|
21 | 21 | import org.apache.spark.sql.Row
|
22 |
| -import org.apache.spark.sql.types.{ |
23 |
| - BooleanType, DataType, DateType, DoubleType, FloatType, IntegerType, LongType, |
24 |
| - ShortType, StringType, TimestampType |
25 |
| -} |
| 22 | +import org.apache.spark.sql.types.{BooleanType, DataType, DateType, DoubleType, FloatType, IntegerType, LongType, |
| 23 | + ShortType, StringType, StructType, TimestampType} |
26 | 24 |
|
27 | 25 | object SparkRowUtil {
|
28 | 26 |
|
29 |
| - def getLongFromIndex(keyIdx: Int, sparkType: DataType, row: Row): java.lang.Long = { |
| 27 | + def rowToString(schema: StructType, row: Row): String = { |
| 28 | + val rowStr = new StringBuilder("Spark row: ") |
| 29 | + (0 until schema.size).foreach(i => { |
| 30 | + if (i == 0) { |
| 31 | + rowStr ++= s"${schema(i).dataType}: ${getColumnStringValue(schema, row, i)}" |
| 32 | + } else { |
| 33 | + rowStr ++= s", ${schema(i).dataType}: ${getColumnStringValue(schema, row, i)}" |
| 34 | + } |
| 35 | + }) |
| 36 | + rowStr.toString() |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Get the string value of the specified column. |
| 41 | + * |
| 42 | + * @param row |
| 43 | + * @param index |
| 44 | + * @return |
| 45 | + */ |
| 46 | + def getColumnStringValue(schema: StructType, row: Row, index: Int): String = { |
| 47 | + if (row.isNullAt(index)) { |
| 48 | + "null" |
| 49 | + } else { |
| 50 | + val colType = schema(index).dataType |
| 51 | + colType match { |
| 52 | + case BooleanType => String.valueOf(row.getBoolean(index)) |
| 53 | + case ShortType => String.valueOf(row.getShort(index)) |
| 54 | + case DoubleType => String.valueOf(row.getDouble(index)) |
| 55 | + case IntegerType => String.valueOf(row.getInt(index)) |
| 56 | + case LongType => String.valueOf(row.getLong(index)) |
| 57 | + case TimestampType => String.valueOf(row.getTimestamp(index)) |
| 58 | + case DateType => String.valueOf(row.getDate(index)) |
| 59 | + case StringType => row.getString(index) |
| 60 | + case _ => |
| 61 | + throw new HybridSeException(s"Unsupported data type: $colType") |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + def getLongFromIndex(keyIdx: Int, colType: DataType, row: Row): java.lang.Long = { |
30 | 67 | if (row.isNullAt(keyIdx)) {
|
31 | 68 | null
|
32 | 69 | } else {
|
33 |
| - sparkType match { |
| 70 | + colType match { |
34 | 71 | case ShortType => row.getShort(keyIdx).toLong
|
35 | 72 | case IntegerType => row.getInt(keyIdx).toLong
|
36 | 73 | case LongType => row.getLong(keyIdx)
|
37 | 74 | case TimestampType => row.getTimestamp(keyIdx).getTime
|
38 | 75 | case DateType => row.getDate(keyIdx).getTime
|
39 | 76 | case _ =>
|
40 |
| - throw new HybridSeException(s"Illegal window key type: $sparkType") |
| 77 | + throw new HybridSeException(s"Illegal window key type: $colType") |
41 | 78 | }
|
42 | 79 | }
|
43 | 80 | }
|
|
0 commit comments