<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Getting a random value from DB table/column in Talend Studio</title>
    <link>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292744#M65786</link>
    <description>I think I may have found the source of my problem after looking at the result in tLogRow. The first row was correct in getting a random value from a table in the DB as I expected but subsequent rows (therefore calls to my custom function) would have the connection closed error.
&lt;BR /&gt;I remove the lConnection.close() code and VOILA, now I get random values for my targeted column for all generated row.
&lt;BR /&gt;It will be nice to have this kind of DB lookup capability added to the tRowGenerator component. I will try to make it a request</description>
    <pubDate>Sun, 16 Aug 2009 18:43:44 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2009-08-16T18:43:44Z</dc:date>
    <item>
      <title>Getting a random value from DB table/column</title>
      <link>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292739#M65781</link>
      <description>Hi all, 
&lt;BR /&gt;I am new to TOS and Java and I am trying to "extend" my tRowGenerator component to use a function defined in routines that would connect to a DB defined in Metadata and randomly get a value from a table/column. My "code" is below (no laughing, guys, be nice). This function would be defined for a column in the Map Editor of the tRowGenerator component. 
&lt;BR /&gt;I know that this way is not efficient at all because for each row created in tRowGenerator, a connection to the DB is created. opened, data read and then the connection is closed and destroyed. I would love to be able to persist the first connection and reuse it for the rest of the rows to be created by the tRowGenerator component. 
&lt;BR /&gt;I tried dropping a tOracleConnection linked to the tRowGenerator via OnComponentOK, but I could not get the connection passed to my function with (java.sql.Connection) globalMap.get("conn_" + "tOracleConnection_1") but I get a null pointer error, as if the connection object never gets created. 
&lt;BR /&gt;How can I get access to a connection object that points to the object that, I assume, must be created by dropping the tOracleConnection component? I was thinking that I could pass the connection object as a parameter to my function like this: (java.sql.Connection) globalMap.get("conn_" + "tOracleConnection_1"). 
&lt;BR /&gt;Any ideas? 
&lt;BR /&gt;Many thanks 
&lt;BR /&gt;GD 
&lt;BR /&gt;--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
&lt;BR /&gt; public static String getRandomDBValue(String tableName, String columnName) { 
&lt;BR /&gt; //java.sql.Connection DBConn, 
&lt;BR /&gt; 
&lt;BR /&gt; String returnValue = null; 
&lt;BR /&gt; Statement lStatement = null; 
&lt;BR /&gt; ResultSet lRS = null; 
&lt;BR /&gt; 
&lt;BR /&gt; String lSQLString = "select distinct " + columnName + " from (select distinct " + columnName + " from " +tableName + " order by dbms_random.value ) where rownum=1"; 
&lt;BR /&gt; 
&lt;BR /&gt; try { 
&lt;BR /&gt; java.lang.Class.forName("oracle.jdbc.driver.OracleDriver"); 
&lt;BR /&gt; String url_tOracleConnection_1 = "jdbc 
&lt;span class="lia-inline-image-display-wrapper" image-alt="0683p000009MA5A.png"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/143082iB236712184B767DA/image-size/large?v=v2&amp;amp;px=999" role="button" title="0683p000009MA5A.png" alt="0683p000009MA5A.png" /&gt;&lt;/span&gt;racle:thin:@" + "localhost" + ":" + "1521" + ":" + "XE"; 
&lt;BR /&gt; String dbUser_tOracleConnection_1 = "user"; 
&lt;BR /&gt; String dbPwd_tOracleConnection_1 = "pwd"; 
&lt;BR /&gt; java.sql.Connection lConnection = null; 
&lt;BR /&gt; 
&lt;BR /&gt; lConnection = java.sql.DriverManager.getConnection(url_tOracleConnection_1, dbUser_tOracleConnection_1,dbPwd_tOracleConnection_1); 
&lt;BR /&gt; lStatement = lConnection.createStatement(); 
&lt;BR /&gt; lRS = lStatement.executeQuery(lSQLString); 
&lt;BR /&gt; 
&lt;BR /&gt; if(lRS.next()) { 
&lt;BR /&gt; returnValue = lRS.getString(1); 
&lt;BR /&gt; } 
&lt;BR /&gt; lRS.close(); 
&lt;BR /&gt; lStatement.close(); 
&lt;BR /&gt; lConnection.close(); 
&lt;BR /&gt; return returnValue; 
&lt;BR /&gt; } 
&lt;BR /&gt; catch (Exception ee) { 
&lt;BR /&gt; return "Error connecting/executing statement/query at the DB "; 
&lt;BR /&gt; } 
&lt;BR /&gt; }</description>
      <pubDate>Sat, 16 Nov 2024 13:50:01 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292739#M65781</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-11-16T13:50:01Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a random value from DB table/column</title>
      <link>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292740#M65782</link>
      <description>Hi,&lt;BR /&gt;I would guess that you "tRowGenerator-function" is called before the database connection is opened. Could you show your job design or try to connect the components with an onComponentOk - link.&lt;BR /&gt;Bye&lt;BR /&gt;Volker</description>
      <pubDate>Fri, 14 Aug 2009 21:34:38 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292740#M65782</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-08-14T21:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a random value from DB table/column</title>
      <link>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292741#M65783</link>
      <description>Hi Volker, 
&lt;BR /&gt;Thank you for taking time to answer my plea for help. 
&lt;BR /&gt;I tried dropping an OracleConnection component and linked it to the tRowGenerator component via onComponentOk (see screenshot sc1 below). Assuming that the connection is created at runtime, I changed my custom function to below: 
&lt;BR /&gt;public static String getRandomDBValue_WIP(Object object,String tableName, String columnName) { 
&lt;BR /&gt; String returnValue = null; 
&lt;BR /&gt; Statement lStatement = null; 
&lt;BR /&gt; ResultSet lRS = null; 
&lt;BR /&gt; 
&lt;BR /&gt; String lSQLString = "select distinct " + columnName + " from (select distinct " + columnName + " from " +tableName + " order by dbms_random.value ) where rownum=1"; 
&lt;BR /&gt; Connection lConnection = (Connection) object; 
&lt;BR /&gt; 
&lt;BR /&gt; try { 
&lt;BR /&gt; lStatement = lConnection.createStatement(); 
&lt;BR /&gt; lRS = lStatement.executeQuery(lSQLString); 
&lt;BR /&gt; 
&lt;BR /&gt; if(lRS.next()) { 
&lt;BR /&gt; returnValue = lRS.getString(1); 
&lt;BR /&gt; } 
&lt;BR /&gt; lRS.close(); 
&lt;BR /&gt; lStatement.close(); 
&lt;BR /&gt; lConnection.close(); 
&lt;BR /&gt; return returnValue; 
&lt;BR /&gt; } 
&lt;BR /&gt; catch (Exception eee) { 
&lt;BR /&gt; return "no --- " + eee.toString(); 
&lt;BR /&gt; } 
&lt;BR /&gt; } 
&lt;BR /&gt;I call this function as a parameter for the target column in the tRowGenerator Editor (see sc3): 
&lt;BR /&gt;my_custom_code.getRandomDBValue_WIP(globalMap.get("conn_" + "tOracleConnection_1"),"EVENT2", "CREATED_BY") 
&lt;BR /&gt;EVENT2 is the table I am targeting 
&lt;BR /&gt;CREATED_BY is the column that I want to randomly pick a value from and insert as value for my generated column 
&lt;BR /&gt;When I run this I get the error in sc2: java.sql.SQLException: Closed 
&lt;BR /&gt;I am thinking that the connection is never opened in the first place. 
&lt;BR /&gt;Any ideas? 
&lt;BR /&gt;Thank you in advance 
&lt;BR /&gt;Gallas</description>
      <pubDate>Sat, 15 Aug 2009 23:55:11 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292741#M65783</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-08-15T23:55:11Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a random value from DB table/column</title>
      <link>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292742#M65784</link>
      <description>If you want to persist your hardwired connection avoiding an open each time, place the Connection object outside the method, as a static member in the class; then you can reuse it.</description>
      <pubDate>Sun, 16 Aug 2009 11:04:38 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292742#M65784</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-08-16T11:04:38Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a random value from DB table/column</title>
      <link>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292743#M65785</link>
      <description>Emaxt6,&lt;BR /&gt;I would like to avoid using the hardwired connection altogether.  I want to reuse the connection created by having an tOracleConnection component that is defined in the repository.&lt;BR /&gt;I was trying your solution by placing the connection object and code in a static function outside, but I still have to call it in my code in tRowGenerator for every row.  I will have to check for connection existence and all of that.&lt;BR /&gt;Please note I am new to Java and just learning.&lt;BR /&gt;Many thanks</description>
      <pubDate>Sun, 16 Aug 2009 18:09:44 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292743#M65785</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-08-16T18:09:44Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a random value from DB table/column</title>
      <link>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292744#M65786</link>
      <description>I think I may have found the source of my problem after looking at the result in tLogRow. The first row was correct in getting a random value from a table in the DB as I expected but subsequent rows (therefore calls to my custom function) would have the connection closed error.
&lt;BR /&gt;I remove the lConnection.close() code and VOILA, now I get random values for my targeted column for all generated row.
&lt;BR /&gt;It will be nice to have this kind of DB lookup capability added to the tRowGenerator component. I will try to make it a request</description>
      <pubDate>Sun, 16 Aug 2009 18:43:44 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/Getting-a-random-value-from-DB-table-column/m-p/2292744#M65786</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-08-16T18:43:44Z</dc:date>
    </item>
  </channel>
</rss>

