2020
2121import com .google .common .base .Preconditions ;
2222import com .google .common .collect .Lists ;
23+ import org .apache .commons .lang3 .StringUtils ;
2324import org .apache .flink .api .common .typeinfo .TypeInformation ;
2425import org .apache .flink .api .java .tuple .Tuple2 ;
2526import org .apache .flink .api .java .typeutils .RowTypeInfo ;
3031import java .util .regex .Matcher ;
3132import java .util .regex .Pattern ;
3233
34+ import static org .apache .commons .lang3 .StringUtils .split ;
35+
3336/**
3437 * @program: flink.sql
3538 * @author: wuren
@@ -39,28 +42,13 @@ public class DataTypeUtils {
3942
4043 private final static Pattern COMPOSITE_TYPE_PATTERN = Pattern .compile ("(.+?)<(.+)>" );
4144 private final static String ARRAY = "ARRAY" ;
45+ private final static String MAP = "MAP" ;
4246 private final static String ROW = "ROW" ;
4347 private final static char FIELD_DELIMITER = ',' ;
4448 private final static char TYPE_DELIMITER = ' ' ;
4549
4650 private DataTypeUtils () {}
4751
48- /**
49- * 现在只支持ARRAY类型后续可以加入 MAP等类型
50- * @param compositeTypeString
51- * @return
52- */
53- public static TypeInformation convertToCompositeType (String compositeTypeString ) {
54- Matcher matcher = matchCompositeType (compositeTypeString );
55- final String errorMsg = "type " + compositeTypeString + "is not support!" ;
56- Preconditions .checkState (matcher .find (), errorMsg );
57-
58- String normalizedType = normalizeType (matcher .group (1 ));
59- Preconditions .checkState (ARRAY .equals (normalizedType ), errorMsg );
60-
61- return convertToArray (compositeTypeString );
62- }
63-
6452 /**
6553 * 目前ARRAY里只支持ROW和其他基本类型
6654 * @param arrayTypeString
@@ -86,6 +74,30 @@ public static TypeInformation convertToArray(String arrayTypeString) {
8674 return Types .OBJECT_ARRAY (elementType );
8775 }
8876
77+ /**
78+ * 目前Map里只支持基本类型
79+ * @param mapTypeString
80+ * @return
81+ */
82+ public static TypeInformation convertToMap (String mapTypeString ) {
83+ Matcher matcher = matchCompositeType (mapTypeString );
84+ final String errorMsg = mapTypeString + "convert to map type error!" ;
85+ Preconditions .checkState (matcher .find (), errorMsg );
86+
87+ String normalizedType = normalizeType (matcher .group (1 ));
88+ Preconditions .checkState (MAP .equals (normalizedType ), errorMsg );
89+
90+ String kvTypeString = matcher .group (2 );
91+ String [] kvTypeStringList = StringUtils .split (kvTypeString , "," );
92+ final String mapTypeErrorMsg = "There can only be key and value two types in map declaration." ;
93+ Preconditions .checkState (kvTypeStringList .length == 2 , mapTypeErrorMsg );
94+ String keyTypeString = normalizeType (kvTypeStringList [0 ]);
95+ String valueTypeString = normalizeType (kvTypeStringList [1 ]);
96+ TypeInformation keyType = convertToAtomicType (keyTypeString );
97+ TypeInformation valueType = convertToAtomicType (valueTypeString );
98+ return Types .MAP (keyType , valueType );
99+ }
100+
89101 /**
90102 * 目前ROW里只支持基本类型
91103 * @param rowTypeString
@@ -104,6 +116,7 @@ public static RowTypeInfo convertToRow(String rowTypeString) {
104116 return new RowTypeInfo (info .f0 , info .f1 );
105117 }
106118
119+
107120 private static Tuple2 <TypeInformation [], String []> genFieldInfo (Iterable <String > fieldInfoStrs ) {
108121 ArrayList <TypeInformation > types = Lists .newArrayList ();
109122 ArrayList <String > fieldNames = Lists .newArrayList ();
0 commit comments