實現(xiàn)阿里云tablestore的CRUD框架spring-data-tablestore

阿里云tablestore(原OTS)是類Hbase的分布式Nosql,提供高可用的海量存儲KV庫服務(wù)。

優(yōu)勢是無需運維,穩(wěn)定性非常好。

相對劣勢是限制掉了Hbase的部分功能, 以保證性能穩(wěn)定性。

原阿里云client使用上和springboot和springdata的風(fēng)格差距太大,因此嘗試實現(xiàn)了基于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

 <dependency>
    <groupId>in.togetu</groupId>
    <artifactId>spring-data-tablestore</artifactId>
    <version>0.0.1-SNAPSHOT</version>
 </dependency>    

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<CommentsEntity, Key> {
}

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評論 0 10
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,564評論 19 139

友情鏈接更多精彩內(nèi)容