我正在尝试为Gremlin客户端配置事务,该事务将不允许任何变异查询(只读)。
我知道在JanusGraph或Titan中使用其API(buildTransaction()为readOnly())是可能的,但是对于TinkerPop或Neptune,我没有发现任何类似的东西。
我正在使用基于Java脚本的客户端(会话):
Cluster cluster = Cluster.open();
Client client = cluster.connect('SessionID');
String mutatingQuery = "g.addV('Test')";
client.submit("g.tx().open()");
client.submit(mutatingQuery); // This should fail.
client.submit("g.tx().commit()");
我知道您可以从服务器端限制这些查询类型。但是从客户端这也是可能的吗?我也不确定这是否是解决此问题的正确方法。
编辑:
我正在通过提交“脚本”通过WebSocket与Gremlin Server进行远程通信。
从Java,我将群集配置为:
Cluster cluster =
Cluster.build().addContactPoint(url).port(port).create();
然后使用客户端提交查询:
Client c= cluster.connect().init();
c.submit(query);
我了解Graph支持的ReadOnlyStrategy。但是我没有找到仅通过服务器配置脚本通过上述方法启用它的方法。还有另一种方法来限制提交的“查询”吗?
我的服务器配置有以下默认groovy脚本:
globals << [g : graph.traversal()] // Could have used readOnly strategy here.
我的客户正在发送这样的查询:
c.submit("g.addV('test')"); // this should fail
有什么想法吗?
答案 0 :(得分:1)
使用Neptune有几种方法,您可以针对给定用例强制实施只读样式语义。一种是使用TinkerPop ReadOnly策略。另一种方法是仅允许用户/应用程序访问集群的读取端点,而不允许写入器端点。
您能否再谈谈您的特定用例?
已更新:以下是从客户端应用程序使用Java创建ReadOnlyStrategy的示例:
// Experiment with the ReadOnlyStrategy
g2 = g.withStrategies(ReadOnlyStrategy.instance());
try
{
g2.addV("shouldfail").iterate();
}
catch(Exception e)
{
System.out.println("Unable to add vertex as expected");
}
干杯, 开尔文
答案 1 :(得分:0)
海王星目前不支持使用.tx()
的手动交易逻辑。
文档:https://docs.aws.amazon.com/neptune/latest/userguide/access-graph-gremlin-differences.html->交易
并且正如Kelvin在他的回答中提到的那样,您始终可以使您的应用程序与集群的读取器端点进行通信,这使您只能执行只读操作。您可以通过从控制台查看群集详细信息来获取读取器端点,或者使用Neptune SDK以编程方式获取端点。
aws neptune describe-db-clusters --db-cluster-identifier \
neptunedbcluster-t0wz5xpqmiuc --region us-east-1 --output table
---------------------------------------------------------------------------------------------------------------------------------
| DescribeDBClusters |
+-------------------------------------------------------------------------------------------------------------------------------+
|| DBClusters ||
|+-----------------------------------+-----------------------------------------------------------------------------------------+|
|| AllocatedStorage | 1 ||
|| BackupRetentionPeriod | 1 ||
|| ClusterCreateTime | 2018-10-16T04:17:23.384Z ||
|| DBClusterArn | arn:aws:rds:us-east-1:123123123123:cluster:neptunedbcluster-t0wz5xpqmiuc ||
|| DBClusterIdentifier | neptunedbcluster-t0wz5xpqmiuc ||
|| DBClusterParameterGroup | neptunedbclusterparametergr-q6eekezcpd04 ||
|| DBSubnetGroup | neptunedbsubnetgroup-dmcliosqke8b ||
|| DbClusterResourceId | cluster-AEFFOL3WFA7W5H7WL4QWEQWEQWE ||
|| EarliestRestorableTime | 2018-10-21T07:04:17.379Z ||
|| Endpoint | neptunedbcluster-t0wz5xpqmiuc.cluster-qweqweqwe.us-east-1.neptune.amazonaws.com ||
|| Engine | neptune ||
|| EngineVersion | 1.0.1.0 ||
|| HostedZoneId | ZUFXD4SLT2LS7 ||
|| IAMDatabaseAuthenticationEnabled | False ||
|| LatestRestorableTime | 2018-10-22T17:16:44.233Z ||
|| MasterUsername | admin ||
|| MultiAZ | False ||
|| Port | 8182 ||
|| PreferredBackupWindow | 06:52-07:22 ||
|| PreferredMaintenanceWindow | mon:09:33-mon:10:03 ||
|| ReaderEndpoint | neptunedbcluster-t0wz5xpqmiuc.cluster-ro-qweqweqwe.us-east-1.neptune.amazonaws.com ||
|| Status | available ||
|| StorageEncrypted | False ||
|+-----------------------------------+-----------------------------------------------------------------------------------------+|
||| AssociatedRoles |||
||+----------+----------------------------------------------------------------------------------------------------------------+||
||| RoleArn | arn:aws:iam::393993383537:role/RDS-2-Neptune-Demo-NeptuneBa-NeptuneLoadFromS3Role-1NKBKFMRK6L1G |||
||| Status | ACTIVE |||
||+----------+----------------------------------------------------------------------------------------------------------------+||
||| AvailabilityZones |||
||+---------------------------------------------------------------------------------------------------------------------------+||
||| us-east-1b |||
||| us-east-1c |||
||| us-east-1a |||
||+---------------------------------------------------------------------------------------------------------------------------+||
||| DBClusterMembers |||
||+------------------------------------------------------------+--------------------------------------------------------------+||
||| DBClusterParameterGroupStatus | in-sync |||
||| DBInstanceIdentifier | neptunedbinstance-owqd0npl6ar4 |||
||| IsClusterWriter | True |||
||| PromotionTier | 1 |||
||+------------------------------------------------------------+--------------------------------------------------------------+||
||| VpcSecurityGroups |||
||+-----------------------------------+---------------------------------------------------------------------------------------+||
||| Status | VpcSecurityGroupId |||
||+-----------------------------------+---------------------------------------------------------------------------------------+||
||| active | sg-01ab9e609e122c01b |||
||| active | sg-0723b9b248cbe20a3 |||
||+-----------------------------------+---------------------------------------------------------------------------------------+||
请注意,阅读器端点在结果中可以作为ReaderEndpoint
使用。
更新: 如果群集中只有一个实例,则读取器和写入器端点都指向同一实例。如果您想要一个真正的只读端点,则应创建一个多实例群集,在这种情况下,读取器端点会在读取器之间进行DNS轮询。