0

I would like to use Oracle NoSQL database together with Spring data. The aim is to access the data over spring data repositories and even use spring data rest on top of it. So I think the spring-data-keyvalue project would help me, to implement an adapter for Oracle NoSQL KV.

I tried to understand the documentation of spring-data-keyvalue (http://docs.spring.io/spring-data/keyvalue/docs/current/reference/html/#key-value.core-concepts), but didn't get the idea. An example/tutorial about how to implement an adapter from scratch would be very helpful.

What I have is this configuration class where I provide a custom KeyValueAdapter. Now if I use CrudRepository methods it uses my custom adapter.

@Configuration
@EnableMapRepositories
public class KeyValueConfig {

     @Bean
     public KeyValueOperations keyValueTemplate() {
         return new KeyValueTemplate(new OracleKeyValueAdapter());
    }
}

The OracleKeyValueAdapter is an implementation of KeyValueAdapter. I got this from the spring-data-keyvalue-redis project (https://github.com/christophstrobl/spring-data-keyvalue-redis/blob/master/src/main/java/org/springframework/data/keyvalue/redis/RedisKeyValueAdapter.java)

public class OracleKeyValueAdapter extends AbstractKeyValueAdapter {

private KVStore store;

public OracleKeyValueAdapter() {
    String storeName = "kvstore";
    String hostName = "localhost";
    String hostPort = "5000";

    store = KVStoreFactory.getStore
            (new KVStoreConfig(storeName, hostName + ":" + hostPort));
}

//Custom implementations: 

@Override
public Object put(Serializable serializable, Object o, Serializable 
serializable1) {
    return null;
}

@Override
public boolean contains(Serializable serializable, Serializable 
serializable1) {
    return false;
}

.
.
.

Now I'm trying to implement this OracleKeyValueAdapter, but i don't know if that does even make sense.

Can you help me?

Joel Neukom
  • 529
  • 6
  • 7
  • it is old question, but did you found solution for this? I am having same problem currently. – Coma Mar 18 '20 at 13:51

3 Answers3

0

You might want to start with how spring-data-keyvalue is implemented over Redis, the link here should be a good starting point - http://docs.spring.io/spring-data/data-keyvalue/docs/1.0.0.BUILD-SNAPSHOT/reference/redis.html

Let me know how that goes, I am interested in what you are trying to accomplish.

0

The following configuration should work (tested on v2.4.3)

@Configuration
@EnableMapRepositories
public class Configuration {
    @Bean
    public KeyValueOperations mapKeyValueTemplate() {
        return new KeyValueTemplate(keyValueAdapter());
    }
    @Bean
    public KeyValueAdapter keyValueAdapter() {
        return new YourKeyValueAdapter();
    }
}

The name (mapKeyValueTemplate) of the KeyValueOperations bean is important here but it can also be changed as followed:

@Configuration
@EnableMapRepositories(keyValueTemplateRef = "foo")
public class Configuration {
    @Bean
    public KeyValueOperations foo() {
        return new KeyValueTemplate(keyValueAdapter());
    }
    @Bean
    public KeyValueAdapter keyValueAdapter() {
        return new YourKeyValueAdapter();
    }
}
Nick
  • 13
  • 4
0

I saw sources of Spring KeyValue Repository: https://github.com/spring-projects/spring-data-keyvalue

I recomend to understand, how Spring Repository work inside.

If you want to realise own repository (CustomKeyValueRepository), you must create at least 6 classes:

  • EnableCustomKeyValueRepositories - annotation to enable repository type in your project.
  • CustomKeyValueRepositoriesRegistrar - registrator for this annotaion.
  • CustomKeyValueRepository - repository
  • CustomKeyValueRepositoryConfigurationExtension - implementation of Spring ConfigurationExtension.
  • CustomKeyValueAdapter - implementation of custom adapter for your data store.
  • CustomKeyValueConfiguration - configuration of beans Adapter and Template.

I code Infinispan KeyValue Repository by this way: https://github.com/OsokinAlexander/infinispan-spring-repository

I also write article about this: https://habr.com/ru/post/535218/ In Chrome you can translate it to your language.

The simplest way you can try implement only CustomKeyValueAdapter and Configuration. In Configuration you must redefine Spring KeyValueAdapter bean and KeyValueTemplate (it is very important that the name of the bean is with a lowercase letter, that's the only way it worked for me):

@Configuration
public class CustomKeyValueConfiguration extends CachingConfigurerSupport {

  @Autowired
  private ApplicationContext applicationContext;

  @Bean
  public CustomKeyValueAdapter getKeyValueAdapter() {
    return new CustomKeyValueAdapter();
  }

  @Bean("keyValueTemplate")
  public KeyValueTemplate getKeyValueTemplate() {
    return new KeyValueTemplate(getKeyValueAdapter());
  }
}