Spring数据cassandra - 复杂类型值为json

时间:2016-11-15 01:08:58

标签: spring-data-cassandra

我见过Cassandra entities must have the @Table, @Persistent or @PrimaryKeyClass Annotation和。{ Build breaks when using cassandra entity with jodatime object但我在那里找不到答案。

我在弹簧启动应用程序中使用了弹簧数据cassandra 1.4.5.RELEASE。 我试图存储"复杂类型"作为cassandra的JSON字符串。

这是创建我的表的CQL" test":

#include <memory>

template <typename Elem>
class Arr_2d
  {
  public:

    Arr_2d(unsigned r, unsigned c)
      : rows_(r), columns_(c), data(new Elem[rows_ * columns_]) { }

    Elem * operator [] (unsigned row_idx)
      { return(data.get() + (row_idx * columns_)); }

    unsigned rows() const { return(rows_); }
    unsigned columns() const { return(columns_); }

  private:
    const unsigned rows_, columns_;
    std::unique_ptr<Elem []> data;
  };

inline void toggle_one(bool &b) { b = !b; }

void toggle(Arr_2d<bool> &tab, unsigned row, unsigned column)
  {
    toggle_one(tab[row][column]);

    if (column > 0)
      toggle_one(tab[row][column - 1]);

    if (row > 0)
      toggle_one(tab[row - 1][column]);

    if (column < (tab.columns() - 1))
      toggle_one(tab[row][column + 1]);

    if (row < (tab.rows() - 1))
      toggle_one(tab[row + 1][column]);
  }

int solve(Arr_2d<bool> &tab)
  {
    int count = 0;
    unsigned i = 0;

    for ( ; i < (tab.rows() - 1); ++i)
      for (unsigned j = 0; j < tab.columns(); ++j)
        if (tab[i][j])
          {
            toggle(tab, i + 1, j);
            ++count;
          }

    for (unsigned j = 0; j < tab.columns(); ++j)
      if (tab[i][j])
        // Impossible.
        return(-count);

    return(count);
  }

unsigned ex1[] = {
0, 1, 0,
1, 1, 1,
0, 1, 0
};

unsigned ex2[] = {
0, 1, 0, 0, 0, 0, 0, 0,
1, 1, 1, 0, 1, 0, 1, 0,
0, 0, 1, 1, 0, 1, 1, 1,
1, 1, 0, 1, 1, 1, 0, 0,
1, 0, 1, 1, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 0
};

Arr_2d<bool> load(unsigned rows, unsigned columns, const unsigned *data)
  {
    Arr_2d<bool> res(rows, columns);

    for (unsigned i = 0; i < rows; ++i) 
      for (unsigned j = 0; j < columns; ++j)
        res[i][j] = !!*(data++);

    return(res);
  }

#include <iostream>

int main()
  {
    {
      Arr_2d<bool> tab = load(3, 3, ex1);
      std::cout << solve(tab) << '\n';
    }

    {
      Arr_2d<bool> tab = load(6, 8, ex2);
      std::cout << solve(tab) << '\n';
    }

    return(0);
  }

这是我的实体:

CREATE TABLE test (
    id uuid PRIMARY KEY,
    co text
}

这是我的&#34;复杂对象&#34;:

import com.datastax.driver.core.DataType;
import org.springframework.data.cassandra.mapping.CassandraType;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;

import java.util.UUID;

@Table("test")
public class Test {

    @PrimaryKey("id")
    private UUID id;

    @Column("co")
    private ComplexObject co;

    public UUID getId() {
        return id;
    }

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

    @CassandraType(type = DataType.Name.TEXT)
    public ComplexObject getCo() {
        return co;
    }

    public void setCo(ComplexObject co) {
        this.co = co;
    }

}

这是我的存储库:

public class ComplexObject {
    private String value1;
    private String value2;

    public String getValue1() {
        return value1;
    }

    public void setValue1(String value1) {
        this.value1 = value1;
    }

    public String getValue2() {
        return value2;
    }

    public void setValue2(String value2) {
        this.value2 = value2;
    }
}

当我启动应用程序时,出现以下错误:

  

引起:   org.springframework.data.cassandra.mapping.VerifierMappingExceptions:   ComplexObject:Cassandra实体必须具有@ Table,@ Persistent或   @PrimaryKeyClass Annotation

我想要的是我的&#34;复杂的对象&#34;存储在文本栏中&#34; co&#34;用它的JSON表示。 我知道Spring Data Cassandra还不支持UDT,但我只是要求将它存储为&#34; text&#34;。

我设法存储地图&gt;在&#34;文本&#34;带有@ReadingConverter,@ WritingConverter和自定义MappingCassandraConverter的列来解决问题https://jira.spring.io/browse/DATACASS-349

我想做一些非常相似的事情,但问题还不在于转换器。不知何故,Spring Data Cassandra似乎相信我的&#34; ComplexType&#34;当它不是......时,它是一个Cassandra实体。

我做错了吗?

0 个答案:

没有答案
相关问题