Gerar o Cabeçalho Column_name para o OdiSqlUnload
Last week we have shown you the code for generating the Header and populate the same in OdiSqlUnload file , but the codes seems to be limited to the Database and then separate query needs to be created for each database reading from the System table,which doesn’t makes things so easy.
A fim de resolvê-lo e tornar os códigos independentes, revisitamos e reescreveremos todos os códigos para gerar cabeçalho.
Tudo o que você precisa é escrever a consulta que você usou no OdiSqlUnload .
import string
import java.sql as sql
import java.lang as lang
import re
sourceConnection = odiRef.getJDBCConnection("SRC")
output_write=open('c:/output.txt','r+')
myStmt = sourceConnection.createStatement()
my_query = "select * FROM ODI_WORK.SNP_TABLE"
my_query=my_query.upper()
if string.find(my_query, '*') > 0:
myRs = myStmt.executeQuery(my_query)
md=myRs.getMetaData()
collect=[]
i=1
while (i 0:
header = my_query[7:string.find(my_query, 'FROM')].replace("||','||",',')
else:
header = my_query[7:string.find(my_query, 'FROM')]
print header
old=output_write.read()
output_write.seek(0)
output_write.write (header+'n'+old)
sourceConnection.close()
output_write.close()
Nota:
Os códigos atualmente suportam apenas este tipo de formato de consultas
select * from table_name (or)
select column1,column2 from table_name (or)
select column1||’,’||column2 from table_name
Depois de executar o script acima para todas as colunas
( ou ) para menos colunas
Usefull?
Procure odiexperts para mais dicas e truques.
Obrigado por nos visitar!
agosto 4, 2015 at 11:20 AM
Hi,
I have copied the given code into an ODI procedure and ran it after changing the filepath and source table. I get the following error:
org.apache.bsf.BSFException: exception from Jython:
SyntaxError: (“no viable alternative at input ‘0’”, (”, 15, 14, ‘ while (i 0:\n’))
at org.apache.bsf.engines.jython.JythonEngine.exec(JythonEngine.java:146)
at com.sunopsis.dwg.codeinterpretor.SnpScriptingInterpretor.execInBSFEngine(SnpScriptingInterpretor.java:322)
at com.sunopsis.dwg.codeinterpretor.SnpScriptingInterpretor.exec(SnpScriptingInterpretor.java:170)
at com.sunopsis.dwg.dbobj.SnpSessTaskSql.scripting(SnpSessTaskSql.java:2472)
at oracle.odi.runtime.agent.execution.cmd.ScriptingExecutor.execute(ScriptingExecutor.java:47)
at oracle.odi.runtime.agent.execution.cmd.ScriptingExecutor.execute(ScriptingExecutor.java:1)
at oracle.odi.runtime.agent.execution.TaskExecutionHandler.handleTask(TaskExecutionHandler.java:50)
at com.sunopsis.dwg.dbobj.SnpSessTaskSql.processTask(SnpSessTaskSql.java:2913)
at com.sunopsis.dwg.dbobj.SnpSessTaskSql.treatTask(SnpSessTaskSql.java:2625)
at com.sunopsis.dwg.dbobj.SnpSessStep.treatAttachedTasks(SnpSessStep.java:558)
at com.sunopsis.dwg.dbobj.SnpSessStep.treatSessStep(SnpSessStep.java:464)
at com.sunopsis.dwg.dbobj.SnpSession.treatSession(SnpSession.java:2093)
at oracle.odi.runtime.agent.processor.impl.StartSessRequestProcessor$2.doAction(StartSessRequestProcessor.java:366)
at oracle.odi.core.persistence.dwgobject.DwgObjectTemplate.execute(DwgObjectTemplate.java:216)
at oracle.odi.runtime.agent.processor.impl.StartSessRequestProcessor.doProcessStartSessTask(StartSessRequestProcessor.java:300)
at oracle.odi.runtime.agent.processor.impl.StartSessRequestProcessor.access$0(StartSessRequestProcessor.java:292)
at oracle.odi.runtime.agent.processor.impl.StartSessRequestProcessor$StartSessTask.doExecute(StartSessRequestProcessor.java:855)
at oracle.odi.runtime.agent.processor.task.AgentTask.execute(AgentTask.java:126)
at oracle.odi.runtime.agent.support.DefaultAgentTaskExecutor$2.run(DefaultAgentTaskExecutor.java:82)
at java.lang.Thread.run(Thread.java:745)
Caused by: SyntaxError: (“no viable alternative at input ‘0’”, (”, 15, 14, ‘ while (i 0:\n’))
at org.python.core.ParserFacade.fixParseError(ParserFacade.java:96)
at org.python.core.ParserFacade.parse(ParserFacade.java:203)
at org.python.core.Py.compile_flags(Py.java:1685)
at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:172)
at org.apache.bsf.engines.jython.JythonEngine.exec(JythonEngine.java:144)
… 19 more
Can you please help in this?
agosto 23, 2013 at 10:25 AM
hi,
In odisqlload we have parameters, in which parameter i have to enter this code.
Please guide me,
Thanks,
dravid
agosto 27, 2013 at 7:07 PM
Its a jython code you need to call it in a separate odi procedure
setembro 3, 2010 at 12:02 AM
Hi,
using the code I am getting the following error:
org.apache.bsf.BSFException: exception from Jython:
Traceback (innermost last):
(no code object) at line 0
SyntaxError: (‘invalid syntax’, (”, 12, 1, ‘myRs = myStmt.executeQuery(my_query)’))
Please guide.
Thanks
setembro 3, 2010 at 12:00 AM
Hi,
On using the above code I am getting this error:
org.apache.bsf.BSFException: exception from Jython:
Traceback (innermost last):
(no code object) at line 0
SyntaxError: (‘invalid syntax’, (”, 12, 1, ‘myRs = myStmt.executeQuery(my_query)’))
Can you please guide me?
Thanks,
Ritika
setembro 3, 2010 at 4:54 AM
Ritika,
Please look at this thread – http://forums.oracle.com/forums/thread.jspa?messageID=4527748� i have shown a working example and above your code , please check if the query defined in my_query is correct. If you are unable to find the issue , please send me the query at kdevendr@gmail.com and i will fix it and send back to you.
Thanks
Dev
agosto 17, 2010 at 2:33 PM
hi, this scripts works if the source datastore is as400 db2?
agosto 17, 2010 at 6:57 PM
Hi Raul ,
The script will work ,but the query is written for Oracle as Source for DB2 you need to write the query to fetch the columns names from DB2 system tables ,If i am not wrong in db2 you can get column information from SYSTABLES,SYSCOLUMNS.
In the command on Source provide the required technology and Schema and in the command on Target ,change the sqlstmt to the respective query for db2.
Else manually pass the column name list into res as shown in the second part of the code and the header will be written into the File.