Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Php e Cassandra

Php e Cassandra

O que é preciso para instalar e utilizar Cassandra (NoSQL) com PHP de uma forma simples e direta

Ivan Rosolen

December 01, 2013
Tweet

More Decks by Ivan Rosolen

Other Decks in Technology

Transcript

  1. Ivan Rosolen Graduado em Sistemas de Informação Pós-graduado em Gerência

    de Projetos Desenvolvedor a 10+ anos Autor de vários PHPT (testes para o PHP) Gerente de Projetos na Arizona
  2. - Alta escalabilidade e disponibilidade - Sem um único ponto

    de falha - Baseado em colunas (wide-column) - Rendimento de escrita muito alto - Rendimento de leitura bom - Consistência ajustável e suporte a replicação
  3. - Modelar os dados do jeito que você vai buscar,

    desnormalizado [KeySpace][ColumnFamily][Key][SuperColumn][SubColumn]
  4. Keyspace 1 Column Family 1 RowKey 1 RowKey 2 Name

    Value Timestamp Name Value Timestamp Name Value Timestamp Name Value Timestamp Name Value Timestamp Name Value Timestamp
  5. - Algum outro NoSQL não aguentou (MongoDB) - Precisa de

    um modelo de dados mais rebuscado? - Precisa de uma escrita ABSURDA? - Está disposto a reformular sua aplicação? As vezes muito trabalhoso dependendo da arquitetura - Está disposto a gastar $$?
  6. PHP

  7. - Não recomendado usar o java 7 ou o OpenJDK

    - Oracle Java SE Runtime Environment (JRE) 6 - Apache disponibiliza “binary tarballs” e “Debian packages” - Java Native Access (JNA) é necessário para servidores de produção pois melhora a utilização de memória do Cassandra
  8. cassandra-cli create keyspace Palestra with placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy' and strategy_options

    = {replication_factor:1}; use Palestra; create column family Users; cqlsh cqlsh:demo> select * from “Users”;
  9. - Habilitar APC para melhor performace - Dependências / Install

    http://thrift.apache.org/docs/install/ - Módulo nativo do PHP (phpize) cd ext/thrift_protocol phpize ./configure make sudo make install
  10. <?php require_once(__DIR__.'/../lib/autoload.php'); use phpcassa\Connection\ConnectionPool; use phpcassa\ColumnFamily; use phpcassa\SystemManager; use phpcassa\Schema\StrategyClass;

    $sys = new SystemManager('127.0.0.1'); $sys->create_keyspace('Palestra', array( "strategy_class" => StrategyClass::SIMPLE_STRATEGY, "strategy_options" => array('replication_factor' => '1'))); $sys->create_column_family('Palestra', 'Users'); $sys->close();
  11. <?php $GLOBALS['THRIFT_ROOT'] = '/usr/share/php/Thrift'; require_once $GLOBALS['THRIFT_ROOT'].'/packages/cassandra/Cassandra.php'; require_once $GLOBALS['THRIFT_ROOT'].'/packages/cassandra/cassandra_types.php'; require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';

    require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php'; require_once $GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php'; require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
  12. try { $socket = new TSocket('127.0.0.1', 9160); $transport = new

    TBufferedTransport($socket, 1024, 1024); $protocol = new TBinaryProtocolAccelerated($transport); $client = new CassandraClient($protocol); $transport->open(); $keyspace = 'Palestra'; $keyUserId = "user0"; $columnPath = new cassandra_ColumnPath(); $columnPath->column_family = 'Users'; $columnPath->super_column = null; $columnPath->column = 'email'; $timestamp = time(); $consistency_level = cassandra_ConsistencyLevel::ZERO; $value = '[email protected]'; $client->insert($keyspace, $keyUserId, $columnPath, $value, $timestamp, $consistency_level); $transport->close(); } catch (TException $tx) { print 'TException: '.$tx->why. ' Error: '.$tx->getMessage() . "\n"; }
  13. <?php require_once(__DIR__.'/../lib/autoload.php'); use phpcassa\Connection\ConnectionPool; use phpcassa\ColumnFamily; $pool = new ConnectionPool('Palestra',

    array('127.0.0.1')); $users = new ColumnFamily($pool, 'Users'); $users->insert('user0', array("email" => "[email protected]")); $pool->close();
  14. $sys = new SystemManager('127.0.0.1'); $pool = new ConnectionPool('Palestra', array('127.0.0.1')); $users

    = new ColumnFamily($pool, 'Users'); $users->truncate(); $sys->drop_keyspace("Palestra"); $pool->close(); $sys->close();
  15. $users->insert('user0', array("name" => "ivan", "state" => "SP")); $users->insert('user1', array("name" =>

    "elton", "state" => "SC")); $users->batch_insert(array("user3" => array("name" => "er"), vvvvvvvvvvv"user4" => array("name" => "calvin")));
  16. $user = $users->get('user0'); $name = $user["name"]; echo "Nome do usuário

    $name\n"; $columnNames = array('name', 'state'); $user = $users->get('user0',$column_slice = null, $column_names=$columnNames);
  17. $many = $users->multiget(array('user0', 'user1')); foreach($many as $user_id => $columns) {

    echo "Estado do $user_id: ".$columns["state"]."\n"; } $users->get_count('user0'); // retorna 2
  18. <?php require_once(__DIR__.'/../lib/autoload.php'); use phpcassa\Connection\ConnectionPool; use phpcassa\ColumnFamily; use phpcassa\ColumnSlice; $pool =

    new ConnectionPool('Palestra', array('127.0.0.1')); $letters = new ColumnFamily($pool, 'Letras'); $columns = array(1 => "a", 2 => "b", 3 => "c", 4 => "d", 5 => "e"); $letters->insert('linha', $columns); ……. $pool->close();
  19. // Where >= 2 $slice = new ColumnSlice(2); print_slice($letters->get('linha', $slice));

    // Between 2 and 4 $slice = new ColumnSlice(2, 4); print_slice($letters->get('linha', $slice)); // Limit 3 $slice = new ColumnSlice('', '', $count=3); print_slice($letters->get('linha', $slice)); // 3 últimas $slice = new ColumnSlice('', '', $count=3, $reversed=true); print_slice($letters->get('linha', $slice)); // 2 últimas antes do 4 $slice = new ColumnSlice(4, '', $count=2, $reversed=true); print_slice($letters->get('linha', $slice));
  20. <?php require_once(__DIR__.'/../lib/autoload.php'); use phpcassa\Connection\ConnectionPool; use phpcassa\ColumnFamily; $pool = new ConnectionPool('Palestra',

    array('127.0.0.1')); $users = new ColumnFamily($pool, 'Users'); $users->remove("user4"); $users>remove("user0", array("state")); $users->truncate(); $pool->close();
  21. Planet Cassandra http://www.planetcassandra.org/ Cassandra Summit 2013 http://www.youtube.com/watch?v=Qz6ElTdYjjU&list=PLqcm6qE9lgKJzVvwHprow9h7KMpb5hcUU Using Cassandra with

    PHP https://pressflow.atlassian.net/wiki/display/PF/Using+Cassandra+with+PHP DataStax http://www.datastax.com/docs DataStax Data modeling http://www.datastax.com/documentation/cql/3.0/webhelp/index.html#cql/ddl/ddl_anatomy_table_c.html Apache Cassandra Wiki http://wiki.apache.org/cassandra/ArticlesAndPresentations Quadrinho de como o Cassandra Funciona http://blogs.atlassian.com/2013/09/do-you-know-cassandra Pandra https://github.com/mjpearson/Pandra Cassandra PHP Client Library https://github.com/kallaspriit/Cassandra-PHP-Client-Library/ Cassandra PDO https://github.com/Orange-OpenSource/YACassandraPDO