Search in sources :

Example 1 with ExportStrategy

use of org.apache.rya.api.client.CreatePCJ.ExportStrategy in project incubator-rya by apache.

the class RyaAdminCommands method createPcj.

@CliCommand(value = CREATE_PCJ_CMD, help = "Creates and starts the maintenance of a new PCJ using a Fluo application.")
public String createPcj(@CliOption(key = { "exportToRya" }, mandatory = false, help = "Indicates that results for the query should be exported to a Rya PCJ table.", unspecifiedDefaultValue = "false", specifiedDefaultValue = "true") final boolean exportToRya, @CliOption(key = { "exportToKafka" }, mandatory = false, help = "Indicates that results for the query should be exported to a Kafka Topic.", unspecifiedDefaultValue = "false", specifiedDefaultValue = "true") final boolean exportToKafka) {
    // Fetch the command that is connected to the store.
    final ShellState shellState = state.getShellState();
    final RyaClient commands = shellState.getConnectedCommands().get();
    final String ryaInstance = shellState.getRyaInstanceName().get();
    try {
        final Set<ExportStrategy> strategies = new HashSet<>();
        if (exportToRya) {
            strategies.add(ExportStrategy.RYA);
        }
        if (exportToKafka) {
            strategies.add(ExportStrategy.KAFKA);
        }
        if (strategies.isEmpty()) {
            return "The user must specify at least one export strategy: (--exportToRya, --exportToKafka)";
        }
        // Prompt the user for the SPARQL.
        final Optional<String> sparql = sparqlPrompt.getSparql();
        if (sparql.isPresent()) {
            // Execute the command.
            final String pcjId = commands.getCreatePCJ().createPCJ(ryaInstance, sparql.get(), strategies);
            // Return a message that indicates the ID of the newly created ID.
            return String.format("The PCJ has been created. Its ID is '%s'.", pcjId);
        } else {
            // user aborted the SPARQL prompt.
            return "";
        }
    } catch (final InstanceDoesNotExistException e) {
        throw new RuntimeException(String.format("A Rya instance named '%s' does not exist.", ryaInstance), e);
    } catch (final IOException | RyaClientException e) {
        throw new RuntimeException("Could not create the PCJ. Provided reasons: " + e.getMessage(), e);
    }
}
Also used : ExportStrategy(org.apache.rya.api.client.CreatePCJ.ExportStrategy) RyaClientException(org.apache.rya.api.client.RyaClientException) ShellState(org.apache.rya.shell.SharedShellState.ShellState) RyaClient(org.apache.rya.api.client.RyaClient) InstanceDoesNotExistException(org.apache.rya.api.client.InstanceDoesNotExistException) IOException(java.io.IOException) HashSet(java.util.HashSet) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 2 with ExportStrategy

use of org.apache.rya.api.client.CreatePCJ.ExportStrategy in project incubator-rya by apache.

the class RyaAdminCommandsTest method createPCJ.

@Test
public void createPCJ() throws InstanceDoesNotExistException, RyaClientException, IOException {
    // Mock the object that performs the create operation.
    final String instanceName = "unitTest";
    final String sparql = "SELECT * WHERE { ?person <http://isA> ?noun }";
    final String pcjId = "123412342";
    final CreatePCJ mockCreatePCJ = mock(CreatePCJ.class);
    final Set<ExportStrategy> strategies = Sets.newHashSet(ExportStrategy.RYA);
    when(mockCreatePCJ.createPCJ(eq(instanceName), eq(sparql), eq(strategies))).thenReturn(pcjId);
    final RyaClient mockCommands = mock(RyaClient.class);
    when(mockCommands.getCreatePCJ()).thenReturn(mockCreatePCJ);
    final SharedShellState state = new SharedShellState();
    state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands);
    state.connectedToInstance(instanceName);
    final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class);
    when(mockSparqlPrompt.getSparql()).thenReturn(Optional.of(sparql));
    // Execute the command.
    final RyaAdminCommands commands = new RyaAdminCommands(state, mock(InstallPrompt.class), mockSparqlPrompt, mock(UninstallPrompt.class));
    final String message = commands.createPcj(true, false);
    // Verify the values that were provided to the command were passed through to CreatePCJ.
    verify(mockCreatePCJ).createPCJ(eq(instanceName), eq(sparql), eq(strategies));
    // Verify a message is returned that explains what was created.
    final String expected = "The PCJ has been created. Its ID is '123412342'.";
    assertEquals(expected, message);
}
Also used : ExportStrategy(org.apache.rya.api.client.CreatePCJ.ExportStrategy) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) CreatePCJ(org.apache.rya.api.client.CreatePCJ) RyaClient(org.apache.rya.api.client.RyaClient) InstallPrompt(org.apache.rya.shell.util.InstallPrompt) UninstallPrompt(org.apache.rya.shell.util.UninstallPrompt) Test(org.junit.Test)

Example 3 with ExportStrategy

use of org.apache.rya.api.client.CreatePCJ.ExportStrategy in project incubator-rya by apache.

the class FluoQueryMetadataDAO method readQueryMetadataBuilder.

private QueryMetadata.Builder readQueryMetadataBuilder(final SnapshotBase sx, final String nodeId) {
    requireNonNull(sx);
    requireNonNull(nodeId);
    // Fetch the values from the Fluo table.
    final String rowId = nodeId;
    final Map<Column, String> values = sx.gets(rowId, FluoQueryColumns.QUERY_VARIABLE_ORDER, FluoQueryColumns.QUERY_SPARQL, FluoQueryColumns.QUERY_TYPE, FluoQueryColumns.QUERY_EXPORT_STRATEGIES, FluoQueryColumns.QUERY_CHILD_NODE_ID);
    // Return an object holding them.
    final String varOrderString = values.get(FluoQueryColumns.QUERY_VARIABLE_ORDER);
    final VariableOrder varOrder = new VariableOrder(varOrderString);
    final String sparql = values.get(FluoQueryColumns.QUERY_SPARQL);
    final String childNodeId = values.get(FluoQueryColumns.QUERY_CHILD_NODE_ID);
    final String queryType = values.get(FluoQueryColumns.QUERY_TYPE);
    final String[] exportStrategies = values.get(FluoQueryColumns.QUERY_EXPORT_STRATEGIES).split(IncrementalUpdateConstants.VAR_DELIM);
    final Set<ExportStrategy> strategies = new HashSet<>();
    for (final String strategy : exportStrategies) {
        if (!strategy.isEmpty()) {
            strategies.add(ExportStrategy.valueOf(strategy));
        }
    }
    return QueryMetadata.builder(nodeId).setVarOrder(varOrder).setSparql(sparql).setExportStrategies(strategies).setQueryType(QueryType.valueOf(queryType)).setChildNodeId(childNodeId);
}
Also used : ExportStrategy(org.apache.rya.api.client.CreatePCJ.ExportStrategy) Column(org.apache.fluo.api.data.Column) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) HashSet(java.util.HashSet)

Example 4 with ExportStrategy

use of org.apache.rya.api.client.CreatePCJ.ExportStrategy in project incubator-rya by apache.

the class ExporterManager method exportBindingSet.

/**
 * Exports BindingSet using the exporters for a given {@link QueryType}.
 * @param exporters - exporters corresponding to a given queryType
 * @param strategies - export strategies used to export results (possibly a subset of those in the exporters map)
 * @param pcjId - id of the query whose results are being exported
 * @param data - serialized BindingSet result
 * @throws ResultExportException
 */
private void exportBindingSet(final Map<ExportStrategy, IncrementalResultExporter> exporters, final Set<ExportStrategy> strategies, final String pcjId, final Bytes data) throws ResultExportException {
    VisibilityBindingSet bs;
    try {
        bs = BS_SERDE.deserialize(data);
        simplifyVisibilities(bs);
    } catch (final Exception e) {
        throw new ResultExportException("Unable to deserialize the given BindingSet.", e);
    }
    try {
        for (final ExportStrategy strategy : strategies) {
            final IncrementalBindingSetExporter exporter = (IncrementalBindingSetExporter) exporters.get(strategy);
            exporter.export(pcjId, bs);
        }
    } catch (final Exception e) {
        throw new ResultExportException("Unable to export the given BindingSet " + bs + " with the given set of ExportStrategies " + strategies, e);
    }
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ExportStrategy(org.apache.rya.api.client.CreatePCJ.ExportStrategy) ResultExportException(org.apache.rya.indexing.pcj.fluo.app.export.IncrementalBindingSetExporter.ResultExportException) ResultExportException(org.apache.rya.indexing.pcj.fluo.app.export.IncrementalBindingSetExporter.ResultExportException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with ExportStrategy

use of org.apache.rya.api.client.CreatePCJ.ExportStrategy in project incubator-rya by apache.

the class ExporterManager method exportSubGraph.

/**
 * Exports RyaSubGraph using the exporters for a given {@link QueryType}.
 * @param exporters - exporters corresponding to a given queryType
 * @param strategies - export strategies used to export results (possibly a subset of those in the exporters map)
 * @param pcjId - id of the query whose results are being exported
 * @param data - serialized RyaSubGraph result
 * @throws ResultExportException
 */
private void exportSubGraph(final Map<ExportStrategy, IncrementalResultExporter> exporters, final Set<ExportStrategy> strategies, final String pcjId, final Bytes data) throws ResultExportException {
    final RyaSubGraph subGraph = SG_SERDE.fromBytes(data.toArray());
    try {
        simplifyVisibilities(subGraph);
    } catch (final UnsupportedEncodingException e) {
        throw new ResultExportException("Undable to deserialize provided RyaSubgraph", e);
    }
    try {
        for (final ExportStrategy strategy : strategies) {
            final IncrementalRyaSubGraphExporter exporter = (IncrementalRyaSubGraphExporter) exporters.get(strategy);
            exporter.export(pcjId, subGraph);
        }
    } catch (final Exception e) {
        throw new ResultExportException("Unable to export the given subgraph " + subGraph + " using all of the ExportStrategies " + strategies);
    }
}
Also used : RyaSubGraph(org.apache.rya.api.domain.RyaSubGraph) ExportStrategy(org.apache.rya.api.client.CreatePCJ.ExportStrategy) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResultExportException(org.apache.rya.indexing.pcj.fluo.app.export.IncrementalBindingSetExporter.ResultExportException) ResultExportException(org.apache.rya.indexing.pcj.fluo.app.export.IncrementalBindingSetExporter.ResultExportException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

ExportStrategy (org.apache.rya.api.client.CreatePCJ.ExportStrategy)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashSet (java.util.HashSet)2 RyaClient (org.apache.rya.api.client.RyaClient)2 ResultExportException (org.apache.rya.indexing.pcj.fluo.app.export.IncrementalBindingSetExporter.ResultExportException)2 IOException (java.io.IOException)1 Column (org.apache.fluo.api.data.Column)1 CreatePCJ (org.apache.rya.api.client.CreatePCJ)1 InstanceDoesNotExistException (org.apache.rya.api.client.InstanceDoesNotExistException)1 RyaClientException (org.apache.rya.api.client.RyaClientException)1 AccumuloConnectionDetails (org.apache.rya.api.client.accumulo.AccumuloConnectionDetails)1 RyaSubGraph (org.apache.rya.api.domain.RyaSubGraph)1 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)1 VariableOrder (org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder)1 ShellState (org.apache.rya.shell.SharedShellState.ShellState)1 InstallPrompt (org.apache.rya.shell.util.InstallPrompt)1 SparqlPrompt (org.apache.rya.shell.util.SparqlPrompt)1 UninstallPrompt (org.apache.rya.shell.util.UninstallPrompt)1 Test (org.junit.Test)1 CliCommand (org.springframework.shell.core.annotation.CliCommand)1