`
gigi_112
  • 浏览: 114461 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Pelops-----cassandra客户端封装(实现连接池)

阅读更多

   Pelops相传是cassandra的一个儿子,对于一个命途多舛的女人来说,儿子应该是她唯一的寄托,可见这个短小精悍的框架的强悍之处。

   这个框架的优点:

   1.基于thrift底层编写,速度快,实现优美,使用方便。

   2.实现了连接池,支持集群配置。

   3.对于集群的每个节点实现负载均衡。

   4.增加了对错误的处理。

   5.跟踪最新的cassandra版本,不会每次做很大的改动。

   下面就来看看pelops是如何封装的。

   1.建立连接池:

 

Pelops.addPool(
    "Main",
    new String[] { "cass1.database.com", "cass2.database.com", "cass3.database.com"},
    9160,
    new Policy());

 

2.插入删除数据:Mutator

 

Mutator mutator = Pelops.createMutator("Main", "SupportTickets");
/**
 * Write multiple sub-column values to a super column...
 * @param rowKey                    The key of the row to modify
 * @param colFamily                 The name of the super column family to operate on
 * @param colName                   The name of the super column
 * @param subColumns                A list of the sub-columns to write
 */
mutator. writeSubColumns(
    userId,
    "L1Tickets",
    UuidHelper.newTimeUuidBytes(), // using a UUID value that sorts by time
    mutator.newColumnList(
        mutator.newColumn("category", "videoPhone"),
        mutator.newColumn("reportType", "POOR_PICTURE"),
        mutator.newColumn("createdDate", NumberHelper.toBytes(System.currentTimeMillis())),
        mutator.newColumn("capture", jpegBytes),
        mutator.newColumn("comment") ));

/**
 * Delete a list of columns or super columns...
 * @param rowKey                    The key of the row to modify
 * @param colFamily                 The name of the column family to operate on
 * @param colNames                  The column and/or super column names to delete
 */
mutator.deleteColumns(
    userId,
    "L1Tickets",
    resolvedList);


mutator.execute(ConsistencyLevel.ONE);

 

3.读取数据 Selector

 

Selector selector = Pelops.createSelector("Main", "SupportTickets");
/**
 * Retrieve a super column from a row...
 * @param rowKey                        The key of the row
 * @param columnFamily                  The name of the column family containing the super column
 * @param superColName                  The name of the super column to retrieve
 * @param cLevel                        The Cassandra consistency level with which to perform the operation
 * @return                              The requested SuperColumn
 */
SuperColumn ticket = selector.getSuperColumnFromRow(
    userId,
    "L1Tickets",
    ticketId,
    ConsistencyLevel.ONE);


// enumerate sub-columns
for (Column data : ticket.columns) {
    String name = data.name;
    byte[] value = data.value;
}

/**
 * Retrieve super columns from a row
 * @param rowKey                        The key of the row
 * @param columnFamily                  The name of the column family containing the super columns
 * @param colPredicate                  The super column selector predicate
 * @param cLevel                        The Cassandra consistency level with which to perform the operation
 * @return                              A list of matching columns
 */
List<SuperColumn> allTickets = selector.getSuperColumnsFromRow(
    userId,
    "L1Tickets",
    Selector.newColumnsPredicateAll(true, 10000),
    ConsistencyLevel.ONE);

/**
 * Retrieve super columns from a set of rows.
 * @param rowKeys                        The keys of the rows
 * @param columnFamily                   The name of the column family containing the super columns
 * @param colPredicate                   The super column selector predicate
 * @param cLevel                         The Cassandra consistency level with which to perform the operation
 * @return                               A map from row keys to the matching lists of super columns
 */
Map<String, List<SuperColumn>> allTicketsForFriends = selector.getSuperColumnsFromRows(
    Arrays.asList(new String[] { "matt", "james", "dom" }, // the friends
    "L1Tickets",
    Selector.newColumnsPredicateAll(true, 10000),
    ConsistencyLevel.ONE);

/**
 * Retrieve a page of super columns composed from a segment of the sequence of super columns in a row.
 * @param rowKey                        The key of the row
 * @param columnFamily                  The name of the column family containing the super columns
 * @param startBeyondName               The sequence of super columns must begin with the smallest super column name greater than this value. Pass null to start at the beginning of the sequence.
 * @param orderType                     The scheme used to determine how the column names are ordered
 * @param reversed                      Whether the scan should proceed in descending super column name order
 * @param count                         The maximum number of super columns that can be retrieved by the scan
 * @param cLevel                        The Cassandra consistency level with which to perform the operation
 * @return                              A page of super columns
 */
List<SuperColumn> pageTickets = getPageOfSuperColumnsFromRow(
    userId,
    "L1Tickets",
    lastIdOfPrevPage, // null for first page
    Selector.OrderType.TimeUUIDType, // ordering defined in this super column family
    true, // blog order
    10, // count shown per page
    ConsistencyLevel.ONE);


 

参考文章:

http://ria101.wordpress.com/2010/06/11/pelops-the-beautiful-cassandra-database-client-for-java/

Pelops源代码:http://pelops.googlecode.com/

1
0
分享到:
评论

相关推荐

    kundera-cassandra-pelops-2.12.zip

    【标题】"Kundera-Cassandra-Pelops-2.12.zip" 是一个与Apache Cassandra数据库相关的开源项目,Kundera是一个对象关系映射(ORM)框架,专为分布式NoSQL数据库设计,特别是针对Cassandra。这个版本2.12的Kundera包含...

    Python库 | Pelops-0.1a23.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:Pelops-0.1a23.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    pelops:--

    标题和描述都为空,标签也未给出任何线索,而“pelops-main”可能是一个项目或软件的主文件夹名,但这本身并不足以构建一个详尽的知识点解释。为了提供更详细的信息,我需要这个“pelops”是关于什么的,例如,它...

    scale7-pelops, 用于访问Cassandra数据库的Java库.zip

    scale7-pelops, 用于访问Cassandra数据库的Java库 简介Pelops已经创建成了与Cassandra一起工作的漂亮东西( 所以昵称很漂亮"Cassandra 。 使用Pelops开发人员可以以快速访问to的完全功能,同时编写清晰的代码,使基础...

    cassandra入门demo

    Pelops是Java的一个Cassandra客户端库,方便在Java应用中使用Cassandra。在项目中添加Pelops依赖,然后通过以下代码建立连接并执行操作: ```java import org.apache.cassandra.thrift.*; // 创建连接 ThriftClient...

Global site tag (gtag.js) - Google Analytics