org.apache.hive.service.cli.CLIService Java Examples

The following examples show how to use org.apache.hive.service.cli.CLIService. 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: HiveTester.java    From transport with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void createHiveServer() {
  HiveServer2 server = new HiveServer2();
  server.init(new HiveConf());
  for (Service service : server.getServices()) {
    if (service instanceof CLIService) {
      _client = (CLIService) service;
    }
  }
  Preconditions.checkNotNull(_client, "CLI service not found in local Hive server");
  try {
    _sessionHandle = _client.openSession(null, null, null);
    _functionRegistry = SessionState.getRegistryForWrite();
    // "map_from_entries" UDF is required to create maps with non-primitive key types
    _functionRegistry.registerGenericUDF("map_from_entries", MapFromEntriesWrapper.class);
    // TODO: This is a hack. Hive's public API does not have a way to register an already created GenericUDF object
    // It only accepts a class name after which the parameterless constructor of the class is called to create a
    // GenericUDF object. This does not work for HiveTestStdUDFWrapper as it accepts the UDF classes as parameters.
    // However, Hive has an internal method which does allow passing GenericUDF objects instead of classes.
    _functionRegistryAddFunctionMethod =
        _functionRegistry.getClass().getDeclaredMethod("addFunction", String.class, FunctionInfo.class);
    _functionRegistryAddFunctionMethod.setAccessible(true);
  } catch (HiveSQLException | NoSuchMethodException e) {
    throw new RuntimeException(e);
  }
}
 
Example #2
Source File: HiveEmbeddedServer2.java    From elasticsearch-hadoop with Apache License 2.0 6 votes vote down vote up
private void waitForStartup() throws Exception {
    long timeout = TimeUnit.MINUTES.toMillis(1);
    long unitOfWait = TimeUnit.SECONDS.toMillis(1);

    CLIService hs2Client = getServiceClientInternal();
    SessionHandle sessionHandle = null;
    for (int interval = 0; interval < timeout / unitOfWait; interval++) {
        Thread.sleep(unitOfWait);
        try {
            Map <String, String> sessionConf = new HashMap<String, String>();
            sessionHandle = hs2Client.openSession("foo", "bar", sessionConf);
            return;
        } catch (Exception e) {
            // service not started yet
            continue;
        }
        finally {
            hs2Client.closeSession(sessionHandle);
        }
    }
    throw new TimeoutException("Couldn't get a hold of HiveServer2...");
}
 
Example #3
Source File: HiveEmbeddedServer2.java    From elasticsearch-hadoop with Apache License 2.0 5 votes vote down vote up
private CLIService getServiceClientInternal() {
    for (Service service : hiveServer.getServices()) {
        if (service instanceof CLIService) {
            return (CLIService) service;
        }
    }
    throw new IllegalStateException("Cannot find CLIService");
}
 
Example #4
Source File: HiveEmbeddedServer2.java    From elasticsearch-hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> execute(String cmd) throws Exception {
    if (cmd.toUpperCase().startsWith("ADD JAR")) {
        // skip the jar since we're running in local mode
        System.out.println("Skipping ADD JAR in local/embedded mode");
        return Collections.emptyList();
    }
    // remove bogus configuration
    config.set("columns.comments", "");
    CLIService client = getServiceClientInternal();
    SessionHandle sh = null;
    try {
        Map<String, String> opConf = new HashMap<String, String>();
        sh = client.openSession("anonymous", "anonymous", opConf);
        OperationHandle oh = client.executeStatement(sh, cmd, opConf);

        if (oh.hasResultSet()) {
            RowSet rows = client.fetchResults(oh);
            List<String> result = new ArrayList<String>(rows.numRows());
            for (Object[] objects : rows) {
                result.add(StringUtils.concatenate(objects, ","));
            }
            return result;
        }
        return Collections.emptyList();

    } finally {
        if (sh != null) {
            client.closeSession(sh);
        }
    }
}
 
Example #5
Source File: HiveServerContainer.java    From HiveRunner with Apache License 2.0 5 votes vote down vote up
/**
 * Will start the HiveServer.
 *
 * @param testConfig Specific test case properties. Will be merged with the HiveConf of the context
 * @param hiveVars   HiveVars to pass on to the HiveServer for this session
 */
public void init(Map<String, String> testConfig, Map<String, String> hiveVars) {

    context.init();

    HiveConf hiveConf = context.getHiveConf();

    // merge test case properties with hive conf before HiveServer is started.
    for (Map.Entry<String, String> property : testConfig.entrySet()) {
        hiveConf.set(property.getKey(), property.getValue());
    }

    try {
        hiveServer2 = new HiveServer2();
        hiveServer2.init(hiveConf);

        // Locate the ClIService in the HiveServer2
        for (Service service : hiveServer2.getServices()) {
            if (service instanceof CLIService) {
                client = (CLIService) service;
            }
        }

        Preconditions.checkNotNull(client, "ClIService was not initialized by HiveServer2");

        sessionHandle = client.openSession("noUser", "noPassword", null);

        SessionState sessionState = client.getSessionManager().getSession(sessionHandle).getSessionState();
        currentSessionState = sessionState;
        currentSessionState.setHiveVariables(hiveVars);
    } catch (Exception e) {
        throw new IllegalStateException("Failed to create HiveServer :" + e.getMessage(), e);
    }

    // Ping hive server before we do anything more with it! If validation
    // is switched on, this will fail if metastorage is not set up properly
    pingHiveServer();
}
 
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: HiveServerContainer.java    From HiveRunner with Apache License 2.0 4 votes vote down vote up
public CLIService getClient() {
    return client;
}