Connessione a Cassandra in PHP

Mattepuffo's logo
Connessione a Cassandra in PHP

Connessione a Cassandra in PHP

In questo articolo vediamo come connetterci ad un db Cassandra in PHP.

Abbiamo due tabelle identiche:

  • in una salviamo tutti gli utenti (utenti_tutti)
  • nell'altra solo i maggiorenni (utenti_maggiorenni)

Prima di tutto dobbiamo installare l'estensione con pecl:

pecl install cassandra

Qui sotto un pò di codice; ho messo tutto insieme per comodità:

<?php

class Utente {
    public $id;
    public $email;
    public $eta;
    public $nomeCompleto;

    public function __construct($email, $eta, $nomeCompleto) {
        $this->id = new Cassandra\Uuid();
        $this->email = $email;
        $this->eta = $eta;
        $this->nomeCompleto = $nomeCompleto;
    }
}

try {
    $cluster = Cassandra::cluster()
        ->withContactPoints(['172.17.0.4']) // Il tuo contact point
        ->withDefaultConsistency(Cassandra::CONSISTENCY_QUORUM)
        ->build();

    $keyspace = 'test_keyspace';
    $session = $cluster->connect($keyspace);

    echo "Connesso a Cassandra!<br><br>";

    $insertTuttiCql = "INSERT INTO utenti_tutti (id, email, eta, nome_completo) VALUES (?, ?, ?, ?)";
    $insertMaggiorenniCql = "INSERT INTO utenti_maggiorenni (id, email, eta, nome_completo) VALUES (?, ?, ?, ?)";

    $insertTuttiStmt = $session->prepare($insertTuttiCql);
    $insertMaggiorenniStmt = $session->prepare($insertMaggiorenniCql);

    $utenti = [
        new Utente("mario.rossi@email.com", 25, "Mario Rossi"),
        new Utente("giulia.verdi@email.com", 30, "Giulia Verdi"),
        new Utente("luca.bianchi@email.com", 16, "Luca Bianchi")
    ];

    echo "=== INSERIMENTO UTENTI ===<br>";

    foreach ($utenti as $utente) {
        $params = [
            $utente->id,
            $utente->email,
            $utente->eta,
            $utente->nomeCompleto
        ];

        $session->execute($insertTuttiStmt, $params);
        echo "Inserito in utenti_tutti: {$utente->nomeCompleto} (età {$utente->eta})<br>";

        if ($utente->eta >= 18) {
            $session->execute($insertMaggiorenniStmt, $params);
            echo "Inserito in utenti_maggiorenni: {$utente->nomeCompleto}<br>";
        } else {
            echo "NON inserito in utenti_maggiorenni: {$utente->nomeCompleto} (minorenne)<br>";
        }
        echo "<br>";
    }

    echo "=== QUERY TABELLA: utenti_tutti ===<br>";
    $countTutti = queryAndPrintResults($session, "SELECT * FROM utenti_tutti");
    echo "Totale utenti_tutti: {$countTutti}<br><br>";

    echo "=== QUERY TABELLA: utenti_maggiorenni ===<br>";
    $countMaggiorenni = queryAndPrintResults($session, "SELECT * FROM utenti_maggiorenni");
    echo "Totale utenti_maggiorenni: {$countMaggiorenni}<br><br>";
} catch (Cassandra\Exception $e) {
    echo "Si è verificato un errore di Cassandra: " . $e->getMessage() . "<br>";

} catch (Exception $e) {
    echo "Si è verificato un errore generico: " . $e->getMessage() . "<br>";
}

function queryAndPrintResults($session, $cql) {
    $results = $session->execute($cql);

    $count = 0;
    foreach ($results as $row) {
        echo "ID: " . $row['id'] . "<br>";
        echo "Nome: " . $row['nome_completo'] . "<br>";
        echo "Email: " . $row['email'] . "<br>";
        echo "Età: " . $row['eta'] . "<br>";
        echo "----------<br>";
        $count++;
    }
    return $count;
}

echo "Operazioni completate. Uscita...<br>";

Enjoy!


Condividi

Commentami!