当使用Java API的塞式客户端遵循这些最佳实践。
同步操作,设置clientpolicy.maxthreads将生成最大的数量的客户端线程。maxthreads数量是连接各节点的连接池(默认值= 300)。如果设置连接池大小太低,则为每个数据库调用创建套接字,这是慢的。如果设置连接池太高,则保留太多空闲连接
异步操作,设置asyncclientpolicy.asyncmaxcommands的并发命令处理由客户端在任何时间点的最大数目。
public class MyConsole implements Log.Callback { public MyConsole() { Log.setLevel(Log.Level.INFO); Log.setCallback(this); } @Override public void log(Log.Level level, String message) { // Write log messages to the appropriate place. } }
每个aerospikeclient / asyncclient实例生成一个线程定期维护使信息请求的所有服务器节点分区映射。多个客户端实例在服务器上创建额外的负载。在应用程序中只使用一个客户实例,并在多个线程中共享实例。aerospikeclient和asyncclient是线程安全的。
默认情况下,用户定义的key不存储在服务器上。它被转换为用于识别记录的哈希摘要。如果用户定义的key必须保存在服务器,使用下列方法之一:
Set WritePolicy.sendKey to true — 该key被发送到服务器上并且存储,用于存储在多个记录扫描和查询中的写入。
明确地存储和检索一个bin中的用户定义键。
WritePolicy policy = new WritePolicy(); policy.recordExistsAction = RecordExistsAction.REPLACE; client.put(policy, key, bins);
ClientPolicy policy = new ClientPolicy(); policy.readPolicyDefault.timeout = 50; policy.readPolicyDefault.maxRetries = 1; policy.readPolicyDefault.sleepBetweenRetries = 10; policy.writePolicyDefault.timeout = 200; policy.writePolicyDefault.maxRetries = 1; policy.writePolicyDefault.sleepBetweenRetries = 50; AerospikeClient client = new AerospikeClient(policy, "hostname", 3000); client.put(null, new Key("test", "set", 1), new Bin("bin", 5));
public MyClass { private AerospikeClient client; private WritePolicy writePolicy; public MyClass(AerospikeClient client) { this.client = client; writePolicy = new WritePolicy(); writePolicy.timeout = 200; writePolicy.maxRetries = 1; writePolicy.sleepBetweenRetries = 50; } public void put(Key key, Bin... bins) { client.put(writePolicy, key, bins); } }
If a policy needs to change from the default (such as setting an expected generation), instantiated that policy on-the-fly:
public void putIfGeneration(Key key, int generation, Bin... bins) { WritePolicy policy = new WritePolicy(); policy.generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL; policy.generation = generation; client.put(policy, key, bins); }