Search in sources :

Example 6 with Table

use of co.cask.cdap.cli.util.table.Table in project cdap by caskdata.

the class ExecuteQueryCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String query = arguments.get(ArgumentName.QUERY.toString());
    long timeOutMins = arguments.getLongOptional(ArgumentName.TIMEOUT.toString(), DEFAULT_TIMEOUT_MIN);
    ListenableFuture<ExploreExecutionResult> future = queryClient.execute(cliConfig.getCurrentNamespace(), query);
    try {
        ExploreExecutionResult executionResult = future.get(timeOutMins, TimeUnit.MINUTES);
        if (!executionResult.canContainResults()) {
            output.println("SQL statement does not output any result.");
            executionResult.close();
            return;
        }
        final List<ColumnDesc> schema = executionResult.getResultSchema();
        String[] header = new String[schema.size()];
        for (int i = 0; i < header.length; i++) {
            ColumnDesc column = schema.get(i);
            // Hive columns start at 1
            int index = column.getPosition() - 1;
            header[index] = column.getName() + ": " + column.getType();
        }
        List<QueryResult> rows = Lists.newArrayList(executionResult);
        executionResult.close();
        QueryStatus.OpStatus opStatus = executionResult.getStatus().getStatus();
        if (opStatus != QueryStatus.OpStatus.FINISHED) {
            throw new SQLException(String.format("Query '%s' execution did not finish successfully. " + "Got final state - %s", query, opStatus));
        }
        Table table = Table.builder().setHeader(header).setRows(rows, new RowMaker<QueryResult>() {

            @Override
            public List<?> makeRow(QueryResult object) {
                return object.getColumns();
            }
        }).build();
        cliConfig.getTableRenderer().render(cliConfig, output, table);
        output.printf("Fetched %d rows", rows.size()).println();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (ExecutionException e) {
        Throwable t = Throwables.getRootCause(e);
        if (t instanceof HandleNotFoundException) {
            throw Throwables.propagate(t);
        }
        throw new SQLException(Throwables.getRootCause(e));
    } catch (CancellationException e) {
        throw new RuntimeException("Query has been cancelled on ListenableFuture object.");
    } catch (TimeoutException e) {
        output.println("Couldn't obtain results after " + timeOutMins + "mins.");
    }
}
Also used : Table(co.cask.cdap.cli.util.table.Table) SQLException(java.sql.SQLException) RowMaker(co.cask.cdap.cli.util.RowMaker) ColumnDesc(co.cask.cdap.proto.ColumnDesc) QueryStatus(co.cask.cdap.proto.QueryStatus) HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) QueryResult(co.cask.cdap.proto.QueryResult) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) ExploreExecutionResult(co.cask.cdap.explore.client.ExploreExecutionResult) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with Table

use of co.cask.cdap.cli.util.table.Table in project cdap by caskdata.

the class DescribeNamespaceCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    NamespaceId namespace = new NamespaceId(arguments.get(ArgumentName.NAMESPACE_NAME.getName()));
    NamespaceMeta namespaceMeta = namespaceClient.get(namespace);
    Table table = Table.builder().setHeader("name", "description", "config").setRows(Lists.newArrayList(namespaceMeta), new RowMaker<NamespaceMeta>() {

        @Override
        public List<?> makeRow(NamespaceMeta object) {
            return Lists.newArrayList(object.getName(), object.getDescription(), NamespaceCommandUtils.prettyPrintNamespaceConfigCLI(object.getConfig()));
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, output, table);
}
Also used : Table(co.cask.cdap.cli.util.table.Table) RowMaker(co.cask.cdap.cli.util.RowMaker) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) NamespaceId(co.cask.cdap.proto.id.NamespaceId)

Example 8 with Table

use of co.cask.cdap.cli.util.table.Table in project cdap by caskdata.

the class ListArtifactPluginTypesCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String artifactName = arguments.get(ArgumentName.ARTIFACT_NAME.toString());
    String artifactVersion = arguments.get(ArgumentName.ARTIFACT_VERSION.toString());
    ArtifactId artifactId = cliConfig.getCurrentNamespace().artifact(artifactName, artifactVersion);
    List<String> types;
    String scopeStr = arguments.getOptional(ArgumentName.SCOPE.toString());
    if (scopeStr == null) {
        types = artifactClient.getPluginTypes(artifactId);
    } else {
        types = artifactClient.getPluginTypes(artifactId, ArtifactScope.valueOf(scopeStr.toUpperCase()));
    }
    Table table = Table.builder().setHeader("plugin type").setRows(types, new RowMaker<String>() {

        @Override
        public List<?> makeRow(String object) {
            return Lists.newArrayList(object);
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, output, table);
}
Also used : Table(co.cask.cdap.cli.util.table.Table) ArtifactId(co.cask.cdap.proto.id.ArtifactId) RowMaker(co.cask.cdap.cli.util.RowMaker)

Example 9 with Table

use of co.cask.cdap.cli.util.table.Table in project cdap by caskdata.

the class ListArtifactVersionsCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String scopeStr = arguments.getOptional(ArgumentName.SCOPE.toString());
    String artifactName = arguments.get(ArgumentName.ARTIFACT_NAME.toString());
    List<ArtifactSummary> artifactSummaries;
    if (scopeStr == null) {
        artifactSummaries = artifactClient.listVersions(cliConfig.getCurrentNamespace(), artifactName);
    } else {
        ArtifactScope scope = ArtifactScope.valueOf(scopeStr.toUpperCase());
        artifactSummaries = artifactClient.listVersions(cliConfig.getCurrentNamespace(), artifactName, scope);
    }
    Table table = Table.builder().setHeader("name", "version", "scope").setRows(artifactSummaries, new RowMaker<ArtifactSummary>() {

        @Override
        public List<?> makeRow(ArtifactSummary object) {
            return Lists.newArrayList(object.getName(), object.getVersion(), object.getScope().name());
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, output, table);
}
Also used : ArtifactScope(co.cask.cdap.api.artifact.ArtifactScope) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) Table(co.cask.cdap.cli.util.table.Table) RowMaker(co.cask.cdap.cli.util.RowMaker)

Example 10 with Table

use of co.cask.cdap.cli.util.table.Table in project cdap by caskdata.

the class ListArtifactsCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    List<ArtifactSummary> artifactSummaries;
    String artifactScope = arguments.getOptional(ArgumentName.SCOPE.toString());
    if (artifactScope == null) {
        artifactSummaries = artifactClient.list(cliConfig.getCurrentNamespace());
    } else {
        artifactSummaries = artifactClient.list(cliConfig.getCurrentNamespace(), ArtifactScope.valueOf(artifactScope.toUpperCase()));
    }
    Table table = Table.builder().setHeader("name", "version", "scope").setRows(artifactSummaries, new RowMaker<ArtifactSummary>() {

        @Override
        public List<?> makeRow(ArtifactSummary object) {
            return Lists.newArrayList(object.getName(), object.getVersion(), object.getScope().name());
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, output, table);
}
Also used : ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) Table(co.cask.cdap.cli.util.table.Table) RowMaker(co.cask.cdap.cli.util.RowMaker)

Aggregations

Table (co.cask.cdap.cli.util.table.Table)48 RowMaker (co.cask.cdap.cli.util.RowMaker)38 CommandInputError (co.cask.cdap.cli.exception.CommandInputError)7 List (java.util.List)7 ArtifactId (co.cask.cdap.proto.id.ArtifactId)5 StreamId (co.cask.cdap.proto.id.StreamId)5 Nullable (javax.annotation.Nullable)5 ArtifactScope (co.cask.cdap.api.artifact.ArtifactScope)3 DatasetModuleMeta (co.cask.cdap.proto.DatasetModuleMeta)3 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)3 ProgramRecord (co.cask.cdap.proto.ProgramRecord)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3 DatasetId (co.cask.cdap.proto.id.DatasetId)3 EntityId (co.cask.cdap.proto.id.EntityId)3 ProgramId (co.cask.cdap.proto.id.ProgramId)3 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)3 Map (java.util.Map)3 ArtifactInfo (co.cask.cdap.api.artifact.ArtifactInfo)2 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)2 BatchProgramResult (co.cask.cdap.proto.BatchProgramResult)2