Search in sources :

Example 1 with HiveSQL

use of com.klarna.hiverunner.annotations.HiveSQL in project flink by apache.

the class FlinkEmbeddedHiveRunner method loadScriptsUnderTest.

private HiveShellField loadScriptsUnderTest(final Class testClass, HiveShellBuilder hiveShellBuilder) {
    try {
        Set<Field> fields = ReflectionUtils.getAllFields(testClass, withAnnotation(HiveSQL.class));
        Preconditions.checkState(fields.size() == 1, "Exactly one field should to be annotated with @HiveSQL");
        final Field field = fields.iterator().next();
        List<Path> scripts = new ArrayList<>();
        HiveSQL annotation = field.getAnnotation(HiveSQL.class);
        for (String scriptFilePath : annotation.files()) {
            Path file = Paths.get(Resources.getResource(scriptFilePath).toURI());
            Preconditions.checkState(Files.exists(file), "File " + file + " does not exist");
            scripts.add(file);
        }
        Charset charset = annotation.encoding().equals("") ? Charset.defaultCharset() : Charset.forName(annotation.encoding());
        final boolean isAutoStart = annotation.autoStart();
        hiveShellBuilder.setScriptsUnderTest(scripts, charset);
        return new HiveShellField() {

            @Override
            public void setShell(HiveShell shell) {
                ReflectionUtils.setStaticField(testClass, field.getName(), shell);
            }

            @Override
            public boolean isAutoStart() {
                return isAutoStart;
            }
        };
    } catch (Throwable t) {
        throw new IllegalArgumentException("Failed to init field annotated with @HiveSQL: " + t.getMessage(), t);
    }
}
Also used : Path(java.nio.file.Path) HiveSQL(com.klarna.hiverunner.annotations.HiveSQL) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) Field(java.lang.reflect.Field) HiveShell(com.klarna.hiverunner.HiveShell)

Aggregations

HiveShell (com.klarna.hiverunner.HiveShell)1 HiveSQL (com.klarna.hiverunner.annotations.HiveSQL)1 Field (java.lang.reflect.Field)1 Charset (java.nio.charset.Charset)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1