Cassandra is a NoSQL-based distributed database management system that can support processing large amounts of data. PHP, as a popular server-side programming language, can be used to operate Cassandra database. This article will introduce how to use PHP driver and CQL to connect and operate Cassandra database.
Before you begin, make sure you have installed the Cassandra database and PHP driver by following these steps:
1. Install Cassandra database
2. Install PHP
3. Install Cassandra For the PHP driver
installation steps, please search for relevant tutorials by yourself. The following are the basic steps for PHP to operate a Cassandra database:
withContactPoints('127.0.0.1') ->build(); $session = $cluster->connect();
In this example,127.0.0.1
represents the Cassandra node on the local host.$cluster->build()
will return a Cassandra cluster object.
session
object of Cassandra in PHP. The code is as follows:execute("CREATE KEYSPACE my_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};");
A new Keyspace namedmy_keyspace
is created here. The replication parameter specifies the data backup strategy.
execute("CREATE TABLE my_keyspace.my_table (id UUID primary key, name text);");
This code will create a new table named$my_table
. The table contains two columns,id
andname
, whereid
is the primary key column.
prepare("INSERT INTO my_keyspace.my_table (id, name) VALUES (?, ?)"); $session->execute($statement, array(new CassandraUuid(), "John Doe"));
In this example, we prepare a statement and then execute a statement calledJohn Doe
's name. Here, we reference PHP’sUuid()
object to generate a unique identifier.
$statement
variable we prepared previously to query the data in themy_table
table:prepare("SELECT * FROM my_keyspace.my_table"); $results = $session->execute($statement); foreach ($results as $row) { echo $row['id'] . " " . $row['name'] . " "; }
In this example, we can simply use aforeach()
loop to retrieve the data from the query and use string concatenation to output the data to the console.
prepare("UPDATE my_keyspace.my_table SET name = ? WHERE id = ?"); $session->execute($statement, array("Jane Doe", new CassandraUuid())); $statement = $session->prepare("DELETE FROM my_keyspace.my_table WHERE id = ?"); $session->execute($statement, array(new CassandraUuid()));
In this example, we use theUPDATE
keyword and key to update the name, and then use theDELETE
keyword and key to delete rows.
Summary
In this article, we have learned how to connect Cassandra database, create Keyspace and tables, insert, update, delete data and query data from PHP using PHP driver and CQL.
When developing applications, the combination of Cassandra database with PHP can make your applications faster, reliable, scalable, and use the latest NoSQL database technology. At the same time, using Cassandra's PHP driver makes it easier for you to integrate and manage Cassandra databases.
The above is the detailed content of Use PHP to operate Cassandra database. For more information, please follow other related articles on the PHP Chinese website!