Hector (API)
Encyclopedia
Hector is a high-level client API
Application programming interface
An application programming interface is a source code based specification intended to be used as an interface by software components to communicate with each other...

 for Apache Cassandra. Named after Hector
Hector
In Greek mythology, Hectōr , or Hektōr, is a Trojan prince and the greatest fighter for Troy in the Trojan War. As the first-born son of King Priam and Queen Hecuba, a descendant of Dardanus, who lived under Mount Ida, and of Tros, the founder of Troy, he was a prince of the royal house and the...

, the builder of Troy
Troy
Troy was a city, both factual and legendary, located in northwest Anatolia in what is now Turkey, southeast of the Dardanelles and beside Mount Ida...

 in Greek mythology
Greek mythology
Greek mythology is the body of myths and legends belonging to the ancient Greeks, concerning their gods and heroes, the nature of the world, and the origins and significance of their own cult and ritual practices. They were a part of religion in ancient Greece...

, it is a substitute for the Cassandra Java Client, or Thrift, that is encapsulated by Hector. It also has Maven
Maven
A maven is a trusted expert in a particular field, who seeks to pass knowledge on to others. The word maven comes from Hebrew, via Yiddish, and means one who understands, based on an accumulation of knowledge.-History:...

 repository access.

History

As Cassandra is shipped with the low-level Thrift (protocol)
Thrift (protocol)
Thrift is an interface definition language that is used to define and create services for numerous languages. It is used as a remote procedure call framework and was developed at Facebook for "scalable cross-language services development"...

, there was a potential to develop a better protocol for application developers. Hector was developed by Ran Tavory as a high-level interface that overlays the shortcomings of Thrift. It is licensed with the MIT License that allows to use, modify, split and change the design.

Over the last couple of days I got the conclusion that the java client I’ve been using so far to speak to cassanrda wasn’t satisfactory. I used the one simply called cassandra-java-client, which is a good start but had some shortcomings I could just not live with (no support for Cassandra v0.5, no JMX and no failover). So I’ve written my own.

Features

The high-level features of Hector are
  • A high-level object oriented interface to Cassandra: It is mainly inspired by the Cassandra-java-client. The API is defined in the Keyspace interface.
  • Connection pooling. As in high-scale applications, the usual pattern for DAO
    Data Access Object
    In computer software, a data access object is an object that provides an abstract interface to some type of database or persistence mechanism, providing some specific operations without exposing details of the database. It provides a mapping from application calls to the persistence layer...

    s is a large number of reads/writes. It is too expensive for clients to open new connections with each request. So, a client may easily run out of available sockets, if it operates fast enough. Hector provides connection pooling and a nice framework that manages the details.
  • Failover support: As Cassandra is a distributed data store where hosts (nodes
    Node (networking)
    In communication networks, a node is a connection point, either a redistribution point or a communication endpoint . The definition of a node depends on the network and protocol layer referred to...

    ) may go down. Hector has its own failover policy
    Failover
    In computing, failover is automatic switching to a redundant or standby computer server, system, or network upon the failure or abnormal termination of the previously active application, server, system, or network...

    .
    Type Comment
    FAIL_FAST If an error occurs, it fails
    ON_FAIL_TRY_ONE_NEXT_AVAILABLE Tries one more host before giving up
    ON_FAIL_TRY_ALL_AVAILABLE Tries all available hosts before giving up
  • JMX support: Hector exposes JMX for many important runtime metrics, such as number of available connections, idle connections, error statistics.
  • Load balancing
    Load balancing
    Load balancing or load distribution may refer to:*Load balancing , balancing a workload amongst multiple computer devices*Load balancing , the storing of excess electrical power by power stations during low demand periods, for release as demand rises*Weight distribution, the apportioning of weight...

    : A simple load balancing exists in the newer version.
  • Supports the command design pattern to allow clients to concentrate on their business logic and let Hector take care of the required plumbing.

Load balancing

Hector follows two load balancing policies with the LoadBalancingPolicy interface. The default is called the LeastActiveBalancingPolicy and routes requests to the pools having the lowest number of active connections, ensuring a good spread of utilisation across the cluster. The RoundRobinBalancingPolicy is a simple round-robin
Round-robin
The term round-robin was originally used to describe a document signed by multiple parties in a circle to make it more difficult to determine the order in which it was signed, thus preventing a ringleader from being identified...

 distribution algorithm.

Pooling

The ExhaustedPolicy determines how the underlying client connection pools are controlled. Currently, three options are available:
Type Comment
WHEN_EXHAUSTED_FAIL Fails acquisition when no more clients are available
WHEN_EXHAUSTED_GROW The pool is automatically increased to react to load increases
WHEN_EXHAUSTED_BLOCK Block on acquisition until a client becomes available (the default)

Code examples

As an example, an implementation of a simple distributed hashtable over Cassandra is listed.

/**
* Insert a new value keyed by key
* @param key Key for the value
* @param value the String value to insert
*/
public void insert(final String key, final String value) throws Exception {
execute(new Command{
public Void execute(final Keyspace ks) throws Exception {
ks.insert(key, createColumnPath(COLUMN_NAME), bytes(value));
return null;
}
});
}

/**
* Get a string value.
* @return The string value; null if no value exists for the given key.
*/
public String get(final String key) throws Exception {
return execute(new Command{
public String execute(final Keyspace ks) throws Exception {
try {
return string(ks.getColumn(key, createColumnPath(COLUMN_NAME)).getValue);
} catch (NotFoundException e) {
return null;
}
}
});
}

/**
* Delete a key from cassandra
*/
public void delete(final String key) throws Exception {
execute(new Command{
public Void execute(final Keyspace ks) throws Exception {
ks.remove(key, createColumnPath(COLUMN_NAME));
return null;
}
});
}

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK