学习cassandra(1)入门,使用场景(写多读少)和搭建启动使用,整合Spring boot

1.场景 这个nosql数据库适合写多读少的情况

2.入门

http://cassandra.apache.org/
下载后

wget http://mirror.bit.edu.cn/apache/cassandra/3.11.1/apache-cassandra-3.11.1-bin.tar.gz

解压

tar -xvf apache-cassandra-3.11.1-bin.tar.gz
cd apache-cassandra-3.11.1

修改配置文件

通用配置
vim conf/cassandra.yaml 
jvm配置
vim conf/jvm.options

启动

[root@rabbit1 apache-cassandra-3.11.1]# bin/cqlsh localhost
Connected to Test Cluster at localhost:9042.
[cqlsh 5.0.1 | Cassandra 3.11.1 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.

创建keyspace

tables由keyspace维护

CQL stores data in tables, whose schema defines the layout of said data in the table, and those tables are grouped in keyspaces.
官网原话

cqlsh> CREATE KEYSPACE Excelsior
   ...            WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};
cqlsh> use Excelsior;
cqlsh:excelsior> CREATE TABLE t (
             ...     pk int,
             ...     t int,
             ...     v text,
             ...     s text static,
             ...     PRIMARY KEY (pk, t)
             ... );
cqlsh:excelsior> 
cqlsh:excelsior> INSERT INTO t (pk, t, v, s) VALUES (0, 0, 'val0', 'static0');
cqlsh:excelsior> INSERT INTO t (pk, t, v, s) VALUES (0, 1, 'val1', 'static1');
cqlsh:excelsior> 
cqlsh:excelsior> SELECT * FROM t;

 pk | t | s       | v
----+---+---------+------
  0 | 0 | static1 | val0
  0 | 1 | static1 | val1

学习cassandra(1)入门,使用场景(写多读少)和搭建启动使用,整合Spring boot_第1张图片
这个表表示建立了一张名为t的表。属性pk和t是主键,s(有static后缀)是全局的。
插入和查询还可以经过JSON

cqlsh:excelsior> select json * from t;

 [json]
------------------------------------------------
 {"pk": 0, "t": 0, "s": "static5", "v": "val0"}
 {"pk": 0, "t": 1, "s": "static5", "v": "val1"}
 {"pk": 0, "t": 2, "s": "static5", "v": "val3"}
 {"pk": 0, "t": 4, "s": "static5", "v": "val4"}
 {"pk": 0, "t": 5, "s": "static5", "v": "val5"}

(5 rows)
cqlsh:excelsior> insert into t json '{"pk": 0, "t": 5, "s": "static6", "v": "val6"}';
cqlsh:excelsior> select json * from t;

 [json]
------------------------------------------------
 {"pk": 0, "t": 0, "s": "static6", "v": "val0"}
 {"pk": 0, "t": 1, "s": "static6", "v": "val1"}
 {"pk": 0, "t": 2, "s": "static6", "v": "val3"}
 {"pk": 0, "t": 4, "s": "static6", "v": "val4"}
 {"pk": 0, "t": 5, "s": "static6", "v": "val6"}

3. 常用运维命令

查看所有的keyspaces
cqlsh> desc keyspaces

查看所有的tables
cqlsh> use excelsior;
cqlsh:excelsior> desc tables;
查看表结构
cqlsh:excelsior> desc table t;

5. 设置外网访问

修改conf/cassandra.yuml

rpc_address: 0.0.0.0
broadcast_rpc_address: 123.56.13.70

重启

6. 整合Spring boot cassandra

pom.xml

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-cassandraartifactId>
        dependency>

登录cassandra创建keyspace和table

CREATE KEYSPACE mykeyspace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

desc keyspaces;

use mykeyspace ;
CREATE TABLE customer (id TimeUUID PRIMARY KEY, firstname text, lastname text);
CREATE INDEX customerfistnameindex ON customer (firstname);
CREATE INDEX customersecondnameindex ON customer (lastname);

app.applications

spring.data.cassandra.keyspace-name=mykeyspace
spring.data.cassandra.contact-points=123.56.13.70
spring.data.cassandra.port=9042

示例代码:

@SpringBootApplication
public class DemoCassandraApplication  implements CommandLineRunner {

    @Autowired
    private CustomerRepository repository;

    @Override
    public void run(String... args) throws Exception {
        this.repository.deleteAll();

        // save a couple of customers
        this.repository.save(new Customer(UUIDs.timeBased(), "Alice", "Smith"));
        this.repository.save(new Customer(UUIDs.timeBased(), "Bob", "Smith"));

        // fetch all customers
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : this.repository.findAll()) {
            System.out.println(customer);
        }
        System.out.println();

        // fetch an individual customer
        System.out.println("Customer found with findByFirstName('Alice'):");
        System.out.println("--------------------------------");
        System.out.println(this.repository.findByFirstName("Alice"));

        System.out.println("Customers found with findByLastName('Smith'):");
        System.out.println("--------------------------------");
        for (Customer customer : this.repository.findByLastName("Smith")) {
            System.out.println(customer);
        }
    }

    public static void main(String[] args) {
            System.out.println("Math.abs(x)" +Math.abs(-2147483648));
            System.out.println(0-(-2147483648));
            System.out.println(Integer.MIN_VALUE);
            ConfigurableApplicationContext ctx = SpringApplication.run(DemoCassandraApplication.class, args);
            ConfigurableEnvironment env =  ctx.getEnvironment();
            String rst = JSON.toJSONString(env, SerializerFeature.PrettyFormat, SerializerFeature.WriteClassName,
                    SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
            System.out.println(rst);
    }

}
public interface CustomerRepository extends CrudRepository<Customer, String> {

    @Query("Select * from customer where firstname=?0")
    public Customer findByFirstName(String firstName);

    @Query("Select * from customer where lastname=?0")
    public List findByLastName(String lastName);
}
@Table
public class Customer {

    @PrimaryKey
    private UUID id;

    private String firstName;

    private String lastName;

    public Customer() {
    }

    public Customer(UUID id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format("Customer[id=%s, firstName='%s', lastName='%s']", this.id,
                this.firstName, this.lastName);
    }

}

你可能感兴趣的:(cassandra)