A problem with insert data into cassandra - cassandra

enter image description herethis is the data structure of the table.
reviewer_id,reviewer_username,reviewer_personal_details,reviewer_preferences
1,RhMadsen,"{fname: 'Rhea', lanme: 'Madsen', age: 23}","{genre: {id: 14, name: 'Fantasy'}, production_company: {'name': 'Universal Pictures', 'id': 33}, production_country: {'iso_3166_1': 'US', 'name': 'United States of America'}}"
and my insert cmd is like this
INSERT INTO reviewers(
"reviewer_id",
"reviewer_username",
"reviewer_personal_details",
"reviewer_preferences"
)
VALUES (
'30',
'Reilio',
{fname:'Jack',lname:'O'Reilly',age:'19'},
{"genres":{"id":'28',"name":'Action'},"production_company":{"id":'3333',"name":'Kangaroo Pictures'},"production_country":{"iso_3166_1":'AU',"name":'Australia'}}
);
and the shell always say its should end with a ;, but i have a ;. always stard a neew line. have no idea what happened.

You have a multiple problems in your statement:
you have single quote character inside this string: lname:'O'Reilly' - this lead that it's thinking that line is continuing. You need to escape it correctly with either additional ' character: lname:'O''Reilly', or using $$ for string boundaries.
you're using incorrect quotes for strings in the map - you're using double instead of single
you have incorrect data types for some fields, like, reviewer_id, etc.
you don't need to put column names into the double quotes - this is required only when you want to have case-sensitive names
The working query is following:
INSERT INTO reviewers(
reviewer_id,
reviewer_username,
reviewer_personal_details,
reviewer_preferences
)
VALUES (
30,
'Reilio',
{fname:'Jack',lname:$$O'Reilly$$, age:19},
{'genres':{'id':'28','name':'Action'},
'production_company':{'id':'3333','name':'Kangaroo Pictures'},
'production_country':{'iso_3166_1':'AU','name':'Australia'}
}
);
I recommend to read Cassandra documentation or grab the free book about Cassandra & read at least first part of it

Related

How to resolve the problem with the SQL LIKE operator

There is a table 'Phones', which includes a column 'phone_no', declared as varchar(20). It can be NULL, too.
Some of the values stored in this column are:
'(310) 369-1000', '(415) 623-1000', '(310) 449-3000', '(323) 956-8398', and '(800) 864-8377'.
I would like to filter out all the records where the phone number ends with '0', so I use the expression phone_no LIKE '%0'. However, the resulting recordset is empty! The same happens when using any number (not just 0) at the end of the pattern. Why? Where is the problem?

ADLA Job: Write To Different Files Based On Line Content

I have a BUNCH of fixed width text files that contain multiple transaction types with only 3 that I care about (121,122,124).
Sample File:
D103421612100188300000300000000012N000002000001000032021420170012260214201700122600000000059500000300001025798
D103421612200188300000300000000011000000000010000012053700028200004017000000010240000010000011NNYNY000001000003N0000000000 00
D1034216124001883000003000000000110000000000300000100000000000CS00000100000001200000033NN0 00000001200
So What I need to do is read line by line from these files and look for the ones that have a 121, 122, or 124 at startIndex = 9 and length = 3.
Each line needs to be parsed based on a data dictionary I have and the output needs to be grouped by transaction type into three different files.
I have a process that works but it's very inefficient, basically reading each line 3 times. The code I have is something like this:
#121 = EXTRACT
col1 string,
col2 string,
col3 string //ect...
FROM inputFile
USING new MyCustomExtractor(
new SQL.MAP<string, string> {
{"col1","2"},
{"col2","6"},
{"col3","3"} //ect...
};
);
OUTPUT #121
TO 121.csv
USING Outputters.Csv();
And I have the same code for 122 and 124. My custom extractor takes the SQL MAP and returns the parsed line and skips all lines that don't contain the transaction type I'm looking for.
This approach also means I'm running through all the lines in a file 3 times. Obviously this isn't as efficient as it could be.
What I'm looking for is a high level concept of the most efficient way to read a line, determine if it is a transaction I care about, then output to the correct file.
Thanks in advance.
How about pulling out the transaction type early using the Substring method of the String datatype? Then you can do some work with it, filtering etc. A simple example:
// Test data
#input = SELECT *
FROM (
VALUES
( "D103421612100188300000300000000012N000002000001000032021420170012260214201700122600000000059500000300001025798" ),
( "D103421612200188300000300000000011000000000010000012053700028200004017000000010240000010000011NNYNY000001000003N0000000000 00" ),
( "D1034216124001883000003000000000110000000000300000100000000000CS00000100000001200000033NN0 00000001200" ),
( "D1034216999 0000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000" )
) AS x ( rawData );
// Pull out the transaction type
#working =
SELECT rawData.Substring(8,3) AS transactionType,
rawData
FROM #input;
// !!TODO do some other work here
#output =
SELECT *
FROM #working
WHERE transactionType IN ("121", "122", "124"); //NB Note the case-sensitive IN clause
OUTPUT #output TO "/output/output.csv"
USING Outputters.Csv();
As of today, there is no specific U-SQL function that can define the output location of a tuple on the fly.
wBob presented an approach to a potential workaround. I'd extend the solution the following way to address your need:
Read the entire file, adding a new column that helps you identify the transaction type.
Create 3 rowsets (one for each file) using a WHERE statement with the specific transaction type (121, 122, 124) on the column created in the previous step.
Output each rowset created in the previous step to their individual file.
If you have more feedback or needs, feel free to create an item (and voting for others) on our UserVoice site: https://feedback.azure.com/forums/327234-data-lake. Thanks!

Insert Cassandra Query Error

I am writing a simple insert query but getting error.
CREATE TABLE revenue_classification_region (
year int,
region text,
revenue_total int,
PRIMARY KEY (year,region));
INSERT INTO revenue_classification_region (year,region,revenue_total) VALUES (2015,’Western Europe’,5709);
Error:
Invalid syntax at line 1, char 84
INSERT INTO revenue_classification_region (year,region,revenue_total) VALUES (2015,’Western Europe’,5709);
Please help
The text value ('Western Europe') must be enclosed in normal simple quotes: '. Most probably this insert was copied from a web page in which the simple quote was replaced by one of the alternative quote characters.

With VI search and replace how do I replace the text within the last two sets of single quotes?

I have a lot of lines like this:
{ id: 22, name: 'Alice', score: 123, city: 'Atlanta', birthday: '1981/12/03'},
I want to use a VI (gvim to be exact) search and replace and empty the last two sets of single quotes.
you can use :normal cmd, sometimes it comes easier than the :s. for your example:
:norm! $F'di'3;.
will change your line into:
{ id: 22, name: 'Alice', score: 123, city: '', birthday: ''},
If you want to do this on all lines, you just :%norm! ..... You can combine the norm cmd with :g too, e.g. you want to do this transform on all lines with name:
:g/name:/norm! ...
Write a macro to achieve that. To start recording a macro, hit q followed by a register where you want to save it (for example under a register). Do it in command mode.
$F'di';;di'j
$ - go to the end of line
F' - search backwards for '
di' - delete what is between single quotes
;; - go to the previous single quotes
di' - delete again
j - go to line below
Finish a macro by hitting again q. Then, to apply a macro on a single line hit #a. To apply a macro on all lines type :%norm! #a
If you want to use search and replace, you can do this:
:%s/city: '.\{-}'/city: ''
and the same for the birthday field
:%s/birthday: '.\{-}'/birthday: ''
the "\{-}" is the same as "*" but uses de shortest match first algorithm. (see :help non-greedy)
if you really like do this in a single command:
%s/\(city:\|birthday:\) '.\{-}'/\1 ''/g
"city:" and "birthday:" are matched as a sub-expression with the \( and \), then you can use the \1 (means the first sub-expression) in the substitute string, the end g option is needed to make more than one substitution in a single line.

Pl/Sql using instr to find exact match

I am trying to find if a string exist in a word and extract it. I have uses the instr() function but this works as the LIKE function: if part or the whole word exists it returns it.
Here I want to get the string 'Services' out, it works but if I change 'Services' to 'Service' it still works. I don't want that. If 'Service' is entered it should return null and not 'Services'
Modified:
What I am trying to do here is abbreviate certain parts of the company name.
This is what my database table looks like :
Word | Abb
---------+-----
Company | com
Limited | ltd
Service | serv
Services | servs
Here is the code:
Declare
Cursor Words Is
SELECT word,abb
FROM abbWords
processingWord VARCHAR2(50);
abbreviatedName VARCHAR(120);
fullName = 'A.D Company Services Limited';
BEGIN
FOR eachWord IN Words LOOP
--find the position of the word in name
wordPosition := INSTR(fullName, eachWord.word);
--extracts the word form the full name that matches the database
processingWord := Substr(fullName,instr(fullName,eachWord.word), length(eachWord.word));
--only process words that exist in name
if wordPosition > 0 then
abbreviatedName = replace(fullName, eachWord.word,eachWord.abb);
end if;
END lOOP;
END;
So if the user enters 'Service' I don't want 'Services' to be returned. By this I mean word position should be 0 if the word 'Service' in not found instead of returning the position for the word 'Services'
One way of doing it:
DECODE(INSTR('A.D Company Seervices Limited','Services'),
0,
NULL,
SUBSTR('A.D Company Services Limited',
INSTR('A.D Company Services Limited','Services'),
length('Services')))
INSTR() will return 0 if text is not found. DECODE() will evaluate the first argument, compare to the second, if match, return third argument, if not, return fourth argument. (sqlfiddle link)
Arguably not the most elegant way, but matches your requirement.
I think you're over-complicating this. You can do everything with regular expressions. For instance; given the following table:
create table names ( name varchar2(100));
insert into names values ('A.D Company Services Limited');
insert into names values ('A.D Company Service Limited');
This query will only return the name 'A.D Company Services Limited'.
select *
from names
where regexp_like( name
, '(^|[[:space:]])services($|[[:space:]])'
, 'i' )
This means match the beginning of the string, ^, or a space followed by services followed the end of the string, $, or a space. This is what differentiates regular expressions from using instr etc. You can make your matches easily conditional on other factors.
However, though this seems to be your question I don't think this is what you're trying to do. You're trying to replace the string 'serv' in your wider string without replacing 'services' or 'service'. For this you need to use regexp_replace().
If I add the following row to the table:
insert into names values ('A.D Company Serv Limited');
and run this query:
select regexp_replace( name
, '(^|[[:space:]])serv($|[[:space:]])'
, ' Services '
, 1, 0, 'i' )
from names
The only thing that will change is ' Serv ', which in this newest line, will be replaced with ' Services '. Note the spaces; as you don't want to replace 'Services' with 'ServServices' these are very important.
Here's a little SQL Fiddle to demonstrate.
Another alternative is to use something like:
select replace(name,' serv ', ' Services ')
from names;
This will replace only the word 'Serv' situated between 2 spaces.
Thank you,
Alex.
INSTR returns a number: the index of the first occurrence of the matching string. You should use regexp_substr instead (10g+):
SQL> select regexp_substr('A.D Company Services Limited', 'Services') match,
2 regexp_substr('A.D Company Service Limited', 'Services') unmatch
3 from dual;
MATCH UNMATCH
-------- -------
Services

Resources