`
sillycat
  • 浏览: 2551952 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring Boot and RESTful API(3)Cassandra

 
阅读更多
Spring Boot and RESTful API(3)Cassandra

Installation of the Latest 3.10 Version
>wget http://mirror.stjschools.org/public/apache/cassandra/3.10/apache-cassandra-3.10-bin.tar.gz
unzip the file and put it in working directory, add bin to the PATH

Prepare the directory
>sudo mkdir /var/lib/cassandra
>sudo chown -R carl /var/lib/cassandra

Command to start cassandra
>cassandra -Dcassandra.config=file:///opt/cassandra/conf/cassandra.yaml

Client connect to local
>cqlsh localhost 9042

Run the Easy Sample
Prepare the Database
cqlsh>CREATE KEYSPACE mykeyspace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
cqlsh>use mykeyspace;
cqlsh>CREATE TABLE customer (id TimeUUID PRIMARY KEY, firstname text, lastname text);
cqlsh>CREATE INDEX customerfistnameindex ON customer (firstname);
cqlsh>CREATE INDEX customersecondnameindex ON customer (lastname);

cqlsh>CREATE TABLE jobcountdiff(
source_id text,
record_date TIMESTAMP,
new_active_count int,
new_admin_count int,
old_active_count int,
old_admin_count int,
PRIMARY KEY ( source_id, record_date )
) WITH CLUSTERING ORDER BY (record_date DESC);

cqlsh>INSERT INTO jobcountdiff( source_id, record_date, new_active_count, new_admin_count, old_active_count, old_admin_count) VALUES ( '9527', toTimestamp(now()), 1,1,1,1);
cqlsh>INSERT INTO jobcountdiff( source_id, record_date, new_active_count, new_admin_count, old_active_count, old_admin_count) VALUES ( '9527', toTimestamp(now()), 2,2,2,2);
cqlsh>INSERT INTO jobcountdiff( source_id, record_date, new_active_count, new_admin_count, old_active_count, old_admin_count) VALUES ( '9527', toTimestamp(now()), 3,3,3,3);
cqssh>INSERT INTO jobcountdiff( source_id, record_date, new_active_count, new_admin_count, old_active_count, old_admin_count) VALUES ( '9528', toTimestamp(now()), 3,3,3,3);

sqlsh>select * from jobcountdiff where source_id = '9527' order by record_date desc;

cqlsh>DESCRIBE TABLES;

https://academy.datastax.com/resources/getting-started-time-series-data-modeling
sqlsh> select * from jobcountdiff where source_id = '9527' and record_date > '2017-06-01' and record_date < '2017-06-18’;
sqlsh>select * from jobcountdiff where source_id = '9527’;

sqlsh>CREATE TABLE temperature_by_day3 (
date text,
weatherstation_id text,
event_time timestamp,
temperature text,
PRIMARY KEY ((date), weatherstation_id, event_time)
);
sqlsh>INSERT INTO temperature_by_day3(weatherstation_id,date,event_time,temperature)
VALUES ('1234ABCD','2013-04-03','2013-04-03 07:01:00','72F’);
sqlsh>INSERT INTO temperature_by_day3(weatherstation_id,date,event_time,temperature)
VALUES ('1234ABCD','2013-04-03','2013-04-03 08:01:00','72F');
sqlsh>INSERT INTO temperature_by_day3(weatherstation_id,date,event_time,temperature)
VALUES ('1234ABCE','2013-04-03','2013-04-03 07:01:00','72F');

sqlsh>CREATE TABLE temperature_by_day4 ( date text, weatherstation_id text, event_time timestamp, temperature text, PRIMARY KEY ((date), weatherstation_id, event_time) ) WITH CLUSTERING ORDER BY (weatherstation_id DESC, event_time DESC);
sqlsh>INSERT INTO temperature_by_day4(weatherstation_id,date,event_time,temperature)
VALUES ('1234ABCE','2013-04-03','2013-04-03 07:01:00','72F’);
sqlsh>INSERT INTO temperature_by_day4(weatherstation_id,date,event_time,temperature)
VALUES ('1234ABCE','2013-04-03','2013-04-03 08:01:00','72F');
sqlsh>INSERT INTO temperature_by_day4(weatherstation_id,date,event_time,temperature)
VALUES ('1234ABCD','2013-04-03','2013-04-03 08:01:00','72F');

Query may be needed:
select * from jobcountdiff where source_id = 9527;
select * from jobcountdiff where source_id = '9527' and record_date > '2017-06-15';
select distinct source_id from jobcountdiff;

Set Up Logging Project and Database
>CREATE KEYSPACE jobsmonitor WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
>use jobsmonitor;
>CREATE TABLE jobcounthistory(
source_id text,
record_date TIMESTAMP,
new_active_count int,
new_admin_count int,
old_active_count int,
old_admin_count int,
PRIMARY KEY ( source_id, record_date )
) WITH CLUSTERING ORDER BY (record_date DESC);

Possible query is as follow:
select * from jobcounthistory where source_id = 'asdfasf';
select * from jobcounthistory where source_id = 'asdf' and record_date > '2017-06-11';

>CREATE TABLE jobcountdiff(
date text,
diff int,
record_date TIMESTAMP,
source_id text,
PRIMARY KEY ( date, diff, record_date )
) WITH CLUSTERING ORDER BY (diff ASC, record_date DESC);

Possible Query is as follow:
select * from jobcountdiff where date = '2017-06-15';
select * from jobcountdiff where date = '2017-06-15' and diff > 10;

http://docs.spring.io/spring-data/cassandra/docs/2.0.0.M4/reference/html/

Cassandra Structure Reference from Old Project
CampaignProfile -> camapignID, profileID (MySQL)
Profile -> profileID, brandCode, name, enabled, description, rules  (MySQL)
Rule -> profileID, attributeMetadataID, operator, value  (MySQL)
AttributeMetadata -> id, brandCode, name, attributeType, allowedValues, enabled, dateCreated (MySQL)

Attributes -> brandCode, deviceID, Attributes(Map)
       put -> brandCode, deviceID, unixtime, attributes
val mutator = HFactory.createMutator(ksp, CompositeSerializer.get)
val column = HFactory.createColumn(unixtime, attrib.toBytes)
column.setClock(ksp.createClock)

mutator.insert(rowkey(brandCode, deviceId), columnFamilyName, column)
if (config.getBoolean(env + ".attributes.indexOnPut")) {
  DeviceLookup.update(brandCode, deviceId, attrib, Profile.enabled(brandCode))
}

        get -> brandCode, deviceID, unixtime
val q = HFactory.createSliceQuery(ksp, CompositeSerializer.get, LongSerializer.get, BytesArraySerializer.get)
  .setKey(rowkey(brandCode, deviceId))
  .setColumnFamily(columnFamilyName)
  .setRange(unixtime, 0L, false, 1)

q.execute.get.getColumns.headOption.map(x => com.digby.localpoint.model.Attributes(x.getValue))

        get -> brandCode, deviceID
val q = HFactory.createSliceQuery(ksp, CompositeSerializer.get, LongSerializer.get, BytesArraySerializer.get)
  .setKey(rowkey(brandCode, deviceId))
  .setColumnFamily(columnFamilyName)
  .setRange(null, null, false, 1)

q.execute.get.getColumns.headOption.map(x => com.digby.localpoint.model.Attributes(x.getValue))

DeviceLookup -> brandCode, profileID,
        addDevice(brandCode, profileId, deviceID)
val mutator: Mutator[Composite] = HFactory.createMutator(ksp, CompositeSerializer.get)
val column = HFactory.createColumn(deviceId, "")
column.setClock(ksp.createClock)
mutator.addInsertion(rowkey(brand, profileId), columnFamilyName, column)
mutator.execute()

        addDevices(brandCode, profileID, deviceIDs:List[String])
var cnt = 0val mutator: Mutator[Composite] = HFactory.createMutator(ksp, CompositeSerializer.get)
for (deviceId <- deviceIds) {
  cnt += 1  val column = HFactory.createColumn(deviceId, "")
  column.setClock(ksp.createClock)
  mutator.addInsertion(rowkey(brand, profileId), columnFamilyName, column)
  if ((cnt % batchSize) == 0 || deviceIds.length <= batchSize)
    mutator.execute()
}

def removeDevice(brand: String, profileId: Profile.Id, deviceId: String): Unit = {
  //Remove column  val mutator: Mutator[Composite] = HFactory.createMutator(ksp, CompositeSerializer.get)
  mutator.addDeletion(rowkey(brand, profileId), columnFamilyName, deviceId, StringSerializer.get)
  mutator.execute()
}

def removeDevices(brand: String, profileId: Profile.Id, deviceIds: List[String]): Unit = {
  //Remove devices  var cnt = 0  val mutator: Mutator[Composite] = HFactory.createMutator(ksp, CompositeSerializer.get)
  for (deviceId <- deviceIds) {
    cnt += 1    mutator.addDeletion(rowkey(brand, profileId), columnFamilyName, deviceId, StringSerializer.get)
    if ((cnt % batchSize) == 0 || deviceIds.length <= batchSize)
      mutator.execute()
  }
}

def update(brandCode: String, deviceId: String, attributes: Attributes, profiles: Set[Profile]): Unit = {
  val mutator: Mutator[Composite] = HFactory.createMutator(ksp, CompositeSerializer.get)
  val column = HFactory.createColumn(deviceId, Array[Byte]())
  val clock = ksp.createClock
  column.setClock(clock)
  profiles.foreach { p =>
    if (p(attributes))
      mutator.addInsertion(rowkey(brandCode, p.id.get), columnFamilyName, column)
    else      mutator.addDeletion(rowkey(brandCode, p.id.get), columnFamilyName, deviceId, StringSerializer.get, clock)
  }
  mutator.execute()
}

Take these as example, then
CREATE TABLE attributes(
brandCode text,
deviceID text,
PRIMARY KEY ( brandCode, deviceID )
);

>alter table attributes add t1499356374 varchar;
>insert into attributes (brandcode, deviceid, t1499356373) values ('1','1','good device');

We can have dynamic columns for one key set.

Redshift
Yesterday, I connect to our redshift database to query some data as well.

We used this driver
https://s3.amazonaws.com/redshift-downloads/drivers/RedshiftJDBC41-1.2.1.1001.jar

And we download this open source tool
http://www.sql-workbench.net/downloads.html

Create a new profile from Manage Drivers

References:
https://github.com/spring-projects/spring-boot/tree/v2.0.0.M1/spring-boot-samples/spring-boot-sample-data-cassandra





分享到:
评论

相关推荐

    Spring Boot 教程、技术栈示例代码,快速简单上手教程。

    - `spring-boot-web-example`:展示如何创建一个简单的 RESTful API。 - `spring-boot-data-jpa-example`:演示如何使用 Spring Data JPA 和 Hibernate 进行数据库操作。 - `spring-boot-security-example`:介绍...

    spring boot中文参考指南

    同时,Spring MVC作为Spring Boot的默认Web框架,提供模型-视图-控制器架构,方便构建RESTful API。Spring Boot还支持Thymeleaf、Freemarker等模板引擎,用于生成动态HTML页面。 在微服务领域,Spring Boot更是大放...

    spring-boot-mvc-cassandra-test

    Spring Data Cassandra是Spring Framework的一部分,它为Cassandra提供了易于使用的API,使得在Spring Boot应用中操作Cassandra变得简单。它支持定义CQL(Cassandra查询语言)实体、CRUD操作、查询方法以及事件监听...

    Spring Boot in Action.rar

    7. **RESTful API**:Spring Boot推荐使用Spring MVC来构建RESTful服务,配合Swagger等工具进行API文档的生成和测试。 8. **安全**:Spring Boot集成了Spring Security,提供了基础的身份验证和授权功能,可以轻松...

    Spring Boot 是一个用于快速构建基于 Java 的企业级应用程序的开源框架

    2. RESTful服务:利用Spring MVC轻松创建RESTful API,提供JSON序列化和反序列化的支持。 3. 安全:集成Spring Security,提供身份验证和授权功能。 4. 任务调度:可以使用Spring Task或Quartz进行定时任务的配置。 ...

    spring boot 集成框架事例

    如果采用RESTful API设计,Spring Boot的`@RestController`和`@RequestMapping`等注解能轻松实现API接口。 接着,Spring Boot也支持数据库集成,包括关系型数据库(如MySQL、PostgreSQL)和NoSQL数据库(如MongoDB...

    中文 Spring Boot 参考指南

    1. **RESTful API**:Spring Boot 提供了 `@RestController` 和 `@RequestMapping` 等注解,方便构建 RESTful 风格的服务。 2. **模板引擎**:支持 Thymeleaf、FreeMarker、Groovy Templates 等模板引擎,用于动态...

    spring-boot-reference-zh

    7. **Web 开发**:Spring Boot 支持 MVC 模式,提供了诸如模板引擎(Thymeleaf、Freemarker)、RESTful API 开发等特性。 8. **数据访问**:支持多种数据访问技术,包括 JDBC、JPA(Hibernate)、MyBatis 等,以及...

    Spring Boot 中文文档 参考手册.pdf

    使用Spring Boot进行Web开发,可以通过`@RestController`和`@RequestMapping`注解创建RESTful API。Spring MVC是其基础,支持模板引擎如Thymeleaf和Freemarker,以及WebSocket等高级功能。 7. **数据访问**: ...

    Spring Boot教程系列(ImportNew ).rar

    在Web开发上,Spring Boot与Spring MVC紧密集成,提供了一套完整的RESTful API开发解决方案。它支持模板引擎如Thymeleaf、FreeMarker,也可以方便地构建WebSocket应用。 安全方面,Spring Boot通过Spring Security...

    spring boot 2.1.1代码示例

    9. **数据访问**:Spring Boot支持多种数据存储选项,包括JDBC、JPA、MongoDB、Cassandra等,提供了统一的API,简化了数据访问。 10. **集成测试**:Spring Boot Test模块提供了对单元测试和集成测试的良好支持,...

    Spring-Boot-2.0-Samples.rar

    例如,`spring-boot-starter-data-jpa`可以快速引入JPA和Hibernat支持,`spring-boot-starter-data-rest`则可以快速实现RESTful API。 3. **内嵌式Web服务器**:Spring Boot可以内嵌Tomcat、Jetty或Undertow等Web...

    Spring Boot快速搭建和部署应用程序

    ### Spring Boot快速搭建和部署应用程序的关键知识点 #### 一、Spring Boot概述 - **定义**:Spring Boot是一种基于Java的开发框架,旨在简化并加速应用程序的构建过程。 - **背景**:由Spring团队开发,构建在...

    nefarious-octo-prune:Spring-boot + Cassandra + SwaggerUI RESTful

    这个项目的核心是结合了三个重要的技术组件:Spring Boot、Cassandra 和 SwaggerUI,来创建一个RESTful API服务。让我们逐一深入探讨这些技术及其在项目中的作用。 **Spring Boot** Spring Boot是Spring框架的一个...

    spring-boot-learning-master.zip

    12. **RESTful API 设计**:Spring Boot 鼓励使用 RESTful 风格设计 API,结合 Swagger 可生成 API 文档。 这个"spring-boot-learning-master.zip"文件可能包含从基础概念到高级特性的各种教程,帮助开发者逐步掌握...

    Spring Data REST简化RESTful开发.docx

    Spring Data REST 是基于 Spring Boot 和 Spring Data 项目构建 RESTful API 的强大工具,极大地简化了开发者的工作。在传统的 RESTful 开发中,我们需要手动编写控制器(Controller)来处理 HTTP 请求并映射到业务...

    干净可用spring-boot的项目

    7. **Web开发**:Spring Boot结合了Spring MVC,提供了RESTful API开发的支持。同时,通过Thymeleaf、FreeMarker或JSP等模板引擎可以方便地开发Web界面。 8. **数据访问**:Spring Boot支持多种数据库,如JDBC、...

    SpringBoot全家桶

    3. **Spring Data**:Spring Boot对Spring Data项目提供了良好的支持,包括JPA、MongoDB、Cassandra等,使得数据访问变得更加简单。只需少量代码,即可实现CRUD操作。 4. **Web开发**:Spring Boot集成了Spring MVC...

    spring-boot-samples:Spring Boot 系列样例演示项目代码库

    1. **Web 开发**:展示了如何使用 Spring MVC 构建 RESTful API,以及 Thymeleaf、FreeMarker 等模板引擎进行 Web 页面开发。 2. **数据访问**:包含与数据库交互的例子,如 JPA(Java Persistence API)与 ...

    springboot项目最详细示例

    这些示例将演示如何使用 Spring Boot 创建不同类型的项目,例如 Web 应用、RESTful API、数据访问、定时任务等,有助于学习者深入理解 Spring Boot 的工作原理和实践应用。通过研究这些示例,你可以逐步掌握 Spring ...

Global site tag (gtag.js) - Google Analytics