|
51 | 51 | import java.util.Date; |
52 | 52 | import java.util.HashMap; |
53 | 53 | import java.util.List; |
| 54 | +import java.util.Locale; |
54 | 55 | import java.util.Map; |
55 | 56 | import java.util.TreeMap; |
| 57 | +import java.util.regex.Matcher; |
| 58 | +import java.util.regex.Pattern; |
56 | 59 |
|
57 | 60 | /** |
58 | 61 | * Date: 2021/06/22 Company: www.dtstack.com |
|
62 | 65 | public class HiveUtil { |
63 | 66 | public static final String TABLE_COLUMN_KEY = "key"; |
64 | 67 | public static final String TABLE_COLUMN_TYPE = "type"; |
| 68 | + public static final String DECIMAL_KEY = "DECIMAL"; |
| 69 | + public static final String DECIMAL_FORMAT = "DECIMAL(%s, %s)"; |
| 70 | + public static final String DECIMAL_PATTERN_STR = "DECIMAL(\\((\\s*\\d+\\s*),(\\s*\\d+\\s*)\\))"; |
| 71 | + public static final Pattern DECIMAL_PATTERN = Pattern.compile(DECIMAL_PATTERN_STR); |
65 | 72 | public static final String PARTITION_TEMPLATE = "%s=%s"; |
66 | 73 | private static final Logger logger = LoggerFactory.getLogger(HiveUtil.class); |
67 | 74 | private static final String CREATE_PARTITION_TEMPLATE = |
@@ -168,8 +175,7 @@ private static void fillTableInfo(Connection connection, TableInfo tableInfo) { |
168 | 175 | metadataParser.fillTableInfo(tableInfo, result); |
169 | 176 | } catch (Exception e) { |
170 | 177 | if (e.getMessage().contains(NO_SUCH_TABLE_EXCEPTION)) { |
171 | | - throw new ChunJunRuntimeException( |
172 | | - String.format("表%s不存在", tableInfo.getTablePath())); |
| 178 | + throw new ChunJunRuntimeException(String.format("表%s不存在", tableInfo.getTablePath())); |
173 | 179 | } else { |
174 | 180 | throw e; |
175 | 181 | } |
@@ -360,11 +366,34 @@ private static String convertType(String type) { |
360 | 366 | type = "TIMESTAMP"; |
361 | 367 | break; |
362 | 368 | default: |
363 | | - type = "STRING"; |
| 369 | + type = convertDefaultType(type); |
364 | 370 | } |
365 | 371 | return type; |
366 | 372 | } |
367 | 373 |
|
| 374 | + /** |
| 375 | + * 转化decimal类型,带上精度 |
| 376 | + * |
| 377 | + * @param type type 类型 |
| 378 | + * @return 有精度的decimal |
| 379 | + */ |
| 380 | + private static String convertDefaultType(String type) { |
| 381 | + String toUpperCase = type.toUpperCase(Locale.ROOT); |
| 382 | + if (toUpperCase.contains(DECIMAL_KEY)) { |
| 383 | + Matcher matcher = DECIMAL_PATTERN.matcher(toUpperCase); |
| 384 | + if (matcher.matches()) { |
| 385 | + final int precision = Integer.parseInt(matcher.group(2).trim()); |
| 386 | + final int scale = Integer.parseInt(matcher.group(3).trim()); |
| 387 | + return String.format(DECIMAL_FORMAT, precision, scale); |
| 388 | + } else { |
| 389 | + throw new IllegalArgumentException( |
| 390 | + "Get wrong type of decimal, the type is: " + type); |
| 391 | + } |
| 392 | + } |
| 393 | + |
| 394 | + return "STRING"; |
| 395 | + } |
| 396 | + |
368 | 397 | public static AbstractBaseColumn parseDataFromMap(Object data) { |
369 | 398 | if (data == null) { |
370 | 399 | return new NullColumn(); |
|
0 commit comments