I created a table, university_by_person, as the following:
CREATE TABLE university_by_person (
person_name frozen<name>,
university_id uuid,
university_name text,
university_phone text,
university_address frozen<address>,
PRIMARY KEY ((person_name), university_id)
) WITH CLUSTERING ORDER BY (university_id ASC)
AND comment = 'Q1. Find universities(including university info) near a person';
And I also self-defined the TYPE name and address as the following:
CREATE TYPE name (
first_name text,
last_name text
)
CREATE TYPE address (
street text,
city text,
state_or_province text,
postal_code text,
country text
)
However, now when I tried to insert a record into this table, and hit ENTER, the cmd prompt only returned '...'. It seems the cmd thought my command was not finished? But I did have a ';' at the end. How could I fix it here?
My insert command:
INSERT INTO university_by_person
(person_name, university_id, university_name, university_phone, university_address)
VALUES (
{first_name: 'Meryl', last_name: 'Streep'},
e7ae5cf3-d358-4d99-b900-85902fda9bb0,
'University of Toronto',
'416-963-0086',
{street: '27 King's College Cir', city: 'Toronto', state_or_province: 'Ontario', postal_code: 'M5S 1A1', country: 'Canada'}
);
The issue is that the value for street in the address is a string which contains a single quote (') character:
street: '27 King's College Cir'
The ... line in cqlsh indicates that it is expecting more input since the string got "terminated" prematurely.
To use an apostrophe within a string, you need to escape with another single quote so it looks like:
street: '27 King''s College Cir'
This is the full corrected CQL statement:
INSERT INTO university_by_person
(person_name, university_id, university_name, university_phone, university_address)
VALUES (
{first_name: 'Meryl', last_name: 'Streep'},
e7ae5cf3-d358-4d99-b900-85902fda9bb0,
'University of Toronto',
'416-963-0086',
{street: '27 King''s College Cir', city: 'Toronto', state_or_province: 'Ontario', postal_code: 'M5S 1A1', country: 'Canada'}
);
should execute as expected. The value of the column should also include the apostrophe:
cqlsh:stackoverflow> SELECT university_address FROM university_by_person
WHERE person_name = {first_name: 'Meryl', last_name: 'Streep'};
university_address
------------------------------------------------------------------------------------------------------------------------------
{street: '27 King''s College Cir', city: 'Toronto', state_or_province: 'Ontario', postal_code: 'M5S 1A1', country: 'Canada'}
For more info and alernatives, see Escaping characters. Cheers!
Related
When tried to insert this data i am getting the following error. Not sure what i am missing but looks silly...
Table Structure
CREATE TABLE PackageItems (
ItemID INT64 NOT NULL,
LocationID INT64 NOT NULL,
PackageID STRING(MAX) NOT NULL,
Price FLOAT64 NOT NULL,
OrganizationID INT64,
CreatedBy STRING(MAX),
CreatedDateTime TIMESTAMP OPTIONS (allow_commit_timestamp=true),
LastModifiedBy STRING(MAX),
LastModifiedDateTime TIMESTAMP OPTIONS (allow_commit_timestamp=true),
) PRIMARY KEY (OrganizationID, LocationID, PackageID, ItemID);
Insert Command
INSERT INTO PackageItems (ItemID, LocationID, PackageID, Price, OrganizationID)
(5,1,"1",12.50,1635931867921)
Error
Syntax error: Unexpected integer literal "5" at line 6, column 2
It seems that you are missing the VALUES keyword in your insert string:
So instead of:
INSERT INTO PackageItems (ItemID, LocationID, PackageID, Price, OrganizationID)
(5,1,"1",12.50,1635931867921)
Try:
INSERT INTO PackageItems (ItemID, LocationID, PackageID, Price, OrganizationID)
VALUES (5,1,'1',12.50,1635931867921)
(Also note the change from "1" to '1' for the string literal.)
I am working with Scylla (Cassandra) db and trying to create tables which is dealing with User-Defined Types as shown below:
CREATE TYPE process (
id int,
discount float
);
CREATE TYPE service (
id int,
url text
);
CREATE TABLE data (
id int PRIMARY KEY,
fname text,
lname text,
service set<frozen<service>>,
monthly_process frozen<process>
);
My confusion is how can I insert data in my data table. Problem is I am confuse how process and service type works here and how can I insert values in them?
I tried with below example but it gave me an error:
insert into test (id, fname, lname, service, monthly_process )
values (1, 'abc', 'world', {'service': [{'id':1, 'url': 'some_url1'},
{'id':2, 'url': 'some_url2'}]}, {'id':1, 'discount': 10.0});
Error I got:
InvalidRequest: Error from server: code=2200 [Invalid query]
message="Invalid map literal for service of type set<frozen<service>>"
Here is the working version of your query;
insert into data (id, fname, lname, service, monthly_process)
values (2, 'abc', 'world', {{id: 1, url: 'some'}, {id : 2, url:'another'}}, {id:1, discount: 10.0});
service set<frozen<service>> format would be {{id:1, url:'a'}, {id:2, url:'b'}}
monthly_process frozen<process> format is {id:1, discount: 10.0}
create table seller(
seller_id int primary key,
seller_name text,
seller_email set<text>,
seller_address map<text>,
seller_phone list<text>,
product_id int,
product_title_text,
product_description text,
product_trackno int,
product_bidoption text,
bid_startdate date,
bid_closedate date,
bid_startprice int,
bid_withdrawdate date);
SyntaxException: line 1:110 mismatched input '>' expecting ',' (...<text>,
seller_address map<text[>],...)
What changes should be made in order to execute?
Of course you can, with some adjustments:
1) It helps if the type of a column isn't linked to the column name by an underscore. Instead of:
product_title_text,
This will work:
product_title text,
2) You'll also need to provide both types for the map collection. Instead of:
seller_address map<TEXT>,
This will work:
seller_address map<TEXT,TEXT>,
Full CQL:
create table seller(
seller_id int primary key,
seller_name text,
seller_email set<TEXT>,
seller_address map<TEXT,TEXT>,
seller_phone list<TEXT>,
product_id int,
product_title text,
product_description text,
product_trackno int,
product_bidoption text,
bid_startdate date,
bid_closedate date,
bid_startprice int,
bid_withdrawdate date);
Also, are you really only ever going to query this table by seller_id? If not, you may want to rethink the primary key definition.
I have created a cassandra table using few primary datatypes and a frozen type address_type.
CREATE TYPE address_type (
first_name text,
last_name text,
address_line1 text,
address_line2 text
);
CREATE TABLE user (
id text,
active_profile boolean,
addresses frozen<address_type>,
PRIMARY KEY (id)
);
And indexed the columns addresses because I want to select few resultset based on address_type.first_name.
CREATE INDEX ON user (addresses) ;
Finally this is my query which returns 0 rows.
select * from user where addresses = {first_name:'test2'};
When I tried
select * from "user" where addresses > {first_name:'test2'};
Which resulted in
code=2200 [Invalid query] message="No secondary indexes on the restricted columns support the provided operators: 'addresses > <value>'"
Can someone help me? Where I am going wrong here?
Let's insert some data :
cqlsh:test> INSERT INTO user (id , addresses) VALUES ('user_0', {first_name:'Ashraful', last_name:'Islam'});
cqlsh:test> INSERT INTO user (id , addresses) VALUES ('user_1', {first_name:'Ashraful'});
cqlsh:test> SELECT * FROM user ;
id | active_profile | addresses
--------+----------------+----------------------------------------------------------------------------------------
user_1 | null | {first_name: 'Ashraful', last_name: null, address_line1: null, address_line2: null}
user_0 | null | {first_name: 'Ashraful', last_name: 'Islam', address_line1: null, address_line2: null}
Since addresses is frozen type. you can't query with a piece of a frozen field. You have to provide full value of addresses
Example :
cqlsh:test> SELECT * FROM user WHERE addresses = {first_name:'Ashraful', last_name:'Islam'} ;
id | active_profile | addresses
--------+----------------+----------------------------------------------------------------------------------------
user_0 | null | {first_name: 'Ashraful', last_name: 'Islam', address_line1: null, address_line2: null}
(1 rows)
cqlsh:test> SELECT * FROM user WHERE addresses = {first_name: 'Ashraful'} ;
id | active_profile | addresses
--------+----------------+-------------------------------------------------------------------------------------
user_1 | null | {first_name: 'Ashraful', last_name: null, address_line1: null, address_line2: null}
(1 rows)
I am using cloudera vm. I have imported the products table from retail_db as a textfile with '|' as a fields separator (using sqoop).
Following is the table schema:
mysql> describe products;
product_id: int(11)
product_category_id: int(11)
product_name: varchar(45)
product_description: varchar(255)
product_price: float
product_image: varchar(255)
I want to create a Dataframe from this data.
I got no issue while using following code:
var products = sc.textFile("/user/cloudera/ex/products").map(r => {var p = r.split('|'); (p(0).toInt, p(1).toInt, p(2), p(3), p(4).toFloat, p(5))})
case class Products(productID: Int, productCategory: Int, productName: String, productDescription: String, productPrice: Float, productImage: String)
var productsDF = products.map(r => Products(r._1, r._2, r._3, r._4, r._5, r._6)).toDF()
productsDF.show()
But I got NumberFormatException exception for following code:
case class Products (product_id: Int, product_category_id: Int, product_name: String, product_description: String, product_price: Float, product_image: String)
val productsDF = sc.textFile("/user/cloudera/ex/products").map(_.split("|")).map(p => Products(p(0).trim.toInt, p(1).trim.toInt, p(2), p(3), p(4).trim.toFloat, p(5))).toDF()
productsDF.show()
java.lang.NumberFormatException: For input string: ""
Why is that I am getting exception in 2nd code even though it is same as that of 1st one?
The error is due to _.split("|") in second part of your code
You need to use _.split('|') or _.split("\\|") or _.split("""\|""") or Pattern.quote("|")
If you use "|" it tries to split with regular expression and | is or in the regular expression, so it does not matches anything and returns empty string ""
Hope this helps!