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
- 浏览: 2490884 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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 718MySQL HA Solution 2019(4)MaxSca ... -
MySQL HA Solution 2019(3)MyCat
2019-07-30 11:45 329MySQL HA Solution 2019(3)MyCat ... -
MySQL HA Solution 2019(2)ProxySQL
2019-07-29 09:39 454MySQL HA Solution 2019(2)ProxyS ... -
MySQL HA Solution 2019(1)Master Slave on MySQL 5.7
2019-07-27 22:26 419MySQL HA Solution 2019(1)Master ... -
Zeppelin and Spark Library Dependency
2019-07-09 03:20 427Zeppelin and Spark Library Depe ... -
PostgreSQL 2018(3)Trigger
2018-10-30 00:12 463PostgreSQL 2018(3)Trigger Some ... -
PostgreSQL 2018(2)Dump the Data to CSV
2018-05-05 23:09 540PostgreSQL 2018(2)Dump the Data ... -
PostgreSQL 2018(1)Installation and Introduction
2018-05-04 01:45 591PostgreSQL 2018(1)Installation ... -
SOLR Performance and SolrJ(1)Client Compare Java VS PHP
2017-03-15 06:10 635SOLR Performance and SolrJ(1)Cl ... -
Regis Memory and Cleanup
2017-02-22 06:44 620Regis Memory and Cleanup Get t ... -
Mysql Database Backup and Dump
2016-12-22 06:06 672Mysql Database Backup and Dump ... -
Mysql Database Event and Procedure(3)Stored Procedure
2016-12-07 03:54 643Mysql Database Event and Proced ... -
Mysql Database Event and Procedure(2)Store Procedure
2016-12-07 03:38 549Mysql Database Event and Proced ... -
Mysql Fails on MAC
2015-10-20 03:06 748Mysql Fails on MAC Today, I pl ... -
FIPS and County Code Lookup
2015-10-13 05:19 955FIPS and County Code Lookup Th ... -
MAC OS with Latest Mysql
2015-01-07 12:35 924MAC OS with Latest Mysql C ... -
Cassandra Database(8)Upgrade to 2.1.2 and OpsCenter
2014-12-26 13:12 1211Cassandra Database(8)Upgrade t ... -
Kafka(6)How to Monitor Kafka
2014-08-23 04:19 2220Kafka(6)How to Monitor Kafka1. ... -
neo4j(4)Java Sample/Scala Client and Routing
2014-08-21 23:15 1377neo4j(4)Java Sample/Scala Clie ... -
neo4j(3)Setup neo4j Server and Know Concepts
2014-08-20 01:08 735neo4j(3)Setup neo4j Server and ...
相关推荐
java NoSql Cassandra hector 做的实例,拿出来分享
Scala-Cassandra-示例Scala-Cassandra-示例
Apache Cassandra is a massively scalable, peer-to-peer database designed for 100 percent uptime, with deployments in the tens of thousands of nodes, all supporting petabytes of data. This book offers ...
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 ...
cassandra cql3. pdf
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. ...
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 ...
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 ...
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 ...
Cassandra The Definitive Guide(2nd) 英文azw3 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Updated for Cassandra 3.0, this second edition provides the technical details and practical examples you need to assess this database management system and put it to work in a production environment....
Apache Cassandra is a massively scalable, peer-to-peer database designed for 100 percent uptime, with deployments in the tens of thousands of nodes, all supporting petabytes of data. This book offers ...
cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战...
Cassandra是一个混合型的非关系的数据库,类似于Google的BigTable。其主要功能比Dynomite(分布式的Key-Value存储系统)更丰富,但支持度却不如文档存储MongoDB(介于关系数据库和非关系数据库之间的开源产品,是非...
Scala搜索引擎 用 Scala 和 Akka 和 Cassandra 编写的玩具搜索引擎。 警告:不要将此代码用于任何重要的事情。 ##依赖 SBT 卡桑德拉 Redis (可选) 概述 履带式 索引器 应用程序接口 用户界面
cassandra cli 命令 大全
Cassandra jdbc driver,支持Cassandra2.x,3.x, 支持标准的jdbc规范,兼容Cassandra的cql语法。