3

I am new to the spring-aerospike-data, I was hoping if someone could let me know how can I remove @user_key and @_class bins when I send post request to the application. I am trying to use spring-areospike-data in my Rest-api. please see bleow example:

  • POM dependecis:

     <dependency>
            <groupId>com.aerospike</groupId>
            <artifactId>spring-data-aerospike</artifactId>
            <version>3.0.0</version>
        </dependency>

  • Spring version: 2.5.1
  • Configruation classes:

    @Data
    @Component
    @ConfigurationProperties(prefix = "aerospike")
    public class AerospikeConfigurationProperties {
     private String host;
     private int port;
     private String namespace;
    }


     @Configuration
    @EnableConfigurationProperties(AerospikeConfigurationProperties.class)
    @EnableAerospikeRepositories(basePackages = "com.moneris.repositories")
    public class AerospikeConfiguration extends AbstractAerospikeDataConfiguration {
     @Autowired
     private AerospikeConfigurationProperties aerospikeConfigurationProperties;
     @Override
     protected Collection<Host> getHosts() {
        return Collections.singleton(new Host(aerospikeConfigurationProperties.getHost(), 
       aerospikeConfigurationProperties.getPort()));
    }
    @Override
    protected String nameSpace() {
        return aerospikeConfigurationProperties.getNamespace();
    }
   }

pom dependencies: here is my POJO:


    @Data
    @Document
    @AllArgsConstructor
    public class UserApi {
     @Id
     private String apiKey;
     private String apiName;
     private int enabled;
   
   }

and here is the repo:


    public interface AerospikeUserApiKeyRepository extends CrudRepository<UserApi, String> {
    
   }

and here is service:


    @Service
    @AllArgsConstructor
    public class UserService {
   
        AerospikeUserApiKeyRepository aerospikeUserApiKeyRepository;
    public void addUserApiKey(UserApi userApi) {
        aerospikeUserApiKeyRepository.save(userApi);
    }
    
    public Optional<UserApi> getUserApi(String apiKey){
        return aerospikeUserApiKeyRepository.findById(apiKey);
    }
    
}

here the controller:


    @RestController
    @AllArgsConstructor
    public class UserController {
    UserService userService;

    @PostMapping("/users/apiKey")
    public void addUserApi(@RequestBody UserApi userApi) {
        userService.addUserApiKey(userApi);;
    }
    
    @GetMapping("/users/apiKey/{apiKey}")
         public Optional<UserApi> getUserApi(@PathVariable("apiKey") String apiKey) {
        return userService.getUserApi(apiKey);
    }
}

when I run my applicaiton and send post request to the http://localhost:8080/users/apiKey with below json body

{
    "apiKey": "ABC",
    "apiName": "TEST",
    "enabled": 1
}

I am geting below record in the aerospike db enter image description here is ther any way not stor @user_key and @_class in the sets?

3 Answers3

4

Storing @class can be disabled via providing the following bean in the context:

@Bean
@Override
public AerospikeTypeAliasAccessor aerospikeTypeAliasAccessor() {
    //we do not want to save @class field with type of document into Aerospike
    return new AerospikeTypeAliasAccessor(null);
}

Note, that disabling saving type information will make it impossible to read documents with generic data types.

3

Spring Data Aerospike stores both @_class and @user_key bins by default out of the box.

We store @_class bin in order to know how to map back the object to a Plain Old Java Object (POJO) when reading a record from Aerospike.

We store @user_key because Aerospike doesn’t store the user key (only the digest) when saving a record, you can read more about the data model in Aerospike here: https://docs.aerospike.com/docs/architecture/data-model.html, so when you have historical data it’s helpful to know the original user key.

You can disable @_class, but with consequences. Please check Anastasiia Smirnova's comment.

Roi Menashe
  • 670
  • 3
  • 9
  • `@class` can actually be turned off, but with the consequences. I've added a comment how to do this. Could you please update your answer? Thanks! – Anastasiia Smirnova Jul 09 '21 at 11:32
0

Yes, @_class is needed to map the data back to the java class you persisted, as others already mentioned. It might be not needed on use cases like write using spring-data-aerospike and read via another application or tool outside.

I had a similar issue with Aerospike but my concern was slightly different: writing very long and repetitive class names with each record on a write-heavy application: sacrificing more bytes, more memory in long term, than actually needed.

Solution to save some bytes per record was using a type alias:

import org.springframework.data.annotation.TypeAlias;

@Document
@TypeAlias("DeliveryRequest")
public class DeliveryRequest {}

So it reduced the space needed from 71 bytes:

com.myorganization.servicemanager.delivery.domain.model.DeliveryRequest

to 15 bytes.

Wanted to share it as a hint for devs dealing with long and annoying class/package names in AQL outputs.

edigu
  • 9,878
  • 5
  • 57
  • 80