Example usage for org.apache.commons.cli CommandLine getOptionValues

List of usage examples for org.apache.commons.cli CommandLine getOptionValues

Introduction

In this page you can find the example usage for org.apache.commons.cli CommandLine getOptionValues.

Prototype

public String[] getOptionValues(char opt) 

Source Link

Document

Retrieves the array of values, if any, of an option.

Usage

From source file:org.apache.helix.provisioning.tools.ContainerAdmin.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Option zkServerOption = OptionBuilder.withLongOpt("zookeeperAddress")
            .withDescription("Provide zookeeper address").create();
    zkServerOption.setArgs(1);//from  ww  w.  j  a va  2 s  .  c  om
    zkServerOption.setRequired(true);
    zkServerOption.setArgName("zookeeperAddress(Required)");

    OptionGroup group = new OptionGroup();
    group.setRequired(true);

    // update container count per service
    Option stopContainerOption = OptionBuilder.withLongOpt(stopContainer)
            .withDescription("appName participantName").create();
    stopContainerOption.setArgs(2);
    stopContainerOption.setRequired(false);
    stopContainerOption.setArgName("appName participantName");

    group.addOption(stopContainerOption);

    Options options = new Options();
    options.addOption(zkServerOption);
    options.addOptionGroup(group);
    CommandLine cliParser = new GnuParser().parse(options, args);

    String zkAddress = cliParser.getOptionValue("zookeeperAddress");
    ContainerAdmin admin = new ContainerAdmin(zkAddress);

    if (cliParser.hasOption(stopContainer)) {
        String appName = cliParser.getOptionValues(stopContainer)[0];
        String participantName = cliParser.getOptionValues(stopContainer)[1];
        admin.stopContainer(appName, participantName);
    }
}

From source file:org.apache.helix.provisioning.tools.UpdateProvisionerConfig.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws ParseException {
    Option zkServerOption = OptionBuilder.withLongOpt("zookeeperAddress")
            .withDescription("Provide zookeeper address").create();
    zkServerOption.setArgs(1);//from   w w  w .j  a  v  a 2  s. c o  m
    zkServerOption.setRequired(true);
    zkServerOption.setArgName("zookeeperAddress(Required)");

    OptionGroup group = new OptionGroup();
    group.setRequired(true);

    // update container count per service
    Option updateContainerCountOption = OptionBuilder.withLongOpt(updateContainerCount)
            .withDescription("appName serviceName numContainers").create();
    updateContainerCountOption.setArgs(3);
    updateContainerCountOption.setRequired(false);
    updateContainerCountOption.setArgName("appName serviceName numContainers");

    group.addOption(updateContainerCountOption);

    Options options = new Options();
    options.addOption(zkServerOption);
    options.addOptionGroup(group);
    CommandLine cliParser = new GnuParser().parse(options, args);

    String zkAddress = cliParser.getOptionValue("zookeeperAddress");
    UpdateProvisionerConfig updater = new UpdateProvisionerConfig(zkAddress);

    if (cliParser.hasOption(updateContainerCount)) {
        String appName = cliParser.getOptionValues(updateContainerCount)[0];
        String serviceName = cliParser.getOptionValues(updateContainerCount)[1];
        int numContainers = Integer.parseInt(cliParser.getOptionValues(updateContainerCount)[2]);
        updater.setNumContainers(appName, serviceName, numContainers);
    }

}

From source file:org.apache.helix.tools.ClusterSetup.java

public static int processCommandLineArgs(String[] cliArgs) throws Exception {
    CommandLineParser cliParser = new GnuParser();
    Options cliOptions = constructCommandLineOptions();
    CommandLine cmd = null;

    try {/*w ww .  ja  va 2 s .  c  o  m*/
        cmd = cliParser.parse(cliOptions, cliArgs);
    } catch (ParseException pe) {
        System.err.println("CommandLineClient: failed to parse command-line options: " + pe.toString());
        printUsage(cliOptions);
        System.exit(1);
    }

    ClusterSetup setupTool = new ClusterSetup(cmd.getOptionValue(zkServerAddress));

    if (cmd.hasOption(addCluster)) {
        String clusterName = cmd.getOptionValue(addCluster);
        setupTool.addCluster(clusterName, false);
        return 0;
    }

    if (cmd.hasOption(activateCluster)) {
        String clusterName = cmd.getOptionValues(activateCluster)[0];
        String grandCluster = cmd.getOptionValues(activateCluster)[1];
        boolean enable = Boolean.parseBoolean(cmd.getOptionValues(activateCluster)[2]);
        setupTool.activateCluster(clusterName, grandCluster, enable);
        return 0;
    }

    if (cmd.hasOption(dropCluster)) {
        String clusterName = cmd.getOptionValue(dropCluster);
        setupTool.deleteCluster(clusterName);
        return 0;
    }

    if (cmd.hasOption(addInstance)) {
        String clusterName = cmd.getOptionValues(addInstance)[0];
        String instanceAddressInfo = cmd.getOptionValues(addInstance)[1];
        String[] instanceAddresses = instanceAddressInfo.split(";");
        setupTool.addInstancesToCluster(clusterName, instanceAddresses);
        return 0;
    }

    if (cmd.hasOption(addResource)) {
        String clusterName = cmd.getOptionValues(addResource)[0];
        String resourceName = cmd.getOptionValues(addResource)[1];
        int partitions = Integer.parseInt(cmd.getOptionValues(addResource)[2]);
        String stateModelRef = cmd.getOptionValues(addResource)[3];
        String modeValue = RebalanceMode.SEMI_AUTO.toString();
        if (cmd.hasOption(mode)) {
            modeValue = cmd.getOptionValues(mode)[0];
        }

        int bucketSizeVal = 0;
        if (cmd.hasOption(bucketSize)) {
            bucketSizeVal = Integer.parseInt(cmd.getOptionValues(bucketSize)[0]);
        }

        int maxPartitionsPerNodeVal = -1;
        if (cmd.hasOption(maxPartitionsPerNode)) {
            maxPartitionsPerNodeVal = Integer.parseInt(cmd.getOptionValues(maxPartitionsPerNode)[0]);
        }
        setupTool.addResourceToCluster(clusterName, resourceName, partitions, stateModelRef, modeValue,
                bucketSizeVal, maxPartitionsPerNodeVal);
        return 0;
    }

    if (cmd.hasOption(rebalance)) {
        String clusterName = cmd.getOptionValues(rebalance)[0];
        String resourceName = cmd.getOptionValues(rebalance)[1];
        int replicas = Integer.parseInt(cmd.getOptionValues(rebalance)[2]);
        String keyPrefixVal = "";
        String instanceGroupTagVal = "";
        if (cmd.hasOption(resourceKeyPrefix)) {
            keyPrefixVal = cmd.getOptionValue(resourceKeyPrefix);
        }
        if (cmd.hasOption(instanceGroupTag)) {
            instanceGroupTagVal = cmd.getOptionValue(instanceGroupTag);
        }
        setupTool.rebalanceCluster(clusterName, resourceName, replicas, keyPrefixVal, instanceGroupTagVal);
        return 0;
    }

    if (cmd.hasOption(expandCluster)) {
        String clusterName = cmd.getOptionValues(expandCluster)[0];

        setupTool.expandCluster(clusterName);
        return 0;
    }

    if (cmd.hasOption(expandResource)) {
        String clusterName = cmd.getOptionValues(expandResource)[0];
        String resourceName = cmd.getOptionValues(expandResource)[1];
        setupTool.expandResource(clusterName, resourceName);
        return 0;
    }

    if (cmd.hasOption(dropInstance)) {
        String clusterName = cmd.getOptionValues(dropInstance)[0];
        String instanceAddressInfo = cmd.getOptionValues(dropInstance)[1];
        String[] instanceAddresses = instanceAddressInfo.split(";");
        setupTool.dropInstancesFromCluster(clusterName, instanceAddresses);
        return 0;
    }

    if (cmd.hasOption(listClusters)) {
        List<String> clusters = setupTool.getClusterManagementTool().getClusters();

        System.out.println("Existing clusters:");
        for (String cluster : clusters) {
            System.out.println(cluster);
        }
        return 0;
    }

    if (cmd.hasOption(listResources)) {
        String clusterName = cmd.getOptionValue(listResources);
        List<String> resourceNames = setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);

        System.out.println("Existing resources in cluster " + clusterName + ":");
        for (String resourceName : resourceNames) {
            System.out.println(resourceName);
        }
        return 0;
    } else if (cmd.hasOption(listClusterInfo)) {
        String clusterName = cmd.getOptionValue(listClusterInfo);
        List<String> resourceNames = setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);
        List<String> instances = setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);

        System.out.println("Existing resources in cluster " + clusterName + ":");
        for (String resourceName : resourceNames) {
            System.out.println(resourceName);
        }

        System.out.println("Instances in cluster " + clusterName + ":");
        for (String InstanceName : instances) {
            System.out.println(InstanceName);
        }
        return 0;
    } else if (cmd.hasOption(listInstances)) {
        String clusterName = cmd.getOptionValue(listInstances);
        List<String> instances = setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);

        System.out.println("Instances in cluster " + clusterName + ":");
        for (String instanceName : instances) {
            System.out.println(instanceName);
        }
        return 0;
    } else if (cmd.hasOption(listInstanceInfo)) {
        String clusterName = cmd.getOptionValues(listInstanceInfo)[0];
        String instanceName = cmd.getOptionValues(listInstanceInfo)[1];
        InstanceConfig config = setupTool.getClusterManagementTool().getInstanceConfig(clusterName,
                instanceName);

        String result = new String(new ZNRecordSerializer().serialize(config.getRecord()));
        System.out.println("InstanceConfig: " + result);
        return 0;
    } else if (cmd.hasOption(listResourceInfo)) {
        // print out partition number, resource name and replication number
        // Also the ideal states and current states
        String clusterName = cmd.getOptionValues(listResourceInfo)[0];
        String resourceName = cmd.getOptionValues(listResourceInfo)[1];
        IdealState idealState = setupTool.getClusterManagementTool().getResourceIdealState(clusterName,
                resourceName);
        ExternalView externalView = setupTool.getClusterManagementTool().getResourceExternalView(clusterName,
                resourceName);

        if (idealState != null) {
            System.out.println("IdealState for " + resourceName + ":");
            System.out.println(new String(new ZNRecordSerializer().serialize(idealState.getRecord())));
        } else {
            System.out.println("No idealState for " + resourceName);
        }

        System.out.println();

        if (externalView != null) {
            System.out.println("ExternalView for " + resourceName + ":");
            System.out.println(new String(new ZNRecordSerializer().serialize(externalView.getRecord())));
        } else {
            System.out.println("No externalView for " + resourceName);
        }
        return 0;

    } else if (cmd.hasOption(listPartitionInfo)) {
        // print out where the partition master / slaves locates
        String clusterName = cmd.getOptionValues(listPartitionInfo)[0];
        String resourceName = cmd.getOptionValues(listPartitionInfo)[1];
        String partitionName = cmd.getOptionValues(listPartitionInfo)[2];
        IdealState idealState = setupTool.getClusterManagementTool().getResourceIdealState(clusterName,
                resourceName);
        ExternalView externalView = setupTool.getClusterManagementTool().getResourceExternalView(clusterName,
                resourceName);

        if (idealState != null) {
            ZNRecord partInfo = new ZNRecord(resourceName + "/" + partitionName);
            ZNRecord idealStateRec = idealState.getRecord();
            partInfo.setSimpleFields(idealStateRec.getSimpleFields());
            if (idealStateRec.getMapField(partitionName) != null) {
                partInfo.setMapField(partitionName, idealStateRec.getMapField(partitionName));
            }
            if (idealStateRec.getListField(partitionName) != null) {
                partInfo.setListField(partitionName, idealStateRec.getListField(partitionName));
            }
            System.out.println("IdealState for " + resourceName + "/" + partitionName + ":");
            System.out.println(new String(new ZNRecordSerializer().serialize(partInfo)));
        } else {
            System.out.println("No idealState for " + resourceName + "/" + partitionName);
        }

        System.out.println();

        if (externalView != null) {
            ZNRecord partInfo = new ZNRecord(resourceName + "/" + partitionName);
            ZNRecord extViewRec = externalView.getRecord();
            partInfo.setSimpleFields(extViewRec.getSimpleFields());
            if (extViewRec.getMapField(partitionName) != null) {
                partInfo.setMapField(partitionName, extViewRec.getMapField(partitionName));
            }
            if (extViewRec.getListField(partitionName) != null) {
                partInfo.setListField(partitionName, extViewRec.getListField(partitionName));
            }

            System.out.println("ExternalView for " + resourceName + "/" + partitionName + ":");
            System.out.println(new String(new ZNRecordSerializer().serialize(partInfo)));
        } else {
            System.out.println("No externalView for " + resourceName + "/" + partitionName);
        }
        return 0;

    } else if (cmd.hasOption(enableInstance)) {
        String clusterName = cmd.getOptionValues(enableInstance)[0];
        String instanceName = cmd.getOptionValues(enableInstance)[1];
        if (instanceName.contains(":")) {
            instanceName = instanceName.replaceAll(":", "_");
        }
        boolean enabled = Boolean.parseBoolean(cmd.getOptionValues(enableInstance)[2].toLowerCase());

        setupTool.getClusterManagementTool().enableInstance(clusterName, instanceName, enabled);
        return 0;
    } else if (cmd.hasOption(enableResource)) {
        String clusterName = cmd.getOptionValues(enableResource)[0];
        String resourceName = cmd.getOptionValues(enableResource)[1];
        boolean enabled = Boolean.parseBoolean(cmd.getOptionValues(enableResource)[2].toLowerCase());
        setupTool.getClusterManagementTool().enableResource(clusterName, resourceName, enabled);
    } else if (cmd.hasOption(enablePartition)) {
        String[] args = cmd.getOptionValues(enablePartition);

        boolean enabled = Boolean.parseBoolean(args[0].toLowerCase());
        String clusterName = args[1];
        String instanceName = args[2];
        String resourceName = args[3];

        List<String> partitionNames = Arrays.asList(Arrays.copyOfRange(args, 4, args.length));
        setupTool.getClusterManagementTool().enablePartition(enabled, clusterName, instanceName, resourceName,
                partitionNames);
        return 0;
    } else if (cmd.hasOption(resetPartition)) {
        String[] args = cmd.getOptionValues(resetPartition);

        String clusterName = args[0];
        String instanceName = args[1];
        String resourceName = args[2];
        List<String> partitionNames = Arrays.asList(Arrays.copyOfRange(args, 3, args.length));

        setupTool.getClusterManagementTool().resetPartition(clusterName, instanceName, resourceName,
                partitionNames);
        return 0;
    } else if (cmd.hasOption(resetInstance)) {
        String[] args = cmd.getOptionValues(resetInstance);

        String clusterName = args[0];
        List<String> instanceNames = Arrays.asList(Arrays.copyOfRange(args, 1, args.length));

        setupTool.getClusterManagementTool().resetInstance(clusterName, instanceNames);
        return 0;
    } else if (cmd.hasOption(resetResource)) {
        String[] args = cmd.getOptionValues(resetResource);

        String clusterName = args[0];
        List<String> resourceNames = Arrays.asList(Arrays.copyOfRange(args, 1, args.length));

        setupTool.getClusterManagementTool().resetResource(clusterName, resourceNames);
        return 0;
    } else if (cmd.hasOption(enableCluster)) {
        String[] params = cmd.getOptionValues(enableCluster);
        String clusterName = params[0];
        boolean enabled = Boolean.parseBoolean(params[1].toLowerCase());
        setupTool.getClusterManagementTool().enableCluster(clusterName, enabled);

        return 0;
    } else if (cmd.hasOption(listStateModels)) {
        String clusterName = cmd.getOptionValues(listStateModels)[0];

        List<String> stateModels = setupTool.getClusterManagementTool().getStateModelDefs(clusterName);

        System.out.println("Existing state models:");
        for (String stateModel : stateModels) {
            System.out.println(stateModel);
        }
        return 0;
    } else if (cmd.hasOption(listStateModel)) {
        String clusterName = cmd.getOptionValues(listStateModel)[0];
        String stateModel = cmd.getOptionValues(listStateModel)[1];
        StateModelDefinition stateModelDef = setupTool.getClusterManagementTool().getStateModelDef(clusterName,
                stateModel);
        String result = new String(new ZNRecordSerializer().serialize(stateModelDef.getRecord()));
        System.out.println("StateModelDefinition: " + result);
        return 0;
    } else if (cmd.hasOption(addStateModelDef)) {
        String clusterName = cmd.getOptionValues(addStateModelDef)[0];
        String stateModelFile = cmd.getOptionValues(addStateModelDef)[1];

        ZNRecord stateModelRecord = (ZNRecord) (new ZNRecordSerializer().deserialize(readFile(stateModelFile)));
        if (stateModelRecord.getId() == null || stateModelRecord.getId().length() == 0) {
            throw new IllegalArgumentException("ZNRecord for state model definition must have an id");
        }
        setupTool.getClusterManagementTool().addStateModelDef(clusterName, stateModelRecord.getId(),
                new StateModelDefinition(stateModelRecord));
        return 0;
    } else if (cmd.hasOption(addIdealState)) {
        String clusterName = cmd.getOptionValues(addIdealState)[0];
        String resourceName = cmd.getOptionValues(addIdealState)[1];
        String idealStateFile = cmd.getOptionValues(addIdealState)[2];

        setupTool.addIdealState(clusterName, resourceName, idealStateFile);
        return 0;
    } else if (cmd.hasOption(dropResource)) {
        String clusterName = cmd.getOptionValues(dropResource)[0];
        String resourceName = cmd.getOptionValues(dropResource)[1];

        setupTool.getClusterManagementTool().dropResource(clusterName, resourceName);
    } else if (cmd.hasOption(swapInstance)) {
        String clusterName = cmd.getOptionValues(swapInstance)[0];
        String oldInstanceName = cmd.getOptionValues(swapInstance)[1];
        String newInstanceName = cmd.getOptionValues(swapInstance)[2];

        setupTool.swapInstance(clusterName, oldInstanceName, newInstanceName);
    }
    // set/get/remove config options
    else if (cmd.hasOption(setConfig)) {
        String values[] = cmd.getOptionValues(setConfig);
        ConfigScopeProperty type = ConfigScopeProperty.valueOf(values[0]);
        String scopeArgs = values[1];
        String keyValueMap = values[2];
        setupTool.setConfig(type, scopeArgs, keyValueMap);
    } else if (cmd.hasOption(getConfig)) {
        String values[] = cmd.getOptionValues(getConfig);
        ConfigScopeProperty type = ConfigScopeProperty.valueOf(values[0]);
        String scopeArgs = values[1];
        String keys = values[2];
        setupTool.getConfig(type, scopeArgs, keys);
    } else if (cmd.hasOption(removeConfig)) {
        String values[] = cmd.getOptionValues(removeConfig);
        ConfigScopeProperty type = ConfigScopeProperty.valueOf(values[0]);
        String scoepArgs = values[1];
        String keys = values[2];
        setupTool.removeConfig(type, scoepArgs, keys);
    }
    // set/get/remove constraint options
    else if (cmd.hasOption(setConstraint)) {
        String values[] = cmd.getOptionValues(setConstraint);
        String clusterName = values[0];
        String constraintType = values[1];
        String constraintId = values[2];
        String constraintAttributesMap = values[3];
        setupTool.setConstraint(clusterName, constraintType, constraintId, constraintAttributesMap);
    } else if (cmd.hasOption(getConstraints)) {
        String values[] = cmd.getOptionValues(getConstraints);
        String clusterName = values[0];
        String constraintType = values[1];
        setupTool.getConstraints(clusterName, constraintType);
    } else if (cmd.hasOption(removeConstraint)) {
        String values[] = cmd.getOptionValues(removeConstraint);
        String clusterName = values[0];
        String constraintType = values[1];
        String constraintId = values[2];
        setupTool.removeConstraint(clusterName, constraintType, constraintId);
    } else if (cmd.hasOption(addInstanceTag)) {
        String clusterName = cmd.getOptionValues(addInstanceTag)[0];
        String instanceName = cmd.getOptionValues(addInstanceTag)[1];
        String tag = cmd.getOptionValues(addInstanceTag)[2];
        setupTool.getClusterManagementTool().addInstanceTag(clusterName, instanceName, tag);
    } else if (cmd.hasOption(removeInstanceTag)) {
        String clusterName = cmd.getOptionValues(removeInstanceTag)[0];
        String instanceName = cmd.getOptionValues(removeInstanceTag)[1];
        String tag = cmd.getOptionValues(removeInstanceTag)[2];
        setupTool.getClusterManagementTool().removeInstanceTag(clusterName, instanceName, tag);
    }
    // help option
    else if (cmd.hasOption(help)) {
        printUsage(cliOptions);
        return 0;
    } else if (cmd.hasOption(addResourceProperty)) {
        String clusterName = cmd.getOptionValues(addResourceProperty)[0];
        String resourceName = cmd.getOptionValues(addResourceProperty)[1];
        String propertyKey = cmd.getOptionValues(addResourceProperty)[2];
        String propertyVal = cmd.getOptionValues(addResourceProperty)[3];

        setupTool.addResourceProperty(clusterName, resourceName, propertyKey, propertyVal);
        return 0;
    } else if (cmd.hasOption(removeResourceProperty)) {
        String clusterName = cmd.getOptionValues(removeResourceProperty)[0];
        String resourceName = cmd.getOptionValues(removeResourceProperty)[1];
        String propertyKey = cmd.getOptionValues(removeResourceProperty)[2];

        setupTool.removeResourceProperty(clusterName, resourceName, propertyKey);
        return 0;
    }
    return 0;
}

From source file:org.apache.helix.tools.IntegrationTestUtil.java

static void processCommandLineArgs(String[] cliArgs) {
    CommandLineParser cliParser = new GnuParser();
    Options cliOptions = constructCommandLineOptions();
    CommandLine cmd = null;
    try {//from   w ww .  j av a  2 s  . c o  m
        cmd = cliParser.parse(cliOptions, cliArgs);
    } catch (ParseException pe) {
        System.err.println("failed to parse command-line args: " + Arrays.asList(cliArgs) + ", exception: "
                + pe.toString());
        printUsage(cliOptions);
        System.exit(1);
    }

    String zkServer = cmd.getOptionValue(zkSvr);

    ZkClient zkclient = new ZkClient(zkServer, ZkClient.DEFAULT_SESSION_TIMEOUT,
            ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
    IntegrationTestUtil util = new IntegrationTestUtil(zkclient);

    if (cmd != null) {
        if (cmd.hasOption(verifyExternalView)) {
            String[] args = cmd.getOptionValues(verifyExternalView);
            util.verifyExternalView(args);
        } else if (cmd.hasOption(verifyLiveNodes)) {
            String[] args = cmd.getOptionValues(verifyLiveNodes);
            util.verifyLiveNodes(args);
        } else if (cmd.hasOption(readZNode)) {
            String path = cmd.getOptionValue(readZNode);
            util.readZNode(path);
        } else if (cmd.hasOption(readLeader)) {
            String clusterName = cmd.getOptionValue(readLeader);
            PropertyKey.Builder keyBuilder = new PropertyKey.Builder(clusterName);
            util.readZNode(keyBuilder.controllerLeader().getPath());
        } else {
            printUsage(cliOptions);
        }
    }
}

From source file:org.apache.helix.tools.ZkGrep.java

public static void processCommandLineArgs(String[] cliArgs) {
    CommandLineParser cliParser = new GnuParser();
    Options cliOptions = constructCommandLineOptions();
    CommandLine cmd = null;

    try {//from w  w  w.ja  va  2s  .com
        cmd = cliParser.parse(cliOptions, cliArgs);
    } catch (ParseException pe) {
        System.err.println("CommandLineClient: failed to parse command-line options: " + pe);
        printUsage(cliOptions);
        System.exit(1);
    }

    String zkCfgDirValue = null;
    String zkCfgFile = null;

    if (cmd.hasOption(zkCfg)) {
        zkCfgDirValue = cmd.getOptionValue(zkCfg);
    }

    if (zkCfgDirValue == null) {
        zkCfgDirValue = guessZkCfgDir();
    }

    if (zkCfgDirValue == null) {
        LOG.error("couldn't figure out path to zkCfg file");
        System.exit(1);
    }

    // get zoo.cfg path from cfg-dir
    zkCfgFile = zkCfgDirValue;
    if (!zkCfgFile.endsWith(".cfg")) {
        // append with default zoo.cfg
        zkCfgFile = zkCfgFile + "/zoo.cfg";
    }

    if (!new File(zkCfgFile).exists()) {
        LOG.error("zoo.cfg file doen't exist: " + zkCfgFile);
        System.exit(1);
    }

    String[] patterns = cmd.getOptionValues(pattern);

    String[] zkDataDirs = getZkDataDirs(zkCfgFile);

    // parse zk data files
    if (zkDataDirs == null || zkDataDirs[0] == null || zkDataDirs[1] == null) {
        LOG.error("invalid zkCfgDir: " + zkCfgDirValue);
        System.exit(1);
    }

    File zkParsedDir = new File(String.format("%s/zklog-parsed", System.getProperty("user.home")));
    if (!zkParsedDir.exists()) {
        LOG.info("creating zklog-parsed dir: " + zkParsedDir.getAbsolutePath());
        zkParsedDir.mkdir();
    }

    if (cmd.hasOption(between)) {
        String[] timeStrings = cmd.getOptionValues(between);

        long startTime = parseTimeString(timeStrings[0]);
        if (startTime == -1) {
            LOG.error("invalid start time string: " + timeStrings[0]
                    + ", should be either timestamp or yyMMdd_hhmmss_SSS");
            System.exit(1);
        }

        long endTime = parseTimeString(timeStrings[1]);
        if (endTime == -1) {
            LOG.error("invalid end time string: " + timeStrings[1]
                    + ", should be either timestamp or yyMMdd_hhmmss_SSS");
            System.exit(1);
        }

        if (startTime > endTime) {
            LOG.warn("empty window: " + startTime + " - " + endTime);
            System.exit(1);
        }
        // zkDataDirs[0] is the transaction log dir
        List<File> parsedZkLogs = parseZkLogs(zkDataDirs[0], startTime, endTime);
        grepZkLogDir(parsedZkLogs, startTime, endTime, patterns);

    } else if (cmd.hasOption(by)) {
        String timeString = cmd.getOptionValue(by);

        long byTime = parseTimeString(timeString);
        if (byTime == -1) {
            LOG.error("invalid by time string: " + timeString
                    + ", should be either timestamp or yyMMdd_hhmmss_SSS");
            System.exit(1);
        }

        // zkDataDirs[1] is the snapshot dir
        File[] lastZkSnapshot = parseZkSnapshot(zkDataDirs[1], byTime);

        // lastZkSnapshot[1] is the parsed last snapshot by byTime
        grepZkSnapshot(lastZkSnapshot[1], patterns);

        // need to grep transaction logs between last-modified-time of snapshot and byTime also
        // lastZkSnapshot[0] is the last snapshot by byTime
        long startTime = lastZkSnapshot[0].lastModified();

        // zkDataDirs[0] is the transaction log dir
        List<File> parsedZkLogs = parseZkLogs(zkDataDirs[0], startTime, byTime);
        grepZkLogDir(parsedZkLogs, startTime, byTime, patterns);
    }
}

From source file:org.apache.hive.beeline.BeeLine.java

int initArgsFromCliVars(String[] args) {
    List<String> commands = Collections.emptyList();

    CliOptionsProcessor optionsProcessor = new CliOptionsProcessor();
    if (!optionsProcessor.process(args)) {
        return 1;
    }/*from   ww  w  . jav a  2  s  .  com*/
    CommandLine commandLine = optionsProcessor.getCommandLine();

    Properties confProps = commandLine.getOptionProperties("hiveconf");
    for (String propKey : confProps.stringPropertyNames()) {
        setHiveConfVar(propKey, confProps.getProperty(propKey));
    }

    Properties hiveVars = commandLine.getOptionProperties("define");
    for (String propKey : hiveVars.stringPropertyNames()) {
        getOpts().getHiveConfVariables().put(propKey, hiveVars.getProperty(propKey));
    }

    Properties hiveVars2 = commandLine.getOptionProperties("hivevar");
    for (String propKey : hiveVars2.stringPropertyNames()) {
        getOpts().getHiveConfVariables().put(propKey, hiveVars2.getProperty(propKey));
    }

    getOpts().setScriptFile(commandLine.getOptionValue("f"));

    if (commandLine.getOptionValues("i") != null) {
        getOpts().setInitFiles(commandLine.getOptionValues("i"));
    }

    dbName = commandLine.getOptionValue("database");
    getOpts().setVerbose(Boolean.parseBoolean(commandLine.getOptionValue("verbose")));
    getOpts().setSilent(Boolean.parseBoolean(commandLine.getOptionValue("slient")));

    int code = 0;
    if (commandLine.getOptionValues("e") != null) {
        commands = Arrays.asList(commandLine.getOptionValues("e"));
    }

    if (!commands.isEmpty() && getOpts().getScriptFile() != null) {
        System.err.println("The '-e' and '-f' options cannot be specified simultaneously");
        optionsProcessor.printCliUsage();
        return 1;
    }

    if (!commands.isEmpty()) {
        embeddedConnect();
        connectDBInEmbededMode();
        for (Iterator<String> i = commands.iterator(); i.hasNext();) {
            String command = i.next().toString();
            debug(loc("executing-command", command));
            if (!dispatch(command)) {
                code++;
            }
        }
        exit = true; // execute and exit
    }
    return code;
}

From source file:org.apache.hive.beeline.BeeLine.java

int initArgs(String[] args) {
    List<String> commands = Collections.emptyList();

    CommandLine cl;
    BeelineParser beelineParser;/*from  www  . j  a va  2  s  .com*/

    try {
        beelineParser = new BeelineParser();
        cl = beelineParser.parse(options, args);
    } catch (ParseException e1) {
        output(e1.getMessage());
        usage();
        return -1;
    }

    boolean connSuccessful = connectUsingArgs(beelineParser, cl);
    // checks if default hs2 connection configuration file is present
    // and uses it to connect if found
    // no-op if the file is not present
    if (!connSuccessful && !exit) {
        connSuccessful = defaultBeelineConnect();
    }

    int code = 0;
    if (cl.getOptionValues('e') != null) {
        commands = Arrays.asList(cl.getOptionValues('e'));
    }

    if (!commands.isEmpty() && getOpts().getScriptFile() != null) {
        error("The '-e' and '-f' options cannot be specified simultaneously");
        return 1;
    } else if (!commands.isEmpty() && !connSuccessful) {
        error("Cannot run commands specified using -e. No current connection");
        return 1;
    }
    if (!commands.isEmpty()) {
        for (Iterator<String> i = commands.iterator(); i.hasNext();) {
            String command = i.next().toString();
            debug(loc("executing-command", command));
            if (!dispatch(command)) {
                code++;
            }
        }
        exit = true; // execute and exit
    }
    return code;
}

From source file:org.apache.hive.beeline.BeeLine.java

private boolean connectUsingArgs(BeelineParser beelineParser, CommandLine cl) {
    String driver = null, user = null, pass = "", url = null;
    String auth = null;/*from   w w  w .  j a v a  2 s  .c  o  m*/

    if (cl.hasOption("help")) {
        usage();
        getOpts().setHelpAsked(true);
        return true;
    }

    Properties hiveVars = cl.getOptionProperties("hivevar");
    for (String key : hiveVars.stringPropertyNames()) {
        getOpts().getHiveVariables().put(key, hiveVars.getProperty(key));
    }

    Properties hiveConfs = cl.getOptionProperties("hiveconf");
    for (String key : hiveConfs.stringPropertyNames()) {
        setHiveConfVar(key, hiveConfs.getProperty(key));
    }

    driver = cl.getOptionValue("d");
    auth = cl.getOptionValue("a");
    user = cl.getOptionValue("n");
    getOpts().setAuthType(auth);
    if (cl.hasOption("w")) {
        pass = obtainPasswordFromFile(cl.getOptionValue("w"));
    } else {
        if (beelineParser.isPasswordOptionSet) {
            pass = cl.getOptionValue("p");
        }
    }
    url = cl.getOptionValue("u");
    if ((url == null) && cl.hasOption("reconnect")) {
        // If url was not specified with -u, but -r was present, use that.
        url = getOpts().getLastConnectedUrl();
    }
    getOpts().setInitFiles(cl.getOptionValues("i"));
    getOpts().setScriptFile(cl.getOptionValue("f"));

    if (url != null) {
        String com;
        String comForDebug;
        if (pass != null) {
            com = constructCmd(url, user, pass, driver, false);
            comForDebug = constructCmd(url, user, pass, driver, true);
        } else {
            com = constructCmdUrl(url, user, driver, false);
            comForDebug = constructCmdUrl(url, user, driver, true);
        }
        debug(comForDebug);
        return dispatch(com);
    }
    // load property file
    String propertyFile = cl.getOptionValue("property-file");
    if (propertyFile != null) {
        try {
            this.consoleReader = new ConsoleReader();
        } catch (IOException e) {
            handleException(e);
        }
        if (!dispatch("!properties " + propertyFile)) {
            exit = true;
            return false;
        }
    }
    return false;
}

From source file:org.apache.hive.hcatalog.streaming.StreamingIntegrationTester.java

public static void main(String[] args) {

    try {//from  w  ww .ja  v a  2  s. co  m
        LogUtils.initHiveLog4j();
    } catch (LogUtils.LogInitializationException e) {
        System.err.println("Unable to initialize log4j " + StringUtils.stringifyException(e));
        System.exit(-1);
    }

    Options options = new Options();

    options.addOption(OptionBuilder.hasArg().withArgName("abort-pct")
            .withDescription("Percentage of transactions to abort, defaults to 5").withLongOpt("abortpct")
            .create('a'));

    options.addOption(OptionBuilder.hasArgs().withArgName("column-names")
            .withDescription("column names of table to write to").withLongOpt("columns").withValueSeparator(',')
            .isRequired().create('c'));

    options.addOption(OptionBuilder.hasArg().withArgName("database")
            .withDescription("Database of table to write to").withLongOpt("database").isRequired().create('d'));

    options.addOption(OptionBuilder.hasArg().withArgName("frequency")
            .withDescription("How often to commit a transaction, in seconds, defaults to 1")
            .withLongOpt("frequency").create('f'));

    options.addOption(OptionBuilder.hasArg().withArgName("iterations")
            .withDescription("Number of batches to write, defaults to 10").withLongOpt("num-batches")
            .create('i'));

    options.addOption(OptionBuilder.hasArg().withArgName("metastore-uri")
            .withDescription("URI of Hive metastore").withLongOpt("metastore-uri").isRequired().create('m'));

    options.addOption(OptionBuilder.hasArg().withArgName("num_transactions")
            .withDescription("Number of transactions per batch, defaults to 100").withLongOpt("num-txns")
            .create('n'));

    options.addOption(OptionBuilder.hasArgs().withArgName("partition-values")
            .withDescription("partition values, must be provided in order of partition columns, "
                    + "if not provided table is assumed to not be partitioned")
            .withLongOpt("partition").withValueSeparator(',').create('p'));

    options.addOption(OptionBuilder.hasArg().withArgName("records-per-transaction")
            .withDescription("records to write in each transaction, defaults to 100")
            .withLongOpt("records-per-txn").withValueSeparator(',').create('r'));

    options.addOption(OptionBuilder.hasArgs().withArgName("column-types")
            .withDescription("column types, valid values are string, int, float, decimal, date, " + "datetime")
            .withLongOpt("schema").withValueSeparator(',').isRequired().create('s'));

    options.addOption(OptionBuilder.hasArg().withArgName("table").withDescription("Table to write to")
            .withLongOpt("table").isRequired().create('t'));

    options.addOption(OptionBuilder.hasArg().withArgName("num-writers")
            .withDescription("Number of writers to create, defaults to 2").withLongOpt("writers").create('w'));

    options.addOption(OptionBuilder.hasArg(false).withArgName("pause")
            .withDescription("Wait on keyboard input after commit & batch close. default: disabled")
            .withLongOpt("pause").create('x'));

    Parser parser = new GnuParser();
    CommandLine cmdline = null;
    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        usage(options);
    }

    boolean pause = cmdline.hasOption('x');
    String db = cmdline.getOptionValue('d');
    String table = cmdline.getOptionValue('t');
    String uri = cmdline.getOptionValue('m');
    int txnsPerBatch = Integer.parseInt(cmdline.getOptionValue('n', "100"));
    int writers = Integer.parseInt(cmdline.getOptionValue('w', "2"));
    int batches = Integer.parseInt(cmdline.getOptionValue('i', "10"));
    int recordsPerTxn = Integer.parseInt(cmdline.getOptionValue('r', "100"));
    int frequency = Integer.parseInt(cmdline.getOptionValue('f', "1"));
    int ap = Integer.parseInt(cmdline.getOptionValue('a', "5"));
    float abortPct = ((float) ap) / 100.0f;
    String[] partVals = cmdline.getOptionValues('p');
    String[] cols = cmdline.getOptionValues('c');
    String[] types = cmdline.getOptionValues('s');

    StreamingIntegrationTester sit = new StreamingIntegrationTester(db, table, uri, txnsPerBatch, writers,
            batches, recordsPerTxn, frequency, abortPct, partVals, cols, types, pause);
    sit.go();
}

From source file:org.apache.hive.ptest.execution.PTest.java

public static void main(String[] args) throws Exception {
    LOG.info("Args " + Arrays.toString(args));
    CommandLineParser parser = new GnuParser();
    Options options = new Options();
    options.addOption(null, PROPERTIES, true, "properties file");
    options.addOption(null, REPOSITORY, true, "Overrides git repository in properties file");
    options.addOption(null, REPOSITORY_NAME, true, "Overrides git repository *name* in properties file");
    options.addOption(null, BRANCH, true, "Overrides git branch in properties file");
    options.addOption(null, PATCH, true, "URI to patch, either file:/// or http(s)://");
    options.addOption(ANT_ARG, null, true, "Supplemental ant arguments");
    options.addOption(null, JAVA_HOME, true,
            "Java Home for compiling and running tests (unless " + JAVA_HOME_TEST + " is specified)");
    options.addOption(null, JAVA_HOME_TEST, true, "Java Home for running tests (optional)");
    options.addOption(null, ANT_TEST_ARGS, true, "Arguments to ant test on slave nodes only");
    options.addOption(null, ANT_ENV_OPTS, true, "ANT_OPTS environment variable setting");
    CommandLine commandLine = parser.parse(options, args);
    if (!commandLine.hasOption(PROPERTIES)) {
        throw new IllegalArgumentException(
                Joiner.on(" ").join(PTest.class.getName(), "--" + PROPERTIES, "config.properties"));
    }//  w w w .ja v  a 2 s.co m
    String testConfigurationFile = commandLine.getOptionValue(PROPERTIES);
    ExecutionContextConfiguration executionContextConfiguration = ExecutionContextConfiguration
            .fromFile(testConfigurationFile);
    String buildTag = System.getenv("BUILD_TAG") == null ? "undefined-" + System.currentTimeMillis()
            : System.getenv("BUILD_TAG");
    File logDir = Dirs.create(new File(executionContextConfiguration.getGlobalLogDirectory(), buildTag));
    LogDirectoryCleaner cleaner = new LogDirectoryCleaner(
            new File(executionContextConfiguration.getGlobalLogDirectory()), 5);
    cleaner.setName("LogCleaner-" + executionContextConfiguration.getGlobalLogDirectory());
    cleaner.setDaemon(true);
    cleaner.start();
    TestConfiguration conf = TestConfiguration.fromFile(testConfigurationFile, LOG);
    String repository = Strings.nullToEmpty(commandLine.getOptionValue(REPOSITORY)).trim();
    if (!repository.isEmpty()) {
        conf.setRepository(repository);
    }
    String repositoryName = Strings.nullToEmpty(commandLine.getOptionValue(REPOSITORY_NAME)).trim();
    if (!repositoryName.isEmpty()) {
        conf.setRepositoryName(repositoryName);
    }
    String branch = Strings.nullToEmpty(commandLine.getOptionValue(BRANCH)).trim();
    if (!branch.isEmpty()) {
        conf.setBranch(branch);
    }
    String patch = Strings.nullToEmpty(commandLine.getOptionValue(PATCH)).trim();
    if (!patch.isEmpty()) {
        conf.setPatch(patch);
    }
    String javaHome = Strings.nullToEmpty(commandLine.getOptionValue(JAVA_HOME)).trim();
    if (!javaHome.isEmpty()) {
        conf.setJavaHome(javaHome);
    }
    String javaHomeForTests = Strings.nullToEmpty(commandLine.getOptionValue(JAVA_HOME_TEST)).trim();
    if (!javaHomeForTests.isEmpty()) {
        conf.setJavaHomeForTests(javaHomeForTests);
    }
    String antTestArgs = Strings.nullToEmpty(commandLine.getOptionValue(ANT_TEST_ARGS)).trim();
    if (!antTestArgs.isEmpty()) {
        conf.setAntTestArgs(antTestArgs);
    }
    String antEnvOpts = Strings.nullToEmpty(commandLine.getOptionValue(ANT_ENV_OPTS)).trim();
    if (!antEnvOpts.isEmpty()) {
        conf.setAntEnvOpts(antEnvOpts);
    }
    String antTestTarget = Strings.nullToEmpty(commandLine.getOptionValue(ANT_TEST_TARGET)).trim();
    if (!antTestTarget.isEmpty()) {
        conf.setAntTestTarget(antTestTarget);
    }
    String[] supplementalAntArgs = commandLine.getOptionValues(ANT_ARG);
    if (supplementalAntArgs != null && supplementalAntArgs.length > 0) {
        String antArgs = Strings.nullToEmpty(conf.getAntArgs());
        if (!(antArgs.isEmpty() || antArgs.endsWith(" "))) {
            antArgs += " ";
        }
        antArgs += "-" + ANT_ARG + Joiner.on(" -" + ANT_ARG).join(supplementalAntArgs);
        conf.setAntArgs(antArgs);
    }
    ExecutionContextProvider executionContextProvider = null;
    ExecutionContext executionContext = null;
    int exitCode = 0;
    try {
        executionContextProvider = executionContextConfiguration.getExecutionContextProvider();
        executionContext = executionContextProvider.createExecutionContext();
        LocalCommandFactory localCommandFactory = new LocalCommandFactory(LOG);
        PTest ptest = new PTest(conf, executionContext, buildTag, logDir, localCommandFactory,
                new SSHCommandExecutor(LOG, localCommandFactory, conf.getSshOpts()),
                new RSyncCommandExecutor(LOG, 10, localCommandFactory), LOG);
        exitCode = ptest.run();
    } finally {
        if (executionContext != null) {
            executionContext.terminate();
        }
        if (executionContextProvider != null) {
            executionContextProvider.close();
        }
    }
    System.exit(exitCode);
}