I have just started with Cassandra, created a simple ColumnFamily and some data using command given on Getting started on Cassandra download page, by executing below queries.
CREATE TABLE users (
user_id int PRIMARY KEY,
fname text,
lname text
);
INSERT INTO users (user_id, fname, lname)
VALUES (1745, 'john', 'smith');
INSERT INTO users (user_id, fname, lname)
VALUES (1744, 'john', 'doe');
INSERT INTO users (user_id, fname, lname)
VALUES (1746, 'john', 'smith');
My Hector code for fetching data:
RangeSlicesQuery<String, String, String> rangeSlicesQuery
= HFactory.createRangeSlicesQuery(keyspace, STR, STR, STR);
rangeSlicesQuery.setColumnFamily(columnFamily)
.setColumnNames(" fname ", " lname ")
.setKeys("6", "5")
.setRowCount(row_count);
QueryResult<OrderedRows<String, String, String>> result = rangeSlicesQuery.execute();
But when i fetch data using Hector API from Cassandra in Column name it contain blank space like column name fname will be: ' fname '.
And for my queries from Java Hector API i need to use same column name with blank space to fetch or save data.
Anybody have any idea, what i have done wrong, or what do i do to remove those trailing space.
Thanks
This issue has been fixed by using "WITH COMPACT STORAGE" when creating column families.
Discussion is here.
Instead of Hector i used Cassandra Java Driver pointed by Allan Elder, and my problem for trailing space in column name has been solved.
Doc for how to using Cassandra Java Driver is here and jars will be found by this project.
Thanks
Related
I have a case class which represents partition key values.
case class UserKeys (bucket:Int,
email: String)
I create query Clauses as follows:
def conditions(id: UserKeys):List[Clauses] = List(
QueryBuilder.eq("bucket", id.bucket), //TODOM - pick table description from config/env file.
QueryBuilder.eq("email", id.email)
)
And use the query as follows
val selectStmt =
select()
.from(tablename)
.where(QueryBuilder.eq(partitionKeyColumns(0), whereClauseList(0))).and(QueryBuilder.eq(partitionKeyColumns(1), whereClauseList(1)))
.limit(1)
I am getting following error.
com.datastax.driver.core.exceptions.InvalidTypeException: Value 0 of type class com.datastax.driver.core.querybuilder.Clause$SimpleClause does not correspond to any CQL3 type
Question 1 - What am I doing wrong?
The query works on cqlsh
The table I am querying is
CREATE TABLE users (
bucket int,
email text,
firstname text,
lastname text,
authprovider text,
password text,
PRIMARY KEY ((bucket, email), firstname, lastname)
Question 2 - Is there a way to print the List which contains the query clauses? I tried it but I get this incomprehensible text.
List(com.datastax.driver.core.querybuilder.Clause$SimpleClause#2389b3ee, com.datastax.driver.core.querybuilder.Clause$SimpleClause#927f81)
My bad, I was using the query clauses incorrectly. Rather than
.where(QueryBuilder.eq(partitionKeyColumns(0), whereClauseList(0))).and(QueryBuilder.eq(partitionKeyColumns(1), whereClauseList(1)))
I needed to do
.where(whereClauseList(0)).and(whereClauseList(1))
because the List already has QueryBuilder.eq("bucket", id.bucket) part
We're trying to setup an INSERT statement on a table with a set<text> column data type in Cassandra, but have come up with no luck. Here's an example:
INSERT INTO test_table
(my_items)
VALUES
([{"test1":"test val 1","test2":"test val 2"}])
The result is always something like:
no viable alternative at input for [test val ]2
Enclosing values in curly brackets and separate by comma
Example :
Let's we have the schema :
CREATE TABLE users (
user_id text PRIMARY KEY,
first_name text,
last_name text,
emails set<text>
);
Insert :
INSERT INTO users (user_id, first_name, last_name, emails)
VALUES('frodo', 'Frodo', 'Baggins', {'f#baggins.com', 'baggins#gmail.com'});
Update :
UPDATE users SET emails = emails + {'fb#friendsofmordor.org'}
WHERE user_id = 'frodo';
More Using the set type
Edited
If you want to insert value "test1":"test val 1" and "test2":"test val 2" into set then enclose each value with single quote
Example :
INSERT INTO users (user_id, first_name, last_name, emails)
VALUES('2011331035', 'Md Ashraful', 'Islam', {'"test1":"test val 1"', '"test2":"test val 2"'});
How to delete rows in cassandra based on an indexed column ?
Tried:
upload_id is added as an index in the table.
Delete from table where upload_id = '"+uploadId+"'"
But this gives me an Error "NON PRIMARY KEY found in where clause".
String selectQuery = "Select hashkey from table where upload_id='" + uploadId + "'"
entityManager.createNativeQuery(selectQuery).getResultList()
and delete all the elements in the List using a for loop.
This query is changed by kundera to append LIMIT 100 ALLOW Filtering.
Found a Question similar to this at Kundera for Cassandra - Deleting record by row key but that was asked in 2012 after that there were a lot of changes to cassandra and Kundera.
Kundera by default uses LIMIT 100. You can use query.setMaxResults(<integer>) to modify the limit accordingly and then run the loop.
Example:
Query findQuery = entityManager.createQuery("Select p from PersonCassandra p where p.age = 10", PersonCassandra.class);
findQuery.setMaxResults(5000);
List<PersonCassandra> allPersons = findQuery.getResultList();
INSERT INTO testtable (id, user_name, gender, rank) VALUES (now(), 'test_user', 'MALE', 2) ;
Bad Request: line 1:60 no viable alternative at input 'now'
Can someone help on the above error message when trying to insert a timeuuid into a table in cassandra.
You need to upgrade Cassandra - the timeuuid functions were added in Cassandra 1.2.2.
I am using Cassandra 1.2.5 (cqlsh 3.0.2) and trying to inserting data in a small test-database with german characters which is not possible. I get back the message from cqlsh: "Bad Request: Input length = 1"
below is the setup of the keyspace, the table and the insert.
CREATE KEYSPACE test WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
use test;
CREATE TABLE testdata (
id varchar,
text varchar,
PRIMARY KEY (id)
This is working:
insert into testdata (id, text) values ('4711', 'test');
This is not allowed:
insert into testdata (id, text) values ('4711', 'töst`);
->Bad Request: Input length = 1
my locale is :de_DE.UTF-8
Does Cassandra 1.2.5 has a problem with Umlaut ?
I just did what you posted and it worked for me. The one thing that was different however, is that instead of a single quote, you finished 'töst` with a backtick. That doesn't allow me to finish the statement in cqlsh. When I replace that with 'töst' it succeeds and I get:
cqlsh:test> select * from testdata;
id | text
------+------
4711 | töst