在 Apache Cassandra 中存储数据和访问数据

379 阅读5分钟

在 Apache Cassandra 中存储数据和访问数据

在本文中,我将重用上一节中的博客文章示例来展示 Apache Cassandra 的一些基本功能。

上文连接 :在 HBase 中存储数据和从 HBase 访问数据 - 掘金 (juejin.cn)

在上一文中,你对Apache Cassandra有了初步的了解;

现在,您将在此基础上进行构建并熟悉其更多功能。

要开始使用,请转到 Apache Cassandra 安装文件夹,并通过运行以下命令在前台启动服务器:

bin/cassandra -f

当服务器启动时,运行 cassandra-cli 或命令行客户端,如下所示:

bin/cassandra-cli-host localhost -port 9160

现在查询可用的键空间,如下所示:


show keyspaces;

您将看到系统和您可能已创建的任何其他密钥空间。

在上一文中,创建了一个名为 carDatastore 的示例密钥空间。

对于此示例,请借助以下脚本创建一个名为 BlogPosts 的新密钥空间:

/*schema-blogposts.txt*/ Avallable for

create keyspace BlogPosts download on

    with replication_factor=1

    and placement_strategy= 'org.apache. cassandra. locator.Simplestrategy';

use BlogPosts;

create column family post

    with comparator =UTF8Type

    and read_repair_chance=0.1

    and keys_cached=100

    and gc_grace=0

    and min_compaction_threshold = 5

    and max_compaction_threshold = 31

create column family multimedia

    with comparator =UTF8Type

    and read_repair_chance = 0.1

    and keys_cached=100

    and gc_grace=0

    and min_compaction_threshold=5

    and max_compaction_threshold=31;

接下来,添加博客文章示例数据点,如下所示:


Cassandra> use BlogPosts;

Authenticated to keyspace: BlogPosts 

cassandra> set post['post1']['title'] = 'an interesting blog post';

Value inserted.

cassandra> set post['post1']['author']='a blogger';

Value inserted.

cassandra> set post['post1']['body'] = 'interesting content'; 
Value inserted.

cassandra> set multimedia['post1']['header']= 'header.png'; 
Value inserted.

cassandra> set multimedia['post1']['body'] = 'body.mpeg'; 

Value inserted.

cassandra> set post['post2']['title']='yet an interesting blog post'; 

Value inserted.

cassandra> set post['post2']['author']='another blogger';
Value inserted.

cassandra> set post['post2']['body'] ='interesting content';
Value inserted.

cassandra> set multimedia['post2']['body-image']='body_image.png';
Value inserted.

cassandra> set multimedia['post2']['body-video'] = 'body_video.mpeg'; 
Value inserted.

仅此而已。示例已准备就绪。接下来,在博客文章密钥空间中查询数据。

查询 Apache Cassandra

假设您仍然登录到 cassandra-cli 会话并且 BlogPosts 密钥空间正在使用中,您可以像这样查询 Post1 数据:

get post['post1'];

=>(column=author,value=6120626c6f67676572, timestamp=1302061955309000) 

=>(column=body,value=696e746572657374696e6720636f6e74656e74,timestamp=1302062452072000)

=>(column=title,value=616e20696e746572657374696e6720626c6f6720706f7374, timestamp=1302061834881000)

Returned 3 results.

您还可以在多媒体列系列中查询特定列,例如帖子的正文视频,例如post2。查询和输出如下所示:

get multimedia['post2']['body-video'];

=> (column=body-video, value=626f64795f766964656f2e6d706567,

timestamp=1302062623668000) 

NOSQL 数据存储的语言绑定

尽管命令行是快速访问和查询 NoSQL 数据存储的便捷方法,但您可能希望编程语言接口在实际应用程序中与 NoSQL 配合使用。

由于 NoSQL 数据存储的类型和风格各不相同,编程接口和驱动程序的类型也各不相同。不过,总的来说,有足够的支持从流行的高级编程语言(如Python,Ruby,Java和PHP)访问NoSQL存储。

Thrift 安装运行

Apache Thrift是一个开源的跨语言服务开发框架。

它是一个代码生成器引擎,用于创建与各种不同的编程语言接口的服务。Thrift起源于Facebook,此后是开源的。

Apache Thrift本身是用 C 语言编写的。

要构建、安装、使用和运行 Thrift,请执行以下步骤:

1.从 http://incubator.apache.org/thrift/download/ 下载节俭。

2. 提取源分布。

3. 按照熟悉的配置、制作和制作安装例程构建和安装 Thrift。

5. 编写Thrift 服务定义。这是Thrift 最重要的部分。它是生成代码的基础定义。

5.使用 Thrift 编译器生成特定语言的源代码。

接下来,运行 Thrift 服务器,然后使用 Thrift 客户端连接到服务器。

您可能不需要为像 Apache Cassandra 这样的 NoSQL 存储生成 Thrift 客户端,它支持 Thrift 绑定,但可以使用特定于语言的客户端。

Java 语言绑定

Java是一种无处不在的编程语言。

它可能已经失去了一些荣耀,但它肯定没有失去它的受欢迎程度或普遍性。

MongoDB的制造商正式支持Java驱动程序。您可以在 www.mongodb.org/display/DOC… 下载并了解有关MongoDB Java驱动程序的更多信息,该驱动程序作为单个JAR文件分发。下载 JAR 后,只需将其添加到应用程序类路径中,就可以了。

使用 Java 驱动程序连接到该数据库并列出该集合的所有元素。

浏览示例中的代码,看看它是如何完成的。

public class JavaMongoDBClient{

        Mongo m;
        DB db;
        DBCollection coll;

        public void init()throws Exception {
            m=new Mongo( "localhost",27017);
            db=m.getDB("mydb");
            coll= db.getcollection("logdata"); }



        public void getLogData(){
            DBCursor cur=coll.find();

            while(cur.hasNext()){

                system.out.println(cur.next()); }

            }

        public static void main(String[] args){

            try{

                JavaMongoDBClient javaMongoDBClient = new JavaMongoDBClient();
                javaMongoDBClient.init();
                javaMongoDBClient.getLogData(); }

            catch(Exception e){

                e.printStackTrace(); }}

通过这个例子,很明显,从Java接口是简单方便的。

让我们继续讨论HBase。

若要查询在 HBase 中创建的集合,请先获取以下 JAR 文件并将它们添加到类路径中:

commons-logging-1.1.1.jar

hadoop-core-0.20-append-r1056497.jar 

hbase-0.90.1.jar

log4j-1.2.16.jar

要在 blogposts 数据存储中列出 post1 的title和author,请使用示例中的程序。

示例: 用于连接和查询 HBase 的 Java 程序

import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.io.RowResult;
import java.util.HashMap; import java.util.Map;
import java.io.IOException;


public class HBaseconnector{

public static Map retrievePost(String postId) throws IOException{
HTable table = new HTable(new HBaseConfiguration(),"blogposts");
Map post = new HashMap();

RowResult result = table.getRow(postId);

for {byte[] column:result.keyset()){

post.put(new String(column),new String(result.get(column).getvalue()));}}

return post;}}


public static void main(String[] args) throws IOException {
    Map blogpost = HBaseConnector.retrievePost("post1") ;

    System.out.println(blogpost.get("post:title")); 
    system.out.println(blogpost.get("post:author")); }

本文正在参加「金石计划 . 瓜分6万现金大奖」