1
+ /**
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ *
19
+ * Copyright © 2019 AudienceProject. All rights reserved.
20
+ */
1
21
package com .audienceproject .spark .dynamodb .catalyst
2
22
3
23
import java .util
@@ -11,44 +31,44 @@ import scala.collection.JavaConverters._
11
31
12
32
object JavaConverter {
13
33
14
- def extractRowValue (row : InternalRow , index : Int , elementType : DataType ): Any = {
34
+ def convertRowValue (row : InternalRow , index : Int , elementType : DataType ): Any = {
15
35
elementType match {
16
- case ArrayType (innerType, _) => extractArray (row.getArray(index), innerType)
17
- case MapType (keyType, valueType, _) => extractMap (row.getMap(index), keyType, valueType)
18
- case StructType (fields) => extractStruct (row.getStruct(index, fields.length), fields)
36
+ case ArrayType (innerType, _) => convertArray (row.getArray(index), innerType)
37
+ case MapType (keyType, valueType, _) => convertMap (row.getMap(index), keyType, valueType)
38
+ case StructType (fields) => convertStruct (row.getStruct(index, fields.length), fields)
19
39
case StringType => row.getString(index)
20
40
case _ => row.get(index, elementType)
21
41
}
22
42
}
23
43
24
- def extractArray (array : ArrayData , elementType : DataType ): Any = {
44
+ def convertArray (array : ArrayData , elementType : DataType ): Any = {
25
45
elementType match {
26
- case ArrayType (innerType, _) => array.toSeq[ArrayData ](elementType).map(extractArray (_, innerType)).asJava
27
- case MapType (keyType, valueType, _) => array.toSeq[MapData ](elementType).map(extractMap (_, keyType, valueType)).asJava
28
- case structType : StructType => array.toSeq[InternalRow ](structType).map(extractStruct (_, structType.fields)).asJava
46
+ case ArrayType (innerType, _) => array.toSeq[ArrayData ](elementType).map(convertArray (_, innerType)).asJava
47
+ case MapType (keyType, valueType, _) => array.toSeq[MapData ](elementType).map(convertMap (_, keyType, valueType)).asJava
48
+ case structType : StructType => array.toSeq[InternalRow ](structType).map(convertStruct (_, structType.fields)).asJava
29
49
case StringType => convertStringArray(array).asJava
30
50
case _ => array.toSeq[Any ](elementType).asJava
31
51
}
32
52
}
33
53
34
- def extractMap (map : MapData , keyType : DataType , valueType : DataType ): util.Map [String , Any ] = {
54
+ def convertMap (map : MapData , keyType : DataType , valueType : DataType ): util.Map [String , Any ] = {
35
55
if (keyType != StringType ) throw new IllegalArgumentException (
36
56
s " Invalid Map key type ' ${keyType.typeName}'. DynamoDB only supports String as Map key type. " )
37
57
val keys = convertStringArray(map.keyArray())
38
58
val values = valueType match {
39
- case ArrayType (innerType, _) => map.valueArray().toSeq[ArrayData ](valueType).map(extractArray (_, innerType))
40
- case MapType (innerKeyType, innerValueType, _) => map.valueArray().toSeq[MapData ](valueType).map(extractMap (_, innerKeyType, innerValueType))
41
- case structType : StructType => map.valueArray().toSeq[InternalRow ](structType).map(extractStruct (_, structType.fields))
59
+ case ArrayType (innerType, _) => map.valueArray().toSeq[ArrayData ](valueType).map(convertArray (_, innerType))
60
+ case MapType (innerKeyType, innerValueType, _) => map.valueArray().toSeq[MapData ](valueType).map(convertMap (_, innerKeyType, innerValueType))
61
+ case structType : StructType => map.valueArray().toSeq[InternalRow ](structType).map(convertStruct (_, structType.fields))
42
62
case StringType => convertStringArray(map.valueArray())
43
63
case _ => map.valueArray().toSeq[Any ](valueType)
44
64
}
45
65
val kvPairs = for (i <- 0 until map.numElements()) yield keys(i) -> values(i)
46
66
Map (kvPairs : _* ).asJava
47
67
}
48
68
49
- def extractStruct (row : InternalRow , fields : Seq [StructField ]): util.Map [String , Any ] = {
69
+ def convertStruct (row : InternalRow , fields : Seq [StructField ]): util.Map [String , Any ] = {
50
70
val kvPairs = for (i <- 0 until row.numFields)
51
- yield fields(i).name -> extractRowValue (row, i, fields(i).dataType)
71
+ yield fields(i).name -> convertRowValue (row, i, fields(i).dataType)
52
72
Map (kvPairs : _* ).asJava
53
73
}
54
74
0 commit comments