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

Cassandra Database(3)Hector Scala Class

 
阅读更多

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

分享到:
评论

相关推荐

    java NoSql Cassandra hector

    3. 定义Keyspace和ColumnFamily:Cassandra中的数据存储在Keyspace(类似数据库)和ColumnFamily(类似表)中。Hector提供了相应的API来定义它们。 ```java KeyspaceDef keyspaceDef = HFactory....

    scala-cassandra-example:Scala-Cassandra-示例

    **Scala与Cassandra整合实战** 在当今大数据处理和实时分析领域,Scala和Cassandra都是备受推崇的技术。Scala作为一种强大的函数式编程语言,以其丰富的库和与Java的无缝集成,深受开发者的喜爱。而Cassandra则是一...

    Cassandra JDBC Driver

    2. **遵循JDBC规范**:Cassandra JDBC Driver严格遵循Java Database Connectivity (JDBC) API规范,使得熟悉JDBC的开发者能够轻松地将Cassandra集成到他们的Java应用中,利用已有的JDBC知识进行数据存取操作。...

    Learning Apache Cassandra - Second Edition

    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 ...

    apress.beginning.apache.cassandra.development.1484201434

    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 ...

    Mastering Apache Cassandra

    ### Apache Cassandra 掌控指南 #### 一、引言 在大数据时代,高效的数据存储与管理变得至关重要。《Mastering Apache Cassandra》这本书旨在帮助读者掌握 Apache Cassandra 的核心技术和最佳实践,使其能够在处理...

    spring boot与cassandra集成,使用JPA方式。

    在本文中,我们将深入探讨如何将Spring Boot框架与Cassandra数据库集成,并利用Java Persistence API (JPA) 进行数据操作。Spring Boot以其简洁的配置和开箱即用的特性,已经成为Java开发中的首选框架之一。而...

    NoSQL Web Development with Apache Cassandra(2015)

    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 ...

    Mastering.Apache.Cassandra.2nd.Edition.1784392618

    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. ...

    Laravel开发-laravel-cassandra

    Phpdna\Cassandra\CassandraServiceProvider::class, ``` 并创建一个配置文件 `config/cassandra.php`,以自定义更多连接选项。 **3. 使用 Laravel-Cassandra** 安装并配置完成后,可以开始使用 Laravel-...

    Laravel开发-cassandra

    接下来,在`config/database.php`配置文件中添加Cassandra的连接信息: ```php 'cassandra' =&gt; [ 'default' =&gt; env('CASSANDRA_DEFAULT', 'default'), 'connections' =&gt; [ 'default' =&gt; [ 'contact_points' =&gt; ...

    cassandra介绍

    3. **高可用性与可扩展性**:Cassandra 能够承受单点故障,并保持服务的连续性;同时,它的架构允许轻松地通过增加节点来线性扩展系统的容量。 #### 三、数据模型 Cassandra 的数据模型是基于列族(Column Family)...

    Packt.Mastering.Apache.Cassandra.3rd.Edition

    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 CQL3 知识点详解 #### 一、Cassandra Query Language (CQL) 概览 Cassandra 查询语言(CQL)是专为 Apache Cassandra 设计的一种客户端与数据库交互的语言。CQL3 是 CQL 的第三个版本,自 Cassandra ...

    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 ...

    Cassandra The Definitive Guide(2nd) azw3

    Cassandra The Definitive Guide(2nd) 英文azw3 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

Global site tag (gtag.js) - Google Analytics