Oracle KV的Spring数据键值实现

时间:2017-03-29 13:23:07

标签: oracle-nosql spring-data-keyvalue

我想将Oracle NoSQL数据库与Spring数据一起使用。目的是通过spring数据存储库访问数据,甚至在其上使用spring数据。 所以我认为spring-data-keyvalue项目可以帮助我实现Oracle NoSQL KV的适配器。

我试图理解spring-data-keyvalue(http://docs.spring.io/spring-data/keyvalue/docs/current/reference/html/#key-value.core-concepts)的文档,但没有得到这个想法。 关于如何从头开始实现适配器的示例/教程非常有用。

我所拥有的是这个配置类,我提供了一个自定义KeyValueAdapter。现在,如果我使用CrudRepository方法,它将使用我的自定义适配器。

@Configuration
@EnableMapRepositories
public class KeyValueConfig {

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

OracleKeyValueAdapter是KeyValueAdapter的实现。我从spring-data-keyvalue-redis项目(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;
}

.
.
.

现在我正在尝试实现这个OracleKeyValueAdapter,但我不知道这是否有意义。

你能帮助我吗?

3 个答案:

答案 0 :(得分:0)

您可能想要了解如何在Redis上实现spring-data-keyvalue,这里的链接应该是一个很好的起点 - http://docs.spring.io/spring-data/data-keyvalue/docs/1.0.0.BUILD-SNAPSHOT/reference/redis.html

让我知道这是怎么回事,我对你想要完成的事情感兴趣。

答案 1 :(得分:0)

以下配置应该可以工作(在 v2.4.3 上测试)

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

KeyValueOperations bean 的名称 (mapKeyValueTemplate) 在这里很重要,但也可以按如下方式更改:

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

答案 2 :(得分:0)

我看到了 Spring KeyValue Repository 的来源: https://github.com/spring-projects/spring-data-keyvalue

我建议了解一下 Spring Repository 在内部是如何工作的。

如果你想实现自己的仓库(CustomKeyValueRepository),你必须至少创建6个类

  • EnableCustomKeyValueRepositories - 用于在项目中启用存储库类型的注释。
  • CustomKeyValueRepositoriesRegistrar - 此注释的注册器。
  • CustomKeyValueRepository - 存储库
  • CustomKeyValueRepositoryConfigurationExtension - Spring ConfigurationExtension 的实现。
  • CustomKeyValueAdapter - 为您的数据存储实现自定义适配器。
  • CustomKeyValueConfiguration - bean 适配器和模板的配置。

我通过这种方式编写了 Infinispan KeyValue Repository: https://github.com/OsokinAlexander/infinispan-spring-repository

我也写过关于这个的文章: https://habr.com/ru/post/535218/ 在 Chrome 中,您可以将其翻译成您的语言。

您可以尝试仅实现 CustomKeyValueAdapter 和 Configuration 的最简单方法。在配置中,您必须重新定义 Spring KeyValueAdapter bean 和 KeyValueTemplate(bean 的名称使用小写字母非常重要,这是它对我有用的唯一方法):

@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());
  }
}