Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .
As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.
Use ExecuteGremlinProfileQuery com um AWS SDK
Os exemplos de código a seguir mostram como usar o ExecuteGremlinProfileQuery.
Exemplos de ações são trechos de código de programas maiores e devem ser executados em contexto. É possível ver essa ação em contexto no seguinte exemplo de código:
- Java
-
- SDK para Java 2.x
-
/**
* Executes a Gremlin query against an Amazon Neptune database using the provided {@link NeptunedataClient}.
*
* @param client the {@link NeptunedataClient} instance to use for executing the Gremlin query
*/
public static void executeGremlinQuery(NeptunedataClient client) {
try {
System.out.println("Querying Neptune...");
ExecuteGremlinQueryRequest request = ExecuteGremlinQueryRequest.builder()
.gremlinQuery("g.V().has('code', 'ANC')")
.build();
ExecuteGremlinQueryResponse response = client.executeGremlinQuery(request);
System.out.println("Full Response:");
System.out.println(response);
// Retrieve and print the result
if (response.result() != null) {
System.out.println("Query Result:");
System.out.println(response.result().toString());
} else {
System.out.println("No result returned from the query.");
}
} catch (NeptunedataException e) {
System.err.println("Error calling Neptune: " + e.awsErrorDetails().errorMessage());
} catch (Exception e) {
System.err.println("Unexpected error: " + e.getMessage());
} finally {
client.close();
}
}
- Python
-
- SDK para Python (Boto3)
-
# Replace this with your actual Neptune endpoint
NEPTUNE_ENDPOINT = "https://[Specify Endpoint]:8182"
def main():
"""
Entry point of the program. Initializes the Neptune client and executes the Gremlin query.
"""
config = Config(connect_timeout=10, read_timeout=30, retries={'max_attempts': 3})
neptune_client = boto3.client(
"neptunedata",
endpoint_url=NEPTUNE_ENDPOINT,
config=config
)
execute_gremlin_query(neptune_client)
def execute_gremlin_query(neptune_client):
"""
Executes a Gremlin query against an Amazon Neptune database.
"""
try:
print("Querying Neptune...")
response = neptune_client.execute_gremlin_explain_query(
gremlinQuery="g.V().has('code', 'ANC')"
)
print("Full Response:")
print(response['output'].read().decode('UTF-8'))
except ClientError as e:
print(f"Error calling Neptune: {e.response['Error']['Message']}")
except BotoCoreError as e:
print(f"BotoCore error: {str(e)}")
except Exception as e:
print(f"Unexpected error: {str(e)}")
if __name__ == "__main__":
main()