Alice
Alice

Reputation: 144

How insert data to Cassandra DB via library php?

I use Cassandra-PHP-Client-Library library for get/set data to Apache Cassandra. I created a keyspace name via cqlsh coomanda line:

CREATE TABLE employees (
  company text,
  name text,
  age int,
  role text,
  PRIMARY KEY (company,name)
);

Then have inserted some columns also via command line:

INSERT INTO employees (company, name, age, role) VALUES ('OSC', 'eric', 38, 'ceo');
INSERT INTO employees (company, name, age, role) VALUES ('OSC', 'john', 37, 'dev');

Now I try insert any data from php, for this i use method set() of library:

$this->cassandra->set(
            'employees.company',
            array(
                'company' => '[email protected]',
                'name'    => 'Alice Rozenberg',
                'age'     => 45,
                'role'    => 'ddd'
            )
        );

In the official manual was told that first param of function set() is key row (employees is a name of column family, company is a key), and other - are name and value of columns. In this regard, I have a few questions:

What is a row key in my case? At the "CREATE" command I specifed, that primary key is "company,name". But this not correct for use in function. This option does not work:

$this->cassandra->set(
                'employees.name',
...

$this->cassandra->set(
                'employees.company',
...

I change to phpcassa library and have question about composites types:

From tutorial example: https://github.com/thobbs/phpcassa/blob/master/examples/composites.php

// Insert a few records
$key1 = array("key", 1);
$key2 = array("key", 2);

$columns = array(
    array(array(0, "a"), "val0a"),

    array(array(1, "a"), "val1a"),
    array(array(1, "b"), "val1b"),
    array(array(1, "c"), "val1c"),

    array(array(2, "a"), "val2a"),

    array(array(3, "a"), "val3a")
);

Who can me tell, what composire array will be in my case for two row keys "name, company"? Thank you!

Upvotes: 1

Views: 930

Answers (1)

Aaron
Aaron

Reputation: 57843

PRIMARY KEY (company,name)

Compound primary keys in Cassandra have multiple parts. The first column (company in your case) is your partitioning key. This is the key that will help determine which partition the row is stored on.

The next part of a compound primary key, are the clustering columns. The clustering columns indicate the on-disk sort order of your data. In your case, you only have one clustering column (name). Your row data will then be stored by a (assumed unique) combination of company and name, and your data will be sorted (on-disk) by name.

What is a row key in my case?

The row key in your case is this combination of both company and name.

Now how do you set that in your PHP (PHPCassa?) code? I'm not exactly sure. But from what I'm reading on this blog posting (warning: it is a couple of years old) titled Cassandra PHPCassa & Composite Types, you may need to define your rowkey as an array, like this:

$this->cassandra->set(
        array('employees.company','employees.name'),
        array(
            'age'     => 45,
            'role'    => 'ddd'
        )
    );

Try that and see if it helps. Otherwise, reference the article I linked above.

Based on your most-recent edit, I think your key definition will look something like this:

$key = array("[email protected]", "Alice Rozenberg");
$columns = array(45,"ddd");
$cf->insert($key, $columns);

Upvotes: 2

Related Questions