getReplicaFromPreferredServerGroup

Gets a document (possibly a replica) from the specified Couchbase collection matching the specified id.

It will be fetched only from nodes in the preferred server group, which can be configured with the preferredServerGroup client setting. If this client setting is unset or refers to a nonexistent server group, or if no node in group has the document, then DocumentUnretrievableException is thrown.

It is strongly recommended to that this method always be used with a fallback strategy, like:

val result = try {
getReplicaFromPreferredServerGroup(collection, id)
} catch (e: DocumentUnretrievableException) {
get(collection, id)
}

Parameters

collection

the Couchbase collection containing the document

id

the ID of the document to get

Throws

DocumentUnretrievableException

if for any reason the document cannot be fetched.

Samples

import com.couchbase.client.core.env.NetworkResolution
import com.couchbase.client.core.retry.FailFastRetryStrategy
import com.couchbase.client.core.service.ServiceType
import com.couchbase.client.kotlin.Cluster
import com.couchbase.client.kotlin.codec.RawJsonTranscoder
import com.couchbase.client.kotlin.env.ClusterEnvironment
import com.couchbase.client.kotlin.env.dsl.TrustSource
import com.couchbase.client.kotlin.kv.Durability
import com.couchbase.client.kotlin.query.execute
import kotlinx.coroutines.runBlocking
import java.nio.file.Paths
import java.util.Optional
import kotlin.time.Duration.Companion.seconds

fun main() { 
   //sampleStart 
   // Configure the preferred server group
val cluster = Cluster.connect("127.0.0.1", "Administrator", "password") {
    preferredServerGroup = "Group 1"
} 
   //sampleEnd
}