cassandra + spring操作

这里是maven工程, 请先新建maven工程

修改pom.xml 文件,增加依赖
   org.springframework.data
   spring-data-cassandra
   1.0.0.RELEASE
   
   
   
     com.google.collections
     google-collections
   
   
     com.datastax.cassandra
     cassandra-driver-core
   
   
     com.datastax.cassandra
     cassandra-driver-dse
   
   
 
 
 
   com.datastax.cassandra
   cassandra-driver-core
   2.1.0
 
 
 
   com.datastax.cassandra
   cassandra-driver-dse
   2.1.0
 
 
 
   com.google.guava
   guava
   14.0.1
 
创建spring的配置文件 spring-cassandra.xml
 

http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns:cassandra=" http://www.springframework.org/schema/data/cassandra"
xmlns:context=" http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/cql  http://www.springframework.org/schema/cql/spring-cql-1.0.xsd
http://www.springframework.org/schema/data/cassandra  http://www.springframework.org/schema/data/cassandra/spring-cassandra-1.0.xsd
http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd">




port="${cassandra.port}" />











 




 
 paamconfig.properties文件增加
cassandra.contactpoints=IP1, IP2, IP3
cassandra.port=9042
cassandra.keyspace=链接的keyspace
 
新建映射对象
 
@Table(value="test") // test为 keyspace中的一个列(sql中称表)
public class TestVo implements Serializable {
private static final long serialVersionUID = 123456789L;
 @PrimaryKey
 private int id;
@Column(value="name")
 private String name;
@Column(value="age")
 private int age;
 
 public TestVo(int id, String name, int age) {
  this.id = id;
  this.name = name;
  this.age = age;
 }
 public int getId() {
  return id;
 }
 public String getName() {
  return name;
 }
 public int getAge() {
  return age;
 }
 @Override
 public String toString() {
  return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
 }
 
}
 
 测试DEMO
public class CassandraSpringDemo {
    public static void main(String[] args) {
         ConfigurableApplicationContext ct = new ClassPathXmlApplicationContext(
                  "springConfig/spring-cassandra.xml");

             /******CassandraTemplate 继承了CqlTemplate,支持新版本*******/
              CassandraTemplate template = (CassandraTemplate) ct.getBean("cassandraTemplate");
             /****低版本使用***/
             CqlTemplate cqlTemplate = (CqlTemplate) ct.getBean("cqlTemplate");
             List list = new ArrayList();
             for (int i = 600; i < 700; i++) {
                    TestVo test = new TestVo(i, "name", i);
                     list.add(test);
               }
            template.insert( list);
           //template.insert(cql);
           //template.update(entity)
           //template.delete(entity);
            //template.query(cql);
             ct.close();
        }
}
 

 

你可能感兴趣的:(大数据)