How to execute select query on oracle database using pi spark? - oracle

I have written a program using pyspark to connect to oracle database and fetch data. Below command works fine and returns the contents of the table:
sqlContext.read.format("jdbc")
.option("url","jdbc:oracle:thin:user/password#dbserver:port/dbname")
.option("dbtable","SCHEMA.TABLE")
.option("driver","oracle.jdbc.driver.OracleDriver")
.load().show()
Now I do not want to load the entire table data. I want to load selected records. Can I specify select query as part of this command? If yes how?
Note: I can use dataframe and execute select query on the top of it but I do not want to do it. Please help!!

You can use subquery in dbtable option
.option("dbtable", "(SELECT * FROM tableName) AS tmp where x = 1")
Here is similar question, but about MySQL

In general, the optimizer SHOULD be able to push down any relevant select and where elements so if you now do df.select("a","b","c").where("d<10") then in general this should be pushed down to oracle. You can check it by doing df.explain(true) on the final dataframe.

Related

OPENROWSET for Oracle

Could anybody help me translate this query from T-SQL to PLSQL, please?
I am using SQL Developer.
UPDATE schema.Table SET PICTURES =(SELECT * FROM OPENROWSET (BULK '\\shared folder\Picture.jpg', SINGLE_BLOB) a) WHERE (personID = 1111)
Thank you!
It looks to me like you are trying to apply a single image to a single person table row by selecting from an image file external to the database.
If my understanding is correct then I suggest you investigate Oracle EXTERNAL TABLE, DIRECTORY object and DBMS_LOB package.
Using some variant of these will certainly provide the solution you need.

getting error while executing same query in Oracle

This query working as expected in postgres but same query we need to write in Oracle could you please suggest how to write query in Oracle
delete from need_entl_status_history
using need_entitlement
where need_entitlement.need_entitlement_uuid=need_entl_status_history.need_entitlement_uuid
and need_entitlement.user_guid='b8e06968-2839-4fc1-a987-5ea81678d9ge’;
That would be something like this; I used table aliases as they improve readability.
delete from need_entl_status_history h
where h.need_entitlement_uuid in (select e.need_entitlement_uuid
from need_entitlement e
where e.user_guid = 'b8e06968-2839-4fc1-a987-5ea81678d9ge’
);

How can I run Hive Explain command from java code?

I want to run Hive and Impala Explain and compute stats command from java code. So that I can use the collected information for my analysis purpose. If any one have any idea please help
You can run it as any other jdbc query against impala.
The compute stats query for a table called temp would be "compute stats temp" and you can pass this as an argument for the jdbc statement.execute
Similarly, to explain a query, say "select count( * ) from temp" the query to pass as an argument for statement.execute is "explain select count(*) from temp".

How to export the user's data and restrict table rownum?

I want export data something like this.
exp xxx/xxx file=d:\xxx.dmp owner=xxx query=\"where rownum < 1000\"
But I get an error "QUERY parameter is only use in table mode"
Oracle version 10g
As #Thilo says, with exp you can only user the query parameter in table mode. If you're able to use the newer data pump functionality, via the expdp command, you can apply a similar query parameter to the whole export.
#Thilo is right, you can export a single table or a SUBSET of a single table
I also recommend reading Tom's advice in regards to using parfile

Oracle: LONG RAW to?

I am writing a program in java where I need to create a copy of a table (without data). for that I am using the following query
CREATE TABLE NEW_TABLE AS
SELECT * FROM OLD_TABLE
I have come across a table where one of the columns has the data type LONG RAW which is depricated.
I tried using the query below but it did not work. (ORA-01003: no statement parsed
)
CREATE TABLE NEW_TABLE AS
SELECT ID, COL1, COL2, TO_LOB(COL3) FROM OLD_TABLE
Can someone tell me a simple query for this. It should be able to store the values from the previous table. I am using oracle 10g
Thanks in advance.
EDIT:
Sorry it was my mistake, the above query worked fine but I was calling executeQuery instead of executeUpdate
Perhaps this discussion would help.
Sorry it was my mistake, the above query worked fine but I was calling executeQuery instead of executeUpdate which was throwing an SQLException

Resources