Cassandra Database(3)Hector Scala Class
Actually, I am using the latest cassandra from branch 1.2 and I build it myself.
1. Creating the Schema and Insert/Get
The case class object will be as follow
package com.sillyat.easycassandraserver.models
import org.joda.time.DateTime
case class Product(id: Option[Long], productName: String, create: DateTime, productPrice: BigDecimal, productCode: String, country: String)
The sample of the Schema and Insert/Get
package com.sillycat.easycassandraserver.apps
import me.prettyprint.hector.api.{Keyspace, Cluster}
import me.prettyprint.hector.api.factory.HFactory
import me.prettyprint.cassandra.model.BasicColumnFamilyDefinition
import me.prettyprint.hector.api.ddl.{KeyspaceDefinition, ColumnFamilyDefinition, ComparatorType}
import me.prettyprint.cassandra.service.ThriftCfDef
import java.util.Arrays
import me.prettyprint.hector.api.mutation.Mutator
import me.prettyprint.cassandra.serializers.{LongSerializer, StringSerializer, BigDecimalSerializer}
import com.sillyat.easycassandraserver.models.Product
import org.joda.time.DateTime
import me.prettyprint.hector.api.query.{QueryResult, ColumnQuery}
import me.prettyprint.hector.api.beans.HColumn
object CassandraShemaDataDemoApp extends App{
val clusterName = "TestCluster"
val host = "localhost"
val port = 9160
val keyspaceName = "sillycat"
val columnFamilyName = "products"
val cluster: Cluster = HFactory.getOrCreateCluster("TestCluster", host + ":" + 9160)
//drop the schema
cluster.describeKeyspace(keyspaceName) match {
case null => println("There is no keyspace yet.")
case _ => cluster.dropKeyspace(keyspaceName)
}
//define the schema
val columnFamilyProducts: BasicColumnFamilyDefinition = new BasicColumnFamilyDefinition()
columnFamilyProducts.setKeyspaceName(keyspaceName)
columnFamilyProducts.setName(columnFamilyName)
columnFamilyProducts.setKeyValidationClass(ComparatorType.UTF8TYPE.getClassName())
columnFamilyProducts.setComparatorType(ComparatorType.UTF8TYPE)
//columnFamilyProducts.setDefaultValidationClass(ComparatorType.UTF8TYPE.getClassName())
//columnFamilyProducts.setDefaultValidationClass(ComparatorType.DECIMALTYPE.getClassName())
//columnFamilyProducts.setDefaultValidationClass(ComparatorType.ASCIITYPE.getClassName())
//columnFamilyProducts.setDefaultValidationClass(ComparatorType.COMPOSITETYPE.getClassName())
val productsFamily: ColumnFamilyDefinition = new ThriftCfDef(columnFamilyProducts);
val keyspaceDefinition: KeyspaceDefinition =
HFactory.createKeyspaceDefinition(keyspaceName, "org.apache.cassandra.locator.SimpleStrategy",
1, Arrays.asList(productsFamily))
cluster.addKeyspace(keyspaceDefinition);
//insert data
val keyspaceOperator: Keyspace = HFactory.createKeyspace(keyspaceName, cluster)
val mutator: Mutator[String] = HFactory.createMutator(keyspaceOperator, StringSerializer.get())
val p1 = Product(Some(1), "IPHONE5", DateTime.now, 499.99, "iphone5", "China")
val p2 = Product(Some(2), "IPHONE4", DateTime.now, 199.99, "iphone4", "US")
val p3 = Product(Some(3), "IPHONE4S", DateTime.now, 299.99, "iphone4s", "TW")
//single insert p1
mutator.insert(
p1.productCode,
columnFamilyName,
HFactory.createColumn("ProductName",
p1.productName,
StringSerializer.get(),
StringSerializer.get()
)
)
mutator.insert(
p1.productCode,
columnFamilyName,
HFactory.createColumn("Country",
p1.country,
StringSerializer.get(),
StringSerializer.get()
)
)
mutator.insert(
p1.productCode,
columnFamilyName,
HFactory.createColumn("ProductPrice",
java.math.BigDecimal.valueOf(p1.productPrice.doubleValue()),
StringSerializer.get(),
BigDecimalSerializer.get()
)
)
//get p1
val bigdecimalColumnQuery: ColumnQuery[String, String, java.math.BigDecimal] = HFactory.createColumnQuery(keyspaceOperator,
StringSerializer.get(),
StringSerializer.get(),
BigDecimalSerializer.get())
bigdecimalColumnQuery.setColumnFamily(columnFamilyName).setKey(p1.productCode).setName("ProductPrice")
val result1: QueryResult[HColumn[String, java.math.BigDecimal]] = bigdecimalColumnQuery.execute()
val stringColumnQuery: ColumnQuery[String, String, String] = HFactory.createColumnQuery(keyspaceOperator,
StringSerializer.get(),
StringSerializer.get(),
StringSerializer.get())
stringColumnQuery.setColumnFamily(columnFamilyName).setKey(p1.productCode).setName("ProductName")
val result2: QueryResult[HColumn[String, String]] = stringColumnQuery.execute()
stringColumnQuery.setColumnFamily(columnFamilyName).setKey(p1.productCode).setName("Country")
val result3: QueryResult[HColumn[String, String]] = stringColumnQuery.execute()
println("Read HColumn from cassandra: " + result1.get())
println("Read HColumn from cassandra: " + result2.get())
println("Read HColumn from cassandra: " + result3.get())
println("Verify on CLI with: get products['iphone5']; ")
//multiple insert p2,p3
//p2
mutator.addInsertion(
p2.productCode,
columnFamilyName,
HFactory.createColumn("ProductName",
p2.productName,
StringSerializer.get(),
StringSerializer.get()
)
)
mutator.addInsertion(
p2.productCode,
columnFamilyName,
HFactory.createColumn("Country",
p2.country,
StringSerializer.get(),
StringSerializer.get()
)
)
mutator.addInsertion(
p2.productCode,
columnFamilyName,
HFactory.createColumn("ProductPrice",
java.math.BigDecimal.valueOf(p2.productPrice.doubleValue()),
StringSerializer.get(),
BigDecimalSerializer.get()
)
)
//p3
mutator.addInsertion(
p3.productCode,
columnFamilyName,
HFactory.createColumn("ProductName",
p3.productName,
StringSerializer.get(),
StringSerializer.get()
)
)
mutator.addInsertion(
p3.productCode,
columnFamilyName,
HFactory.createColumn("Country",
p3.country,
StringSerializer.get(),
StringSerializer.get()
)
)
mutator.addInsertion(
p3.productCode,
columnFamilyName,
HFactory.createColumn("ProductPrice",
java.math.BigDecimal.valueOf(p3.productPrice.doubleValue()),
StringSerializer.get(),
BigDecimalSerializer.get()
)
)
mutator.execute()
bigdecimalColumnQuery.setColumnFamily(columnFamilyName).setKey(p2.productCode).setName("ProductPrice")
val result4: QueryResult[HColumn[String, java.math.BigDecimal]] = bigdecimalColumnQuery.execute()
println("Read HColumn from cassandra: " + result4.get())
println("Verify on CLI with: get products['iphone4s']; ")
cluster.getConnectionManager.shutdown()
}
The most important part is to remember hector is coming from Java, so, sometimes we need to use
java.math.BigDecimal instead of BigDecimal in scala world.
2. The Querys
package com.sillycat.easycassandraserver.apps
import me.prettyprint.hector.api.{Keyspace, Cluster}
import me.prettyprint.hector.api.factory.HFactory
import me.prettyprint.hector.api.query.{MultigetSliceQuery, QueryResult, RangeSlicesQuery}
import com.sillyat.easycassandraserver.models.Product
import scala.Product
import org.joda.time.DateTime
import me.prettyprint.hector.api.beans.{Row, Rows, OrderedRows}
import me.prettyprint.cassandra.serializers.{BigDecimalSerializer, StringSerializer}
import scala.collection.JavaConversions._
object CassandraQueryDemoApp extends App{
val clusterName = "TestCluster"
val host = "localhost"
val port = 9160
val keyspaceName = "sillycat"
val columnFamilyName = "products"
val cluster: Cluster = HFactory.getOrCreateCluster("TestCluster", host + ":" + 9160)
val p1 = Product(Some(1), "IPHONE5", DateTime.now, 499.99, "iphone5", "China")
val p2 = Product(Some(2), "IPHONE4", DateTime.now, 199.99, "iphone4", "US")
val p3 = Product(Some(3), "IPHONE4S", DateTime.now, 299.99, "iphone4s", "TW")
val keyspaceOperator: Keyspace = HFactory.createKeyspace(keyspaceName, cluster)
//range query
val bigDecimalRangeSlicesQuery: RangeSlicesQuery[String, String, java.math.BigDecimal] =
HFactory.createRangeSlicesQuery(keyspaceOperator, StringSerializer.get(), StringSerializer.get(), BigDecimalSerializer.get())
bigDecimalRangeSlicesQuery.setColumnFamily(columnFamilyName)
bigDecimalRangeSlicesQuery.setKeys(p2.productCode, p2.productCode)
bigDecimalRangeSlicesQuery.setColumnNames("ProductPrice")
val result1: QueryResult[OrderedRows[String, String, java.math.BigDecimal]] = bigDecimalRangeSlicesQuery.execute()
val orderedRows1: OrderedRows[String, String, java.math.BigDecimal]= result1.get()
orderedRows1.toList.foreach( row => println("result1: " + row.getColumnSlice))
val stringRangeSlicesQuery: RangeSlicesQuery[String, String, String] =
HFactory.createRangeSlicesQuery(keyspaceOperator, StringSerializer.get(), StringSerializer.get(), StringSerializer.get())
stringRangeSlicesQuery.setColumnFamily(columnFamilyName)
stringRangeSlicesQuery.setKeys(p2.productCode, p2.productCode)
stringRangeSlicesQuery.setColumnNames("ProductName", "Country")
val result2: QueryResult[OrderedRows[String, String, String]] = stringRangeSlicesQuery.execute()
val orderedRows2: OrderedRows[String, String, String]= result2.get()
orderedRows2.toList.foreach( row => println("result2: " + row.getColumnSlice))
// createIndexedSlicesQuery time between some date
// indexedSlicesQuery.addEqualsExpression("birthdate", 1975L);
// indexedSlicesQuery.addGtExpression("birthmonth", 6L);
// indexedSlicesQuery.addLtExpression("birthmonth", 8L);
// indexedSlicesQuery.setColumnNames("birthdate","birthmonth");
// indexedSlicesQuery.setColumnFamily("Indexed1");
// indexedSlicesQuery.setStartKey("");
//multiget
val multigetSliceQuery: MultigetSliceQuery[String, String, String] =
HFactory.createMultigetSliceQuery(keyspaceOperator, StringSerializer.get(), StringSerializer.get(), StringSerializer.get())
multigetSliceQuery.setColumnFamily(columnFamilyName)
multigetSliceQuery.setKeys(p1.productCode, p2.productCode, p3.productCode)
multigetSliceQuery.setRange(null, null, false, 3) //number 3 is the number of the columns
println(multigetSliceQuery)
val result3: QueryResult[Rows[String, String, String]] = multigetSliceQuery.execute()
val orderedRows3: Rows[String, String, String] = result3.get()
orderedRows3.toList.foreach(row => println("result3: " + row.getColumnSlice))
//pagenation
val pagenationSliceQuery: RangeSlicesQuery[String, String, String] =
HFactory.createRangeSlicesQuery(keyspaceOperator, StringSerializer.get(), StringSerializer.get(), StringSerializer.get())
pagenationSliceQuery.setColumnFamily(columnFamilyName)
pagenationSliceQuery.setRange("", "", false, 3)
pagenationSliceQuery.setRowCount(3)
val result4: QueryResult[OrderedRows[String, String, String]] = pagenationSliceQuery.execute()
val orderedRows4: OrderedRows[String, String, String] = result4.get()
orderedRows4.toList.foreach(row => println("result4: " + row.getColumnSlice))
val lastRow: Row[String,String,String] = orderedRows4.peekLast()
pagenationSliceQuery.setKeys(lastRow.getKey(), "")
val orderedRows5 = pagenationSliceQuery.execute().get()
orderedRows5.toList.foreach(row => println("result5: " + row.getColumnSlice))
}
How to translate java collections to scala
import scala.collection.JavaConversions._
Tips:
1. Error Message:
type mismatch;
found : me.prettyprint.cassandra.serializers.BigDecimalSerializer
required: me.prettyprint.hector.api.Serializer[Number]
Note: java.math.BigDecimal <: Number (and me.prettyprint.cassandra.serializers.BigDecimalSerializer <: me.prettyprint.cassandra.serializers.AbstractSerializer[java.math.BigDecimal]), but Java-defined trait Serializer is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: Number`. (SLS 3.2.10)
p1.productPrice, StringSerializer.get(),BigDecimalSerializer.get()))
^
Solution:
mutator.insert(
p1.productCode,
columnFamilyName,
HFactory.createColumn("ProductPrice",
java.math.BigDecimal.valueOf(p1.productPrice.doubleValue()),
StringSerializer.get(),
BigDecimalSerializer.get()
)
)
val bigdecimalColumnQuery: ColumnQuery[String, String, java.math.BigDecimal] = HFactory.createColumnQuery(keyspaceOperator,
StringSerializer.get(),
StringSerializer.get(),
BigDecimalSerializer.get())
bigdecimalColumnQuery.setColumnFamily(columnFamilyName).setKey(p1.productCode).setName("ProductPrice")
val result1: QueryResult[HColumn[String, java.math.BigDecimal]] = bigdecimalColumnQuery.execute()
References:
http://fbc.pionier.net.pl/pro/clepsydra/storage/clepsydra-nosql-driver/xref/pl/psnc/synat/a9/nosqldriver/cassandra/CassandraDataMutator.html
- 浏览: 2542737 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
MySQL HA Solution 2019(4)MaxScale
2019-08-03 21:19 788MySQL HA Solution 2019(4)MaxSca ... -
MySQL HA Solution 2019(3)MyCat
2019-07-30 11:45 369MySQL HA Solution 2019(3)MyCat ... -
MySQL HA Solution 2019(2)ProxySQL
2019-07-29 09:39 484MySQL HA Solution 2019(2)ProxyS ... -
MySQL HA Solution 2019(1)Master Slave on MySQL 5.7
2019-07-27 22:26 514MySQL HA Solution 2019(1)Master ... -
Zeppelin and Spark Library Dependency
2019-07-09 03:20 457Zeppelin and Spark Library Depe ... -
PostgreSQL 2018(3)Trigger
2018-10-30 00:12 485PostgreSQL 2018(3)Trigger Some ... -
PostgreSQL 2018(2)Dump the Data to CSV
2018-05-05 23:09 564PostgreSQL 2018(2)Dump the Data ... -
PostgreSQL 2018(1)Installation and Introduction
2018-05-04 01:45 630PostgreSQL 2018(1)Installation ... -
SOLR Performance and SolrJ(1)Client Compare Java VS PHP
2017-03-15 06:10 673SOLR Performance and SolrJ(1)Cl ... -
Regis Memory and Cleanup
2017-02-22 06:44 652Regis Memory and Cleanup Get t ... -
Mysql Database Backup and Dump
2016-12-22 06:06 699Mysql Database Backup and Dump ... -
Mysql Database Event and Procedure(3)Stored Procedure
2016-12-07 03:54 676Mysql Database Event and Proced ... -
Mysql Database Event and Procedure(2)Store Procedure
2016-12-07 03:38 582Mysql Database Event and Proced ... -
Mysql Fails on MAC
2015-10-20 03:06 783Mysql Fails on MAC Today, I pl ... -
FIPS and County Code Lookup
2015-10-13 05:19 985FIPS and County Code Lookup Th ... -
MAC OS with Latest Mysql
2015-01-07 12:35 966MAC OS with Latest Mysql C ... -
Cassandra Database(8)Upgrade to 2.1.2 and OpsCenter
2014-12-26 13:12 1243Cassandra Database(8)Upgrade t ... -
Kafka(6)How to Monitor Kafka
2014-08-23 04:19 2247Kafka(6)How to Monitor Kafka1. ... -
neo4j(4)Java Sample/Scala Client and Routing
2014-08-21 23:15 1396neo4j(4)Java Sample/Scala Clie ... -
neo4j(3)Setup neo4j Server and Know Concepts
2014-08-20 01:08 764neo4j(3)Setup neo4j Server and ...
相关推荐
3. 定义Keyspace和ColumnFamily:Cassandra中的数据存储在Keyspace(类似数据库)和ColumnFamily(类似表)中。Hector提供了相应的API来定义它们。 ```java KeyspaceDef keyspaceDef = HFactory....
**Scala与Cassandra整合实战** 在当今大数据处理和实时分析领域,Scala和Cassandra都是备受推崇的技术。Scala作为一种强大的函数式编程语言,以其丰富的库和与Java的无缝集成,深受开发者的喜爱。而Cassandra则是一...
2. **遵循JDBC规范**:Cassandra JDBC Driver严格遵循Java Database Connectivity (JDBC) API规范,使得熟悉JDBC的开发者能够轻松地将Cassandra集成到他们的Java应用中,利用已有的JDBC知识进行数据存取操作。...
Master the advanced features available in Cassandra 3.x through a step-by-step tutorial and build a scalable, high performance database layer Book Description Cassandra is a distributed database ...
Beginning Apache Cassandra Development takes you through the Apache Cassandra NoSQL database and how to build applications that leverage its capabilities. As Java, PHP, Python, and JavaScript are the ...
### Apache Cassandra 掌控指南 #### 一、引言 在大数据时代,高效的数据存储与管理变得至关重要。《Mastering Apache Cassandra》这本书旨在帮助读者掌握 Apache Cassandra 的核心技术和最佳实践,使其能够在处理...
在本文中,我们将深入探讨如何将Spring Boot框架与Cassandra数据库集成,并利用Java Persistence API (JPA) 进行数据操作。Spring Boot以其简洁的配置和开箱即用的特性,已经成为Java开发中的首选框架之一。而...
Apache Cassandra is the most commonly used NoSQL database written in Java and is renowned in the industry as the only NoSQL solution that can accommodate the complex requirements of today’s modern ...
The book is aimed at intermediate developers with an understanding of core database concepts and want to become a master implementing Cassandra for their application. Table of Contents Chapter 1. ...
Phpdna\Cassandra\CassandraServiceProvider::class, ``` 并创建一个配置文件 `config/cassandra.php`,以自定义更多连接选项。 **3. 使用 Laravel-Cassandra** 安装并配置完成后,可以开始使用 Laravel-...
接下来,在`config/database.php`配置文件中添加Cassandra的连接信息: ```php 'cassandra' => [ 'default' => env('CASSANDRA_DEFAULT', 'default'), 'connections' => [ 'default' => [ 'contact_points' => ...
3. **高可用性与可扩展性**:Cassandra 能够承受单点故障,并保持服务的连续性;同时,它的架构允许轻松地通过增加节点来线性扩展系统的容量。 #### 三、数据模型 Cassandra 的数据模型是基于列族(Column Family)...
Build, manage, and configure high-performing, reliable NoSQL database for your applications with Cassandra Key Features Write programs more efficiently using Cassandra's features with the help of ...
### Cassandra CQL3 知识点详解 #### 一、Cassandra Query Language (CQL) 概览 Cassandra 查询语言(CQL)是专为 Apache Cassandra 设计的一种客户端与数据库交互的语言。CQL3 是 CQL 的第三个版本,自 Cassandra ...
The rising popularity of Apache Cassandra rests on its ability to handle very large data sets that include hundreds of terabytes -- and that's why this distributed database has been chosen by ...