Hay más ejemplos de AWS SDK disponibles en el GitHub repositorio de ejemplos de AWS Doc SDK.
Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.
Utilízalo ExecuteGremlinProfileQuery con un SDK AWS
Los siguientes ejemplos de código muestran cómo utilizar ExecuteGremlinProfileQuery.
Los ejemplos de acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Puede ver esta acción en contexto en el siguiente ejemplo 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()