use of org.apache.rya.api.client.RyaClient in project incubator-rya by apache.
the class RyaAdminCommands method printInstanceDetails.
@CliCommand(value = PRINT_INSTANCE_DETAILS_CMD, help = "Print information about how the Rya instance is configured.")
public String printInstanceDetails() {
// 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 Optional<RyaDetails> details = commands.getGetInstanceDetails().getDetails(ryaInstance);
if (details.isPresent()) {
return new RyaDetailsFormatter().format(shellState.getStorageType().get(), details.get());
} else {
return "This instance of Rya does not have a Rya Details table. Consider migrating to a newer version of Rya.";
}
} catch (final InstanceDoesNotExistException e) {
throw new RuntimeException(String.format("A Rya instance named '%s' does not exist.", ryaInstance), e);
} catch (final RyaClientException e) {
throw new RuntimeException("Could not get the instance details. Reason: " + e.getMessage(), e);
}
}
use of org.apache.rya.api.client.RyaClient in project incubator-rya by apache.
the class RyaAdminCommands method addUser.
@CliCommand(value = ADD_USER_CMD, help = "Adds an authorized user to the Rya instance.")
public void addUser(@CliOption(key = { "username" }, mandatory = true, help = "The username of the user that will be granted access.") final String username) {
// Fetch the Rya client that is connected to the store.
final ShellState shellState = state.getShellState();
final RyaClient ryaClient = shellState.getConnectedCommands().get();
final String ryaInstance = shellState.getRyaInstanceName().get();
try {
ryaClient.getAddUser().get().addUser(ryaInstance, username);
} catch (final InstanceDoesNotExistException e) {
throw new RuntimeException(String.format("A Rya instance named '%s' does not exist.", ryaInstance), e);
} catch (final RyaClientException e) {
throw new RuntimeException("The user's access could not be granted. Provided reason: " + e.getMessage(), e);
}
}
use of org.apache.rya.api.client.RyaClient in project incubator-rya by apache.
the class RyaCommands method loadData.
@CliCommand(value = LOAD_DATA_CMD, help = "Loads RDF Statement data from a local file to the connected Rya instance.")
public String loadData(@CliOption(key = { "file" }, mandatory = true, help = "A local file containing RDF Statements that is to be loaded.") final String file, @CliOption(key = { "format" }, mandatory = false, help = "The format of the supplied RDF Statements file. [RDF/XML, N-Triples, Turtle, N3, TriX, TriG, BinaryRDF, N-Quads, JSON-LD, RDF/JSON, RDFa]") final String format) {
// Fetch the command that is connected to the store.
final ShellState shellState = state.getShellState();
final RyaClient commands = shellState.getConnectedCommands().get();
final Optional<String> ryaInstanceName = shellState.getRyaInstanceName();
try {
final long start = System.currentTimeMillis();
// If the provided path is relative, then make it rooted in the user's home.
// Make sure the path is formatted with Unix style file
// separators('/') before using it as a regex replacement string.
// Windows file separators('\') will not work unless escaped.
final String userHome = FilenameUtils.separatorsToUnix(System.getProperty("user.home"));
final Path rootedFile = Paths.get(file.replaceFirst("^~", userHome));
RDFFormat rdfFormat = null;
// If a format was provided, then go with that.
if (format != null) {
rdfFormat = RDFFormat.valueOf(format);
if (rdfFormat == null) {
throw new RuntimeException("Unsupported RDF Statement data input format: " + format);
}
} else // Otherwise try to figure it out using the filename.
if (rdfFormat == null) {
rdfFormat = RDFFormat.forFileName(rootedFile.getFileName().toString());
if (rdfFormat == null) {
throw new RuntimeException("Unable to detect RDF Statement data input format for file: " + rootedFile);
} else {
consolePrinter.println("Detected RDF Format: " + rdfFormat);
consolePrinter.flush();
}
}
commands.getLoadStatementsFile().loadStatements(ryaInstanceName.get(), rootedFile, rdfFormat);
final String seconds = new DecimalFormat("0.0##").format((System.currentTimeMillis() - start) / 1000.0);
return "Loaded the file: '" + file + "' successfully in " + seconds + " seconds.";
} catch (final RyaClientException | IOException e) {
log.error("Error", e);
throw new RuntimeException("Can not load the RDF Statement data. Reason: " + e.getMessage(), e);
}
}
use of org.apache.rya.api.client.RyaClient in project incubator-rya by apache.
the class RyaCommands method sparqlQuery.
@CliCommand(value = SPARQL_QUERY_CMD, help = "Executes the provided SPARQL Query on the connected Rya instance.")
public String sparqlQuery(@CliOption(key = { "file" }, mandatory = false, help = "A local file containing the SPARQL Query that is to be read and executed.") final String file) {
// Fetch the command that is connected to the store.
final ShellState shellState = state.getShellState();
final RyaClient commands = shellState.getConnectedCommands().get();
final Optional<String> ryaInstanceName = shellState.getRyaInstanceName();
try {
// file option specified
String sparqlQuery;
if (file != null) {
sparqlQuery = new String(Files.readAllBytes(new File(file).toPath()), StandardCharsets.UTF_8);
consolePrinter.println("Loaded Query:");
consolePrinter.println(sparqlQuery);
} else {
// No Options specified. Show the user the SPARQL Prompt
final Optional<String> sparqlQueryOpt = sparqlPrompt.getSparql();
if (sparqlQueryOpt.isPresent()) {
sparqlQuery = sparqlQueryOpt.get();
} else {
// user aborted the SPARQL prompt.
return "";
}
}
consolePrinter.println("Executing Query...");
consolePrinter.flush();
return commands.getExecuteSparqlQuery().executeSparqlQuery(ryaInstanceName.get(), sparqlQuery);
} catch (final RyaClientException | IOException e) {
log.error("Error", e);
throw new RuntimeException("Can not execute the SPARQL Query. Reason: " + e.getMessage(), e);
}
}
use of org.apache.rya.api.client.RyaClient in project incubator-rya by apache.
the class RyaAdminCommandsTest method installWithMongoParameters.
@Test
public void installWithMongoParameters() throws DuplicateInstanceNameException, RyaClientException, IOException {
// Mock the object that performs the install operation.
final Install mockInstall = mock(Install.class);
final RyaClient mockCommands = mock(RyaClient.class);
when(mockCommands.getInstall()).thenReturn(mockInstall);
final SharedShellState state = new SharedShellState();
state.connectedToMongo(mock(MongoConnectionDetails.class), mockCommands);
final String instanceName = "unitTests";
final boolean enableFreeTextIndex = false;
final boolean enableTemporalIndex = false;
final boolean enablePcjIndex = false;
// Execute the command.
final InstallConfiguration installConfig = InstallConfiguration.builder().setEnableFreeTextIndex(enableFreeTextIndex).setEnableTemporalIndex(enableTemporalIndex).setEnablePcjIndex(enablePcjIndex).build();
final InstallPrompt mockInstallPrompt = mock(InstallPrompt.class);
when(mockInstallPrompt.promptInstanceName()).thenReturn(instanceName);
when(mockInstallPrompt.promptInstallConfiguration(instanceName)).thenReturn(installConfig);
when(mockInstallPrompt.promptVerified(eq(instanceName), eq(installConfig))).thenReturn(true);
final RyaAdminCommands commands = new RyaAdminCommands(state, mockInstallPrompt, mock(SparqlPrompt.class), mock(UninstallPrompt.class));
final String message = commands.installWithMongoParameters(instanceName, enableFreeTextIndex, enableTemporalIndex, enablePcjIndex);
// Verify the values that were provided to the command were passed through to the Install.
verify(mockInstall).install(eq(instanceName), eq(installConfig));
// Verify a message is returned that indicates the success of the operation.
final String expected = "The Rya instance named 'unitTests' has been installed.";
assertEquals(expected, message);
}
Aggregations