HiveClient { protected void createTableIfNotExist(String table, MessageType schema, String location) throws SQLException { String hiveSchema = schema.getFields().stream().map(field -> { try { return field.getName() + " " + inferHiveType(field); } catch (Exception e) { throw new RuntimeException(e); } }).collect(Collectors.joining(", ")); String tableCreation = "CREATE EXTERNAL TABLE IF NOT EXISTS " + database + "." + table + "(" + hiveSchema + ")" + " PARTITIONED BY (day string)" + " STORED AS PARQUET" + " LOCATION '" + location + "'"; LOGGER.info("Create table {} if not exists", table); execute(tableCreation); } HiveClient(String driverName, String jdbcUrl, String database, String location); void createDatabaseIfAbsent(String location); void createPartitionIfNotExist(String table, MessageType schema, String partition, String location); void close();  }
@Test public void createTableWithoutIssue() throws SQLException { PrimitiveType appId = new PrimitiveType(Type.Repetition.OPTIONAL, PrimitiveType.PrimitiveTypeName.BINARY, "app_id"); MessageType schema = new MessageType("fs", appId); String table = "fs"; String location = "file:" + hdfsTemp + "/garmadon_database/fs"; HiveClient hiveClient = new HiveClient(driverName, "jdbc:hive2: hdfsTemp + "/garmadon_database"); hiveClient.createTableIfNotExist(table, schema, location); HashMap<String, String> result = getResultHashTableDesc(hiveClient, table); assertEquals(location, result.get("Location")); assertEquals("EXTERNAL_TABLE", result.get("Table Type").trim()); assertEquals("string", result.get("day")); assertEquals("string", result.get("app_id")); }
@Test(expected = RuntimeException.class) public void tryCreateTableWithUnsupportedDataTypeShouldThrowRuntimeException() throws SQLException { PrimitiveType unsupported = new PrimitiveType(Type.Repetition.OPTIONAL, PrimitiveType.PrimitiveTypeName.INT96, "unsupported"); MessageType schema = new MessageType("fs", unsupported); String table = "fs"; String location = "file:" + hdfsTemp + "/garmadon_database/fs"; HiveClient hiveClient = new HiveClient(driverName, "jdbc:hive2: hdfsTemp + "/garmadon_database"); hiveClient.createTableIfNotExist(table, schema, location); }