use parameter as date in execute script - sql

I am working on execute Script processor in nifi. I declared lastdate
as 2020-12-21 and I am trying to use this attribute in execute script
(groovy) file to fetch data from oracle.
In oracle it gave me the correct result. In nifi go to failure.
My code (script body):
def last_data = flowFile.getAttribute('last_date')
query = "select t.* from mytable where r.mydate > " + last_date + ",'yyyy-mm-dd')

Hehe, your variable is last_datA and you concatene last_datE.
I thinks that it buddy :)

Related

Impala due DBeaver - how to declare a variable in which the request is sewn

set var:dt= select......from table;
select * from
where date_field = '${var:dt}
That stuff doesn't work. Reminder - this is not in command line, in DBeaver framework.
Solution of that problem.

Passing parameters in HIVE query

I need to pass a parameter into an HQL at execution time. For this we have updated the .param file with the required value. Now I am facing issue in using it.
SELECT * FROM temp_table A
WHERE PRODT_CTGRY_CD = 'CAR' AND
(
(DATE(A.CUST_BRTH_DT) BETWEEN concat(${hiveconf:PRODYEAR} - 13 ,"-02","-28") and concat(${hiveconf:PRODYEAR} - 7 ,"-03","-01")
AND DATE(A.TERM_DT) >= concat(${hiveconf:PRODYEAR} - 1 ,"-03","-31")
)
In the above query if I don't include the condition of term_dt the query is running fine. However when I include that I am getting an error as below:
FAILED: ClassCastException org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableIntObjectInspector cannot be cast to org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector (state=42000,code=40000)
Can someone let me know what needs to be done here?

How to store the output of a query in a variable in HIVE

I want to store current_day - 1 in a variable in Hive. I know there are already previous threads on this topic but the solutions provided there first recommends defining the variable outside hive in a shell environment and then using that variable inside Hive.
Storing result of query in hive variable
I first got the current_Date - 1 using
select date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'),1);
Then i tried two approaches:
1. set date1 = ( select date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'),1);
and
2. set hivevar:date1 = ( select date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'),1);
Both the approaches are throwing an error:
"ParseException line 1:82 cannot recognize input near 'select' 'date_sub' '(' in expression specification"
When I printed (1) in place of yesterday's date the select query is saved in the variable. The (2) approach throws "{hivevar:dt_chk} is undefined
".
I am new to Hive, would appreciate any help. Thanks.
Hive doesn't support a straightforward way to store query result to variables.You have to use the shell option along with hiveconf.
date1 = $(hive -e "set hive.cli.print.header=false; select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);")
hive -hiveconf "date1"="$date1" -f hive_script.hql
Then in your script you can reference the newly created varaible date1
select '${hiveconf:date1}'
After lots of research, this is probably the best way to achieve setting a variable as an output of an SQL:
INSERT OVERWRITE LOCAL DIRECTORY '<home path>/config/date1'
select CONCAT('set hivevar:date1=',date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'),1)) from <some table> limit 1;
source <home path>/config/date1/000000_0;
You will then be able to use ${date1} in your subsequent SQLs.
Here we had to use <some table> limit 1 as hive got a bug in insert overwrite if we don't specify a table name.

Hibernate createSQLQuery and Toad SQL query return different results - parameter problems?

I'm a newbie at Hibernate so excuse me if some of this is glaringly obvious but it's been a very, very long day. I am trying to create and execute a simple query in Hibernate and Toad/Oracle.
The Toad/Oracle sql reads:
select
count(*)
from
fpps_owner.fee_claim_payment_lines l,
fpps_owner.fee_claim_payments p
where
l.fee_claim_payment_id = p.fee_claim_payment_id and
p.claim_index = 87167895
The above returns 10 records, which is correct
The following Java code returns 0 records, which is NOT correct
String sLinesAvailable =
"select count(*) from " +
"fpps_owner.fee_claim_payment_lines l, fpps_owner.fee_claim_payments p " +
"where " +
"l.fee_claim_payment_id = p.fee_claim_payment_id and p.claim_index = :id";
Query qLinesAvailable = em.createNativeQuery(sLinesAvailable);
qLinesAvailable.setParameter("id", "87167895"); // fails
qLinesAvailable.setParameter("id", 87167895); // fails
List<Object> out = (List<Object>) qLinesAvailable.getResultList();
BigDecimal x = (BigDecimal) out.get(0);
Returns 0 records. Using .getSingleResult() also returns 0 records.
What am I missing here?
Any help would be GREATLY appreciated!
If you are not seeing any malformed query errors, it seems like the parameter is not binding correctly.
To debug, I'd print out that SQL statement the line after you set the parameter. This is the only way you can see the SQL after the parameter is set in order to compare it with Toad.
What does your binding file look like? Maybe you have a different name in there for the ID, so it's not able to find it based on the name. Trying binding with the parameter's order value, just as a test.
This might give some ideas: http://www.mkyong.com/hibernate/hibernate-parameter-binding-examples/
Best of luck! We've all been there :)
What happens when you try:
(Number) query.getSingleResult();
Your query isn't returning a list, but rather just a count.
Good luck.

Escaping in plsql

Hi I am tring to fire this query
update txnblackout
set enddate= ''TO_DATE('25/02/2012','dd/mm/yyyy')''
where idsequence='1'
But it is not working giving error
ORA-00933: SQL command not properly ended
Can you please justify it. Why it is happening?
This is the java code i am using to create query : -
Map<String, String> l_script_columns = new HashMap<String, String>();
l_script_columns.put("startdate", "'TO_DATE('"
+ txtStartDate.getText().trim() + "','"
+ FieldMapperHelper.DATE_Format + "')'");
please let me know if you want more clarification.Thanks.
why ever are you doing the escaping?
with txnblackout as
(select trunc(sysdate + level) enddate /* trunc drops time component */ , level idsequence
from dual
connect by level <= 10 /* this will generate 10 rows with an incrementing 'level' value */ )
select enddate
, idsequence
from txnblackout
where enddate = to_date('20/10/2012','dd/mm/yyyy')
;
thus you should be safe to drop the extra quotes, they are not necessary.
update txnblackout
set enddate= TO_DATE('25/02/2012','dd/mm/yyyy')
where idsequence=1;
(note this assumes idsequence is a number)
At the end of the day, if you are passing in a non-parameterized dynamic sql query into Oracle, you'll need it to look like this:
"update txnblackout
set enddate= TO_DATE(''25/02/2012'',''dd/mm/yyyy'')
where idsequence=1;"
(please note I am still assuming that idsequence is a NUMBER type and not a varchar)
Check this out though, try to be safe!
You also won't have to worry about escaping if you parameterize it!
Preventing SQL Injection in Java
Your query above you want to keep the date or the text, so in the first case
update txnblackout
set enddate= TO_DATE('25/02/2012','dd/mm/yyyy')
where idsequence='1'
The id sequence being text or number doesn't matter, in the second case you want to keep the text:
update txnblackout
set enddate= 'TO_DATE(''25/02/2012'',''dd/mm/yyyy'')'
where idsequence='1'
I think you want the first case please check.
Best regards
In this case you have to try this one:
Map<String, String> l_script_columns = new HashMap<String, String>();
l_script_columns.put("startdate", "'||TO_DATE('"
+ txtStartDate.getText().trim() + "','"
+ FieldMapperHelper.DATE_Format + "')||'");
then final result will be:
update txnblackout
set enddate= ''||TO_DATE('25/02/2012','dd/mm/yyyy')||''
where idsequence='1'
Try putting ';' at the end of your command...
You command should look like :
update txnblackout
set enddate= ''TO_DATE('25/02/2012','dd/mm/yyyy')''
where idsequence='1';