Tuesday, February 5, 2013

Write Data to Cassandra with UTF-8 (String) validators

Cluster cassandraCluster = HFactory.createCluster("TestCluster",
                new CassandraHostConfigurator("localhost:9161"), credentials);
        Keyspace keyspace = HFactory.createKeyspace(keyspaceName, cassandraCluster);
       
        BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition();
        columnFamilyDefinition.setKeyspaceName(keyspaceName);
        columnFamilyDefinition.setName(columnFamily);   
      columnFamilyDefinition.setComparatorType(ComparatorType.UTF8TYPE); 
 columnFamilyDefinition.setDefaultValidationClass(ComparatorType.UTF8TYPE.getClassName());
columnFamilyDefinition.setKeyValidationClass(ComparatorType.UTF8TYPE.getClassName());
       
        ColumnFamilyDefinition cfDef = new ThriftCfDef(columnFamilyDefinition);
       
        KeyspaceDefinition keyspaceDefinition =
            HFactory.createKeyspaceDefinition(keyspaceName, "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDef));
        cassandraCluster.addKeyspace(keyspaceDefinition);
       
       
        KeyspaceDefinition fromCluster = cassandraCluster.describeKeyspace(keyspaceName);
        cfDef = fromCluster.getCfDefs().get(0);
       
        columnFamilyDefinition = new BasicColumnFamilyDefinition(cfDef);
       
        BasicColumnDefinition columnDefinition = new BasicColumnDefinition();
       
        columnDefinition.setName(StringSerializer.get().toByteBuffer("lat"));
        columnDefinition.setIndexType(ColumnIndexType.KEYS);
        columnDefinition.setIndexName("lat");

columnDefinition.setValidationClass(ComparatorType.UTF8TYPE.getClassName());
        columnFamilyDefinition.addColumnDefinition(columnDefinition);
       
        columnDefinition = new BasicColumnDefinition();
        columnDefinition.setName(StringSerializer.get().toByteBuffer("lon"));   
        columnDefinition.setIndexType(ColumnIndexType.KEYS);
        columnDefinition.setIndexName("lon");

 columnDefinition.setValidationClass(ComparatorType.UTF8TYPE.getClassName());
        columnFamilyDefinition.addColumnDefinition(columnDefinition);
       
        columnDefinition = new BasicColumnDefinition();
        columnDefinition.setName(StringSerializer.get().toByteBuffer("timestamp"));   
  columnDefinition.setIndexType(ColumnIndexType.KEYS);
        columnDefinition.setIndexName("timestamp");
       columnDefinition.setValidationClass(ComparatorType.UTF8TYPE.getClassName());

        columnFamilyDefinition.addColumnDefinition(columnDefinition);
       
        cassandraCluster.updateColumnFamily(new ThriftCfDef(columnFamilyDefinition));
       
        for (int i = 90; i < 100; i++) {
            Mutator<String> mutator = HFactory.createMutator(keyspace, sser);
            String dID = Integer.toString(i);
            mutator.insert(dID, columnFamily, HFactory.createStringColumn("lat", lat));
            mutator.insert(dID, columnFamily, HFactory.createStringColumn("lon", lon));
            mutator.insert(dID, columnFamily,
                    HFactory.createStringColumn("timestamp", timestamp));
            mutator.execute();
        }



 You can check whether the created CFs are in proper and according to the required validation methods, by using a Cassandra-CLI tool. How to use this tool?
Refer my previous blog post       

No comments:

Post a Comment