public void updateUserProfile ( String firstName , String lastName , boolean married ) { // Get the timestamp we'll use to set the value of the profile_updated // action. long ts = System . currentTimeMillis ( ) ; // Construct the key we'll use to fetch the user. PartitionKey key = new PartitionKey ( lastName , firstName ) ; // Get the profile and actions entity from the composite dao. UserProfileActionsModel profileActionsModel = userProfileActionsDao . get ( key ) ; // Updating the married status is hairy since our avro compiler isn't setup // to compile setters for fields. We have to construct a clone through the // builder. UserProfileActionsModel updatedProfileActionsModel = UserProfileActionsModel . newBuilder ( profileActionsModel ) . setUserProfileModel ( UserProfileModel . newBuilder ( profileActionsModel . getUserProfileModel ( ) ) . setMarried ( married ) . build ( ) ) . build ( ) ; // Since maps are mutable, we can update the actions map without having to // go through the builder like above. updatedProfileActionsModel . getUserActionsModel ( ) . getActions ( ) . put ( "profile_updated" , Long . toString ( ts ) ) ; if ( ! userProfileActionsDao . put ( updatedProfileActionsModel ) ) { // If put returns false, a write conflict occurred where someone else // updated the row between the times we did the get and put. System . out . println ( "Updating the user profile failed due to a write conflict" ) ; } }
Update the married status of a new user record .