User Management

      The Java SDK lets you create users, assign them roles and associated privileges, and remove them from the system.

      User-Management APIs

      Users who have been assigned the Admin role for the cluster are able to create, edit, and remove users. The Java SDK provides APIs to support these activities. A high-level summary of the APIs can be found in User-Management, and details of all options in the UserManager API docs.

      Using the UserManager API

      The most common uses of the UserManager API are creating and listing users:

      Creating Users
      User user = new User(testUsername).password(testPassword).displayName("Constance Lambert");
          // Roles required for the reading of data from the bucket
          new Role("data_reader", "*"),
          new Role("query_select", "*"),
          // Roles required for the writing of data into the bucket.
          new Role("data_writer", bucketName),
          new Role("query_insert", bucketName),
          new Role("query_delete", bucketName),
          // Role required for the creation of indexes on the bucket.
          new Role("query_manage_index", bucketName));
      Listing Users
      List<UserAndMetadata> listOfUsers = cluster.users().getAllUsers();
      for (int j = 0; j < listOfUsers.size(); j++) {
        UserAndMetadata currentUser = listOfUsers.get(j);
        System.out.println("User's display name is: " + currentUser.user().displayName() );
        Set<Role> currentRoles = currentUser.user().roles();
        for (Role role : currentRoles) {
          System.out.println("   User has the role: " + + ", applicable to bucket " + role.bucket() );

      Using a user created in the SDK to access data:

      ClusterEnvironment environment = ClusterEnvironment.builder().build();
      Cluster userCluster = Cluster.connect(connectionString,
          ClusterOptions.clusterOptions(testUsername, testPassword).environment(environment));
      Bucket userBucket = userCluster.bucket(bucketName);
      Scope scope = userBucket.scope("inventory");
      Collection collection = scope.collection("airline");
      cluster.queryIndexes().createPrimaryIndex(bucketName, // create index if needed
      JsonObject returnedAirline10doc = collection.get("airline_10").contentAsObject();
      JsonObject airline11Object = JsonObject.create().put("callsign", "MILE-AIR").put("iata", "Q5").put("icao", "MLA")
          .put("id", 11).put("name", "40-Mile Air").put("type", "airline");
      collection.upsert("airline_11", airline11Object);
      JsonObject returnedAirline11Doc = collection.get("airline_11").contentAsObject();
      QueryResult result = userCluster.query("SELECT * FROM `travel-sample`.inventory.airline LIMIT 5");

      Further Reading

      The SDK also contains management APIs for dealing with Cluster resources.