Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added index creation method

  • Loading branch information...
commit 5a52031a0f942c83e2b0dbdf592a510afd8c1cd1 1 parent 5097879
@fmgoncalves authored
View
23 examples/cassandra_simple_test.pm
@@ -5,13 +5,6 @@ use Data::Dumper;
use Cassandra::Simple;
-#### RUN THESE COMMANDS ON CASSANDRA-CLI FIRST ####
-#
-#create keyspace simple;
-#use simple;
-#create column family simple with comparator= UTF8Type and column_metadata = [ {column_name: age, validation_class: UTF8Type, index_type: KEYS} ];
-#
-
sub println {
print @_, "\n";
}
@@ -19,15 +12,15 @@ sub println {
my ($keyspace, $column_family) = qw/simple simple/;
my $conn = Cassandra::Simple->new(
- server_name => '127.0.0.1', # optional, default to '127.0.0.1'
- server_port => 9160, # optional, default to 9160
- username => '', # optional, default to empty string ''
- password => '', # optional, default to empty string ''
- consistency_level_read => 'QUORUM', # optional, default to 'ONE'
- consistency_level_write => 'ONE', # optional, default to 'ONE'
keyspace => $keyspace,
);
+my $present = grep { $_ eq $column_family } @{[ $conn->list_keyspace_cfs($keyspace) ]};
+
+unless ( $present ){
+ println "Creating $column_family in $keyspace";
+ $conn->create_column_family($keyspace, $column_family);
+}
#Method to test code here success
#get 100% 100%
@@ -66,6 +59,10 @@ println "\$conn->get_range($column_family, { start=> 'ChaveA', finish => 'ChaveB
println Dumper $conn->get_range($column_family, { start=> 'ChaveA', finish => 'ChaveB', column_count => 1 });
#Expected result: Depends on key order inside Cassandra. Probably only these 2 keys are returned with 1 column each.
+
+println "\$conn->create_index($keyspace, $column_family, 'age')";
+println Dumper $conn->create_index($keyspace, $column_family, 'age');
+
println "\$conn->batch_insert($column_family, { 'whisky1' => { 'age' => 12 }, 'whisky2' => { 'age' => 12 }, 'whisky3' => { 'age' => 15 }, 'whisky4' => { 'age' => 12 } })";
println Dumper $conn->batch_insert($column_family, { 'whisky1' => { 'age' => 12 }, 'whisky2' => { 'age' => 12 }, 'whisky3' => { 'age' => 15 }, 'whisky4' => { 'age' => 12 } });
View
2  examples/super_column_support.pm
@@ -35,7 +35,7 @@ println "\$conn->insert($column_family, 'KeyA', { 'SuperColumnA' => { 'SubColumn
$conn->insert_super($column_family, 'KeyA', {'SuperColumnA' => {'SubColumnA' => 'AAA', 'SubColumnB' => 'AAB'}, 'SuperColumnB' => {'SubColumnA' => 'ABA', 'SubColumnB' => 'ABB'}, 'SuperColumnC' => {'SubColumnA' => 'ACA', 'SubColumnB' => 'ACB'} });
println "\$conn->get($column_family, 'KeyA')";
-println Dumper { $conn->get($column_family, 'KeyA') };
+println Dumper $conn->get($column_family, 'KeyA');
#Expected result: both supercolumns with all four subcolumns
println "\$conn->get($column_family, 'KeyA', { columns => ['SuperColumnB'] } )";
View
50 lib/Cassandra/Simple.pm
@@ -276,8 +276,8 @@ sub multiget {
$self->client->multiget_slice( $keys, $columnParent, $predicate, $level );
my %result_columns = map {
- $_ => {map { $_->{column}->{name} => $_->{column}->{value} }
- @{ $result->{$_} }}
+ $_ => { map { $_->{column}->{name} => $_->{column}->{value} }
+ @{ $result->{$_} } }
} keys %$result;
return \%result_columns;
@@ -704,7 +704,7 @@ sub batch_insert {
)
}
);
- } keys % $columns
+ } keys %$columns
]
}
} keys %$rows;
@@ -821,11 +821,51 @@ sub create_column_family {
$cfdef->{comment} = $comment;
$cfdef->{column_type} = $is_super ? 'Super' : 'Standard';
- print Dumper $cfdef;
-
$self->client->system_add_column_family($cfdef);
}
+
+=head2 create_index
+
+Usage: C<< create_index($keyspace, $column_family, $column, [$validation_class]) >>
+
+Creates an index on C<$column> of C<$column_family>.
+
+$validation_class only applies when $column doesn't yet exist, and even then it is optional (defaults to BytesType).
+
+=cut
+sub create_index{
+ my $self = shift;
+
+ my $keyspace = shift;
+ my $column_family = shift;
+ my $column = shift;
+
+ #TODO: get column family definition, substitute the target column with itself but indexed.
+
+ my $cfdef = [grep {$_->{name} eq $column_family } @{ $self->client->describe_keyspace($keyspace)->{cf_defs} }]->[0];
+
+ my $cdef;
+ if(@{ $cfdef->{column_metadata} } and grep {$_->{name} eq $column } @{ $cfdef->{column_metadata} }){
+ $cdef = [grep {$_->{name} eq $column } @{ $cfdef->{column_metadata} }]->[0];
+ }else{
+ $cdef = new Cassandra::ColumnDef({
+ name => $column,
+ validation_class => 'org.apache.cassandra.db.marshal.BytesType',
+ });
+ }
+ $cdef->{index_type} = 0;
+ $cdef->{index_name} = $column . "_idx";
+
+
+ $cfdef->{column_metadata} = [grep {$_->{name} ne $column } @{ $cfdef->{column_metadata} }];
+ push @{ $cfdef->{column_metadata} }, $cdef;
+
+ print Dumper $cfdef;
+
+ $self->client->system_update_column_family($cfdef);
+}
+
=head1 BUGS
Bugs should be reported on github at L<https://github.com/fmgoncalves/p5-cassandra-simple>.
Please sign in to comment.
Something went wrong with that request. Please try again.