com.klarna.hiverunner.HiveShell Java Examples

The following examples show how to use com.klarna.hiverunner.HiveShell. 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: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 6 votes vote down vote up
@Test
public void scriptFilesAreImportedInOtherScriptsBeeline() throws IOException {
    String hiveSql = "use default";
    
    File importedFile = new File(tempFolder.getRoot(), "imported_script.sql");
    Files.write(hiveSql, importedFile, UTF_8);
    
    HiveShell shell = createBeelineShell();
    shell.start();
    
    String importhiveSql = "!run " + importedFile.getAbsolutePath();
    File file = new File(tempFolder.getRoot(), "script.sql");
    Files.write(importhiveSql, file, UTF_8);
    
    shell.execute(file);
    
    verify(container).executeStatement(hiveSql);
}
 
Example #2
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 6 votes vote down vote up
@Test
public void scriptFilesAreImportedInOtherScriptsHiveCli() throws IOException {
  String hiveSql = "use default";

  File importedFile = new File(tempFolder.getRoot(), "imported_script.sql");
  Files.write(hiveSql, importedFile, UTF_8);

  HiveShell shell = createHiveCliShell();
  shell.start();

  String importhiveSql = "source " + importedFile.getAbsolutePath();
  File file = new File(tempFolder.getRoot(), "script.sql");
  Files.write(importhiveSql, file, UTF_8);

  shell.execute(file);

  verify(container).executeStatement(hiveSql);
}
 
Example #3
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 6 votes vote down vote up
@Test
public void scriptFilesAreImportedInQueries() throws IOException {
  String hiveSql = "use default";

  File importedFile = new File(tempFolder.getRoot(), "imported_script.sql");
  Files.write(hiveSql, importedFile, UTF_8);

  HiveShell shell = createHiveCliShell();
  shell.start();

  String importhiveSql = "source " + importedFile.getAbsolutePath();
  List<String> results = shell.executeQuery(importhiveSql);

  assertThat(results.size(), is(0));
  verify(container).executeStatement(hiveSql);
}
 
Example #4
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 6 votes vote down vote up
@Test
public void executeQueryFromFile() throws IOException {
    HiveShell shell = createHiveCliShell();
    shell.start();

    String statement = "select current_database(), NULL, 100";
    when(container.executeStatement(statement)).thenReturn(Arrays.<Object[]> asList( new Object[] {"default", null, 100}));
    String hiveSql = statement + ";";

    File file = tempFolder.newFile("script.sql");
    Files.write(hiveSql, file, UTF_8);

    List<String> results = shell.executeQuery(UTF_8, Paths.get(file.toURI()), "xxx", "yyy");
    assertThat(results.size(), is(1));
    assertThat(results.get(0), is("defaultxxxyyyxxx100"));
}
 
Example #5
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
@Test
public void executeScriptCharsetFile() throws IOException {
  String hiveSql = "use default";

  File file = new File(tempFolder.getRoot(), "script.sql");
  Files.write(hiveSql, file, UTF_8);

  HiveShell shell = createHiveCliShell();
  shell.start();
  shell.execute(UTF_8, file);

  verify(container).executeStatement(hiveSql);
}
 
Example #6
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
private HiveShell createHiveShell(CommandShellEmulator emulation, String... keyValues) {
    Map<String, String> hiveConf = MapUtils.putAll(new HashMap(), keyValues);
    HiveConf conf = createHiveconf(hiveConf);

    CLIService client = Mockito.mock(CLIService.class);

    container = Mockito.mock(HiveServerContainer.class);

    List<String> setupScripts = Arrays.asList();
    List<HiveResource> hiveResources = Arrays.asList();
    List<Script> scriptsUnderTest = Arrays.asList();

    return new HiveShellBase(container, hiveConf, setupScripts, hiveResources, scriptsUnderTest, emulation);
}
 
Example #7
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void executeQueryFromFileZeroStatements() throws IOException {
    HiveShell shell = createHiveCliShell();
    shell.start();
    
    String hiveSql = "";
    
    File file = new File(tempFolder.getRoot(), "script.sql");
    Files.write(hiveSql, file, UTF_8);
    
    shell.executeQuery(UTF_8, Paths.get(file.toURI()), "xxx", "yyy");
}
 
Example #8
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void executeQueryFromFileMoreThanOneStatement() throws IOException {
    HiveShell shell = createHiveCliShell();
    shell.start();
    
    String hiveSql = "use default;\nselect current_database(), NULL, 100;";
    
    File file = new File(tempFolder.getRoot(), "script.sql");
    Files.write(hiveSql, file, UTF_8);
    
    shell.executeQuery(UTF_8, Paths.get(file.toURI()), "xxx", "yyy");
}
 
Example #9
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void executeScriptNotStarted() throws IOException {
  File file = new File(tempFolder.getRoot(), "script.sql");
  
  HiveShell shell = createHiveCliShell();
  shell.execute(UTF_8, Paths.get(file.toURI()));
}
 
Example #10
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void executeScriptFileNotExists() throws IOException {
  File file = new File(tempFolder.getRoot(), "script.sql");

  HiveShell shell = createHiveCliShell();
  shell.start();
  shell.execute(UTF_8, Paths.get(file.toURI()));
}
 
Example #11
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
@Test
public void executeScriptCharsetPath() throws IOException {
  String hiveSql = "use default";

  File file = new File(tempFolder.getRoot(), "script.sql");
  Files.write(hiveSql, file, UTF_8);

  HiveShell shell = createHiveCliShell();
  shell.start();
  shell.execute(UTF_8, Paths.get(file.toURI()));

  verify(container).executeStatement(hiveSql);
}
 
Example #12
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
@Test
public void executeScriptPath() throws IOException {
  String hiveSql = "use default";

  File file = new File(tempFolder.getRoot(), "script.sql");
  Files.write(hiveSql, file, UTF_8);

  HiveShell shell = createHiveCliShell();
  shell.start();
  shell.execute(Paths.get(file.toURI()));

  verify(container).executeStatement(hiveSql);
}
 
Example #13
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 #14
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
@Test
public void executeScriptFile() throws IOException {
  String hiveSql = "use default";

  File file = new File(tempFolder.getRoot(), "script.sql");
  Files.write(hiveSql, file, UTF_8);

  HiveShell shell = createHiveCliShell();
  shell.start();
  shell.execute(file);

  verify(container).executeStatement(hiveSql);
}
 
Example #15
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
@Test
public void setupScriptsShouldBeExecutedAtStart() throws IOException {
    HiveShell shell = createHiveCliShell();
    shell.addSetupScript("foo");
    shell.addSetupScripts(tempFolder.newFile("foo"));
    shell.start();
}
 
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: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void setupScriptsMayNotBeAddedAfterShellIsStarted() throws IOException {
    HiveShell shell = createHiveCliShell();
    shell.start();
    shell.addSetupScripts(tempFolder.newFile("foo"));
}
 
Example #18
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void invalidFilePathShouldThrowException() {
    HiveShell shell = createHiveCliShell();
    shell.addSetupScripts(new File("foo"));
}
 
Example #19
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void setupScriptMayNotBeAddedAfterShellIsStarted() {
    HiveShell shell = createHiveCliShell();
    shell.start();
    shell.addSetupScript("foo");
}
 
Example #20
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 4 votes vote down vote up
@Test
public void setupScriptMayBeAddedBeforeStart() throws IOException {
    HiveShell shell = createHiveCliShell();
    shell.addSetupScript("foo");
    shell.addSetupScripts(tempFolder.newFile("foo"));
}
 
Example #21
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void variableSubstitutionShouldBlowUpIfShellIsNotStarted() {
    HiveShell shell = createHiveCliShell("origin", "spanish");
    shell.expandVariableSubstitutes("The ${hiveconf:origin} fox");
}
 
Example #22
Source File: HiveTestUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
public TextTableInserter(HiveShell hiveShell, String dbName, String tableName) {
	this.hiveShell = hiveShell;
	this.dbName = dbName;
	this.tableName = tableName;
	rows = new ArrayList<>();
}
 
Example #23
Source File: HiveTestUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
public static TextTableInserter createTextTableInserter(HiveShell hiveShell, String dbName, String tableName) {
	return new TextTableInserter(hiveShell, dbName, tableName);
}
 
Example #24
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 4 votes vote down vote up
private HiveShell createHiveCliShell(String... keyValues) {
    return createHiveShell(HiveCliEmulator.INSTANCE, keyValues);
}
 
Example #25
Source File: HiveShellBaseTest.java    From HiveRunner with Apache License 2.0 4 votes vote down vote up
private HiveShell createBeelineShell(String... keyValues) {
    return createHiveShell(BeelineEmulator.INSTANCE, keyValues);
}
 
Example #26
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 votes vote down vote up
void setShell(HiveShell shell); 
Example #27
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 votes vote down vote up
void setShell(HiveShell shell);