com.klarna.hiverunner.builder.HiveShellBuilder Java Examples

The following examples show how to use com.klarna.hiverunner.builder.HiveShellBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HiveRunnerCore.java    From HiveRunner with Apache License 2.0 6 votes vote down vote up
private void loadAnnotatedResources(Object testCase, HiveShellBuilder workFlowBuilder) throws IOException {
  Set<Field> fields = ReflectionUtils.getAllFields(testCase.getClass(), withAnnotation(HiveResource.class));

  for (Field resourceField : fields) {

    HiveResource annotation = resourceField.getAnnotation(HiveResource.class);
    String targetFile = annotation.targetFile();

    if (ReflectionUtils.isOfType(resourceField, String.class)) {
      String data = ReflectionUtils.getFieldValue(testCase, resourceField.getName(), String.class);
      workFlowBuilder.addResource(targetFile, data);
    } else if (ReflectionUtils.isOfType(resourceField, File.class) ||
        ReflectionUtils.isOfType(resourceField, Path.class)) {
      Path dataFile = getMandatoryPathFromField(testCase, resourceField);
      workFlowBuilder.addResource(targetFile, dataFile);
    } else {
      throw new IllegalArgumentException(
          "Fields annotated with @HiveResource currently only supports field type String, File or Path");
    }
  }
}
 
Example #2
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 6 votes vote down vote up
private void loadAnnotatedSetupScripts(Class testClass, HiveShellBuilder hiveShellBuilder) {
	Set<Field> setupScriptFields = ReflectionUtils.getAllFields(testClass, withAnnotation(HiveSetupScript.class));
	for (Field setupScriptField : setupScriptFields) {
		if (ReflectionUtils.isOfType(setupScriptField, String.class)) {
			String script = ReflectionUtils.getStaticFieldValue(testClass, setupScriptField.getName(), String.class);
			hiveShellBuilder.addSetupScript(script);
		} else if (ReflectionUtils.isOfType(setupScriptField, File.class) ||
				ReflectionUtils.isOfType(setupScriptField, Path.class)) {
			Path path = getMandatoryPathFromField(testClass, setupScriptField);
			hiveShellBuilder.addSetupScript(readAll(path));
		} else {
			throw new IllegalArgumentException(
					"Field annotated with @HiveSetupScript currently only supports type String, File and Path");
		}
	}
}
 
Example #3
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 6 votes vote down vote up
private void loadAnnotatedResources(Class testClass, HiveShellBuilder workFlowBuilder) throws IOException {
	Set<Field> fields = ReflectionUtils.getAllFields(testClass, withAnnotation(HiveResource.class));

	for (Field resourceField : fields) {

		HiveResource annotation = resourceField.getAnnotation(HiveResource.class);
		String targetFile = annotation.targetFile();

		if (ReflectionUtils.isOfType(resourceField, String.class)) {
			String data = ReflectionUtils.getStaticFieldValue(testClass, resourceField.getName(), String.class);
			workFlowBuilder.addResource(targetFile, data);
		} else if (ReflectionUtils.isOfType(resourceField, File.class) ||
				ReflectionUtils.isOfType(resourceField, Path.class)) {
			Path dataFile = getMandatoryPathFromField(testClass, resourceField);
			workFlowBuilder.addResource(targetFile, dataFile);
		} else {
			throw new IllegalArgumentException(
					"Fields annotated with @HiveResource currently only supports field type String, File or Path");
		}
	}
}
 
Example #4
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 6 votes vote down vote up
private void loadAnnotatedResources(Class testClass, HiveShellBuilder workFlowBuilder) throws IOException {
	Set<Field> fields = ReflectionUtils.getAllFields(testClass, withAnnotation(HiveResource.class));

	for (Field resourceField : fields) {

		HiveResource annotation = resourceField.getAnnotation(HiveResource.class);
		String targetFile = annotation.targetFile();

		if (ReflectionUtils.isOfType(resourceField, String.class)) {
			String data = ReflectionUtils.getStaticFieldValue(testClass, resourceField.getName(), String.class);
			workFlowBuilder.addResource(targetFile, data);
		} else if (ReflectionUtils.isOfType(resourceField, File.class) ||
				ReflectionUtils.isOfType(resourceField, Path.class)) {
			Path dataFile = getMandatoryPathFromField(testClass, resourceField);
			workFlowBuilder.addResource(targetFile, dataFile);
		} else {
			throw new IllegalArgumentException(
					"Fields annotated with @HiveResource currently only supports field type String, File or Path");
		}
	}
}
 
Example #5
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 6 votes vote down vote up
private void loadAnnotatedSetupScripts(Class testClass, HiveShellBuilder hiveShellBuilder) {
	Set<Field> setupScriptFields = ReflectionUtils.getAllFields(testClass, withAnnotation(HiveSetupScript.class));
	for (Field setupScriptField : setupScriptFields) {
		if (ReflectionUtils.isOfType(setupScriptField, String.class)) {
			String script = ReflectionUtils.getStaticFieldValue(testClass, setupScriptField.getName(), String.class);
			hiveShellBuilder.addSetupScript(script);
		} else if (ReflectionUtils.isOfType(setupScriptField, File.class) ||
				ReflectionUtils.isOfType(setupScriptField, Path.class)) {
			Path path = getMandatoryPathFromField(testClass, setupScriptField);
			hiveShellBuilder.addSetupScript(readAll(path));
		} else {
			throw new IllegalArgumentException(
					"Field annotated with @HiveSetupScript currently only supports type String, File and Path");
		}
	}
}
 
Example #6
Source File: HiveRunnerCore.java    From HiveRunner with Apache License 2.0 6 votes vote down vote up
private void loadAnnotatedSetupScripts(Object testCase, HiveShellBuilder workFlowBuilder) {
  Set<Field> setupScriptFields = ReflectionUtils.getAllFields(testCase.getClass(),
      withAnnotation(HiveSetupScript.class));

  for (Field setupScriptField : setupScriptFields) {
    if (ReflectionUtils.isOfType(setupScriptField, String.class)) {
      String script = ReflectionUtils.getFieldValue(testCase, setupScriptField.getName(), String.class);
      workFlowBuilder.addSetupScript(script);
    } else if (ReflectionUtils.isOfType(setupScriptField, File.class) ||
        ReflectionUtils.isOfType(setupScriptField, Path.class)) {
      Path path = getMandatoryPathFromField(testCase, setupScriptField);
      workFlowBuilder.addSetupScript(readAll(path));
    } else {
      throw new IllegalArgumentException(
          "Field annotated with @HiveSetupScript currently only supports type String, File and Path");
    }
  }
}
 
Example #7
Source File: HiveRunnerCore.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
private HiveShellField loadScriptUnderTest(Object testCaseInstance, HiveShellBuilder hiveShellBuilder) {
  try {
    Set<Field> fields = ReflectionUtils.getAllFields(testCaseInstance.getClass(), withAnnotation(HiveSQL.class));

    Preconditions.checkState(fields.size() == 1, "Exact one field should to be annotated with @HiveSQL");

    Field field = fields.iterator().next();
    List<Path> scriptPaths = new ArrayList<>();
    HiveSQL annotation = field.getAnnotation(HiveSQL.class);
    for (String scriptFilePath : annotation.files()) {
      Path file = Paths.get(Resources.getResource(scriptFilePath).toURI());
      assertFileExists(file);
      scriptPaths.add(file);
    }

    Charset charset = annotation.encoding().equals("") ?
        Charset.defaultCharset() : Charset.forName(annotation.encoding());

    boolean isAutoStart = annotation.autoStart();

    hiveShellBuilder.setScriptsUnderTest(scriptPaths, charset);

    return new HiveShellField() {
      @Override
      public void setShell(HiveShell shell) {
        ReflectionUtils.setField(testCaseInstance, 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);
  }
}
 
Example #8
Source File: HiveRunnerCore.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
private HiveShellContainer buildShell(List<? extends Script> scripts, Object testCase, HiveRunnerConfig config,
    HiveServerContext context) throws IOException {
  HiveServerContainer hiveTestHarness = new HiveServerContainer(context);

  HiveShellBuilder hiveShellBuilder = new HiveShellBuilder();
  hiveShellBuilder.setCommandShellEmulation(config.getCommandShellEmulator());

  HiveShellField shellSetter = loadScriptUnderTest(testCase, hiveShellBuilder);
  if (scripts != null) {
    hiveShellBuilder.overrideScriptsUnderTest(scripts);
  }

  hiveShellBuilder.setHiveServerContainer(hiveTestHarness);

  loadAnnotatedResources(testCase, hiveShellBuilder);

  loadAnnotatedProperties(testCase, hiveShellBuilder);

  loadAnnotatedSetupScripts(testCase, hiveShellBuilder);

  // Build shell
  HiveShellContainer shell = hiveShellBuilder.buildShell();

  // Set shell
  shellSetter.setShell(shell);

  if (shellSetter.isAutoStart()) {
    shell.start();
  }
  return shell;
}
 
Example #9
Source File: HiveRunnerShimV4.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void setCommandShellEmulation(HiveShellBuilder builder, HiveRunnerConfig config) throws Exception {
	Method method = config.getClass().getDeclaredMethod("getCommandShellEmulator");
	Object emulator = method.invoke(config);
	Class emulatorClz = Class.forName("com.klarna.hiverunner.sql.cli.CommandShellEmulator");
	method = builder.getClass().getDeclaredMethod("setCommandShellEmulation", emulatorClz);
	method.invoke(builder, emulator);
}
 
Example #10
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 5 votes vote down vote up
private void loadAnnotatedProperties(Class testClass, HiveShellBuilder workFlowBuilder) {
	for (Field hivePropertyField : ReflectionUtils.getAllFields(testClass, withAnnotation(HiveProperties.class))) {
		Preconditions.checkState(ReflectionUtils.isOfType(hivePropertyField, Map.class),
				"Field annotated with @HiveProperties should be of type Map<String, String>");
		workFlowBuilder.putAllProperties(
				ReflectionUtils.getStaticFieldValue(testClass, hivePropertyField.getName(), Map.class));
	}
}
 
Example #11
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example #12
Source File: HiveRunnerShimV3.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void setCommandShellEmulation(HiveShellBuilder builder, HiveRunnerConfig config) throws Exception {
	Method method = HiveRunnerConfig.class.getDeclaredMethod("getCommandShellEmulation");
	Object emulation = method.invoke(config);
	Class emulationClz = Class.forName("com.klarna.hiverunner.CommandShellEmulation");
	method = HiveShellBuilder.class.getDeclaredMethod("setCommandShellEmulation", emulationClz);
	method.invoke(builder, emulation);
}
 
Example #13
Source File: HiveRunnerShimV3.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void setCommandShellEmulation(HiveShellBuilder builder, HiveRunnerConfig config) throws Exception {
	Method method = HiveRunnerConfig.class.getDeclaredMethod("getCommandShellEmulation");
	Object emulation = method.invoke(config);
	Class emulationClz = Class.forName("com.klarna.hiverunner.CommandShellEmulation");
	method = HiveShellBuilder.class.getDeclaredMethod("setCommandShellEmulation", emulationClz);
	method.invoke(builder, emulation);
}
 
Example #14
Source File: HiveRunnerShimV4.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void setCommandShellEmulation(HiveShellBuilder builder, HiveRunnerConfig config) throws Exception {
	Method method = config.getClass().getDeclaredMethod("getCommandShellEmulator");
	Object emulator = method.invoke(config);
	Class emulatorClz = Class.forName("com.klarna.hiverunner.sql.cli.CommandShellEmulator");
	method = builder.getClass().getDeclaredMethod("setCommandShellEmulation", emulatorClz);
	method.invoke(builder, emulator);
}
 
Example #15
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 5 votes vote down vote up
private void loadAnnotatedProperties(Class testClass, HiveShellBuilder workFlowBuilder) {
	for (Field hivePropertyField : ReflectionUtils.getAllFields(testClass, withAnnotation(HiveProperties.class))) {
		Preconditions.checkState(ReflectionUtils.isOfType(hivePropertyField, Map.class),
				"Field annotated with @HiveProperties should be of type Map<String, String>");
		workFlowBuilder.putAllProperties(
				ReflectionUtils.getStaticFieldValue(testClass, hivePropertyField.getName(), Map.class));
	}
}
 
Example #16
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example #17
Source File: HiveRunnerCore.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
private void loadAnnotatedProperties(Object testCase, HiveShellBuilder workFlowBuilder) {
  for (Field hivePropertyField : ReflectionUtils.getAllFields(testCase.getClass(),
      withAnnotation(HiveProperties.class))) {
    Preconditions.checkState(ReflectionUtils.isOfType(hivePropertyField, Map.class),
        "Field annotated with @HiveProperties should be of type Map<String, String>");
    workFlowBuilder.putAllProperties(
        ReflectionUtils.getFieldValue(testCase, hivePropertyField.getName(), Map.class));
  }
}
 
Example #18
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Traverses the test class annotations. Will inject a HiveShell in the test case that envelopes the HiveServer.
 */
private HiveShellContainer createHiveServerContainer(final Class testClass, HiveServerContext context)
		throws Exception {

	final HiveServerContainer hiveServerContainer = new HiveServerContainer(context);

	HiveShellBuilder hiveShellBuilder = new HiveShellBuilder();
	HiveRunnerShim hiveRunnerShim = HiveRunnerShimLoader.load();
	hiveRunnerShim.setCommandShellEmulation(hiveShellBuilder, config);

	HiveShellField shellSetter = loadScriptsUnderTest(testClass, hiveShellBuilder);

	hiveShellBuilder.setHiveServerContainer(hiveServerContainer);

	loadAnnotatedResources(testClass, hiveShellBuilder);

	loadAnnotatedProperties(testClass, hiveShellBuilder);

	loadAnnotatedSetupScripts(testClass, hiveShellBuilder);

	// Build shell
	final HiveShellContainer shell = hiveShellBuilder.buildShell();

	// Set shell
	shellSetter.setShell(shell);

	if (shellSetter.isAutoStart()) {
		shell.start();
	}

	return shell;
}
 
Example #19
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Traverses the test class annotations. Will inject a HiveShell in the test case that envelopes the HiveServer.
 */
private HiveShellContainer createHiveServerContainer(final Class testClass, HiveServerContext context)
		throws Exception {

	final HiveServerContainer hiveServerContainer = new HiveServerContainer(context);

	HiveShellBuilder hiveShellBuilder = new HiveShellBuilder();
	HiveRunnerShim hiveRunnerShim = HiveRunnerShimLoader.load();
	hiveRunnerShim.setCommandShellEmulation(hiveShellBuilder, config);

	HiveShellField shellSetter = loadScriptsUnderTest(testClass, hiveShellBuilder);

	hiveShellBuilder.setHiveServerContainer(hiveServerContainer);

	loadAnnotatedResources(testClass, hiveShellBuilder);

	loadAnnotatedProperties(testClass, hiveShellBuilder);

	loadAnnotatedSetupScripts(testClass, hiveShellBuilder);

	// Build shell
	final HiveShellContainer shell = hiveShellBuilder.buildShell();

	// Set shell
	shellSetter.setShell(shell);

	if (shellSetter.isAutoStart()) {
		shell.start();
	}

	return shell;
}
 
Example #20
Source File: HiveRunnerShim.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Sets CommandShellEmulation for HiveShellBuilder.
 */
void setCommandShellEmulation(HiveShellBuilder builder, HiveRunnerConfig config) throws Exception;
 
Example #21
Source File: HiveRunnerShim.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Sets CommandShellEmulation for HiveShellBuilder.
 */
void setCommandShellEmulation(HiveShellBuilder builder, HiveRunnerConfig config) throws Exception;