0

我想将 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,但我不知道这是否有意义。

你能帮助我吗?

4

3 回答 3

0

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

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

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

@Configuration
@EnableMapRepositories(keyValueTemplateRef = "foo")
public class Configuration {
    @Bean
    public KeyValueOperations foo() {
        return new KeyValueTemplate(keyValueAdapter());
    }
    @Bean
    public KeyValueAdapter keyValueAdapter() {
        return new YourKeyValueAdapter();
    }
}
于 2021-01-24T14:12:46.560 回答
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 的最简单方法。在 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());
  }
}
于 2021-03-23T11:46:45.160 回答
0

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

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

于 2017-03-29T16:56:41.533 回答