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);
}
}
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);
}
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);
}
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);
}
}
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);
}
}
Aggregations