实现阿里云tablestore的CRUD框架spring-data-tablestore

阿里云tablestore(原OTS)是类Hbase的分布式Nosql,提供高可用的海量存储KV库服务。

优势是无需运维,稳定性非常好。

相对劣势是限制掉了Hbase的部分功能, 以保证性能稳定性。

原阿里云client使用上和springboot和springdata的风格差距太大,因此尝试实现了基于spring-data-commns的组件,支持自动生成CURD操作的拆箱即用。

源码见
https://github.com/togetu/spring-data-tablestore

spring-data-tablestore

spring-data-xxx for aliyun(alibaba clound) tablestore(hbase like)

clone, and run mvnw install to local.
import to your project

 
    in.togetu
    spring-data-tablestore
    0.0.1-SNAPSHOT
     

1. build a entity looks like this

@TableStoreEntity(name = "comments")
public class CommentsEntity {

    @Id
    @PrimaryId(order = 1, name = "id", partition = true)
    private String id;
    @PrimaryId(order = 3, name = "user")
    private String user;
    private String content;
    private Long votes;
    private String status;
    @PrimaryId(order = 2, name = "publishAt")
    private Long publishAt;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Long getVotes() {
        return votes;
    }

    public void setVotes(Long votes) {
        this.votes = votes;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Long getPublishAt() {
        return publishAt;
    }

    public void setPublishAt(Long publishAt) {
        this.publishAt = publishAt;
    }

    @Override
    public String toString() {
        return "CommentsEntity{" +
                "id='" + id + '\'' +
                ", user='" + user + '\'' +
                ", content='" + content + '\'' +
                ", votes=" + votes +
                ", status='" + status + '\'' +
                ", publishAt=" + publishAt +
                '}';
    }
}

2 create CRUD, the same as spring-data framework will do

public interface CommentsRepository extends PagingAndSortingRepository {
}

3 login the alibaba clound page to create tablestore database and table. remember endpoint, key and secret.

4 little config work to connect to tablestore service

@Configuration
@EnableTableStoreRepositories(basePackages = "com.package.to.your.repository")
public class ServiceConfig {

    @Bean("tsConfig")
    @ConfigurationProperties(prefix = "tablestore")
    public AliyunProductConfig getTableStoreConfig() {
        // manual fill the config if not use spring boot
        return new AliyunProductConfig();
    }
}

in application.yml(or properties,change the fomart)

tablestore:
  product: tablestore
  region: xxxx
  endpoint: https://xxxxx.xxxx.ots.aliyuncs.com
  accessKey: xxxxx
  accessSecret: xxxxxx
  instance:
    name: nameofinstace

5 enjoy it

你可能感兴趣的:(实现阿里云tablestore的CRUD框架spring-data-tablestore)