Error inserting data into orc table - orc

Even am facing the same problem of loading data from a text format table to orc format table.When i try to load data from a text format table to orc table am getting this error "Exception in thread "main" java.sql.SQLException: Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask"
This is my code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.hive.jdbc.HiveDriver;
public class HiveJdbcClient {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive2://hdn01.sasken.com:10000/default", "","");
System.out.println("Connected to Hive");
Statement stmt = con.createStatement();
String tempTable = "tempTable";
String orcTable= "orcTable";
stmt.execute("CREATE TABLE IF NOT EXISTS "+ tempTable +" (key int, value string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE");
stmt.execute("CREATE TABLE IF NOT EXISTS " + orcTable + "(key int, value string) CLUSTERED BY (key) INTO 2 BUCKETS STORED AS ORC TBLPROPERTIES ('transactional'='true')");
String filepath = "/home/ee208961/spark-hive/resto.csv";
stmt.execute("LOAD DATA LOCAL INPATH '" + filepath + "' OVERWRITE INTO TABLE " + tempTable);
stmt.execute("INSERT OVERWRITE TABLE "+ orcTable +" SELECT * FROM "+ tempTable);
String sql = "select * from " + orcTable;
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
}
am getting error on this line "stmt.execute("INSERT OVERWRITE TABLE "+ orcTable +" SELECT * FROM "+ tempTable)"
can any1 please help me in fixing this issue?
Thanku.

Related

DB2 - ERRORCODE=-4229, SQLSTATE=null

I'm using a batch class in EJB to INSERT more than 100 rows in the same commit using the command line executeBatch in the DB2.
When I execute the command shows this error: ERRORCODE=-4229, SQLSTATE=null.
The ID sequence is IDENTITY clause on the CREATE TABLE.
Table:
CREATE TABLE table (col1 INT,
col2 DOUBLE,
col3 INT NOT NULL GENERATED ALWAYS AS IDENTITY)
Does anyone have any idea?
ERROR:
Caused by: nested exception is: com.ibm.db2.jcc.am.BatchUpdateException: [jcc][t4][102][10040][4.24.97] Batch failure. The batch was submitted, but at least one exception occurred in an individual batch member.
Use getNextException() to retrieve exceptions for specific batch elements. ERRORCODE=-4229, SQLSTATE=null
It's not an answer, but a suggestion to handle Db2 exceptions to have an ability to deal with such errors.
If you are unable to rewrite your error handling, the only thing you can to is to enable JDBC trace on the client or/and set the Db2 dbm cfg DIAGLEVEL parameter to 4.
PreparedStatement pst = null;
try
{
pst = ...;
...
int [] updateCounts = pst.executeBatch();
System.out.println("Batch results:");
for (int i = 0; i < updateCounts.length; i++)
System.out.println(" Statement " + i + ":" + updateCounts[i]);
} catch (SQLException ex)
{
while (ex != null)
{
if (ex instanceof com.ibm.db2.jcc.DB2Diagnosable)
{
com.ibm.db2.jcc.DB2Diagnosable db2ex = com.ibm.db2.jcc.DB2Diagnosable) ex;
com.ibm.db2.jcc.DB2Sqlca sqlca = db2ex.getSqlca();
if (sqlca != null)
{
System.out.println("SQLCODE: " + sqlca.getSqlCode());
System.out.println("MESSAGE: " + sqlca.getMessage());
}
else
{
System.out.println("Error code: " + ex.getErrorCode());
System.out.println("Error msg : " + ex.getMessage());
}
}
else
{
System.out.println("Error code (no db2): " + ex.getErrorCode());
System.out.println("Error msg (no db2): " + ex.getMessage());
}
if (ex instanceof BatchUpdateException)
{
System.out.println("Contents of BatchUpdateException:");
System.out.println(" Update counts: ");
System.out.println(" Statement.SUCCESS_NO_INFO: " + Statement.SUCCESS_NO_INFO);
System.out.println(" Statement.EXECUTE_FAILED : " + Statement.EXECUTE_FAILED);
BatchUpdateException buex = (BatchUpdateException) ex;
int [] updateCounts = buex.getUpdateCounts();
for (int i = 0; i < updateCounts.length; i++)
System.out.println(" Statement " + i + ":" + updateCounts[i]);
}
ex = ex.getNextException();
}
}
...

Creating Trigger to auto-increment id by sequence in JDBC

I am writing methods through JDBC to create a table and a sequence to recall in a Trigger, I want to set up an id column which auto-increments before every insert on the table. I succeeded in building both the createTable method and the createSequence method in the DAO, but when I run the method to create the Trigger I got the java.sql.SQLException: Missing IN or OUT parameter at index:: 1
public void createTrigger() {
PreparedStatement ps;
StringBuilder queryTrigger = new StringBuilder();
queryTrigger.append("CREATE OR REPLACE TRIGGER ");
queryTrigger.append(Tables.getInstance().getName() + "_INSERTED\n");
queryTrigger.append("BEFORE INSERT ON " + Tabelle.getInstance().getName());
queryTrigger.append("\nFOR EACH ROW\n");
queryTrigger.append("BEGIN\n");
queryTrigger.append("SELECT " + Tables.getInstance().getName() + "SEQ.NEXTVAL\n");
queryTrigger.append("INTO :new.id\n");
queryTrigger.append("FROM dual;\n ");
queryTrigger.append("END;\n");
queryTrigger.append("/\n");
queryTrigger.append("ALTER TRIGGER " +Tabelle.getInstance().getName() + "_INSERTED ENABLE\n");
queryTrigger.append("/\n");
String stringQueryTrigger = queryTrigger.toString();
Connection conn = JDBCUtility.openConnection();
try {
ps = (PreparedStatement) conn.prepareStatement(stringQueryTrigger);
ps.executeUpdate();
ps.close();
} catch(SQLException e) {
e.printStackTrace();
}
JDBCUtility.closeConnection(conn);}
Here instead the creation of the table does actually work even if I don't
write the classic lines with parametrized "?" for the preparedStatement.setString(index, String)
public void createTable(Columns c) {
PreparedStatement ps;
StringBuilder query = new StringBuilder();
query.append("CREATE TABLE " + Tabelle.getInstance().getName() + "(");
query.append(Columns.getInstance().getColumnName() + " ");
query.append(Columns.getInstance().getDataType());
if(Columns.getInstance().isNullOrNot() == true) {
query.append(" NOT NULL");
}
else {
query.append("");
}
if(Columns.getInstance().isPrimaryKeyOrNot() == true) {
query.append(" PRIMARY KEY)");
}
else {
query.append(")");
}
String queryToString = query.toString();
Connection conn = JDBCUtility.openConnection();
try {
ps = (PreparedStatement) conn.prepareStatement(queryToString);
ps.executeUpdate();
ps.close();
} catch(SQLException e) {
e.printStackTrace();
}
JDBCUtility.closeConnection(conn);
}
//EDIT
turns out that is enough to substitute the PreparedStatement with a simple Statement, to get rid of the indexes mechanism and get the DB to accept the query
I would suggest creating an auto-increment sequnce in oracle that can be used for all ids and just add the string id.NextVal to the string query
What I mean is:
Rem in oracle create sequence
CREATE SEQUENCE ID
START BY 1
INCREMENT 1
// in java to execute query
String query = "INSERT INTO TABLE VALUES(ID.NEXTVAL);" ;
//rest of code

SQL Exception: Table does not exist when accessed using SOAP service

I am using Derby database in embedded mode. I have written a CreateDB class with main() function that creates/inserts/select in a table. Works fine.(I have necessary jar files in project build path:) )
But, when i use same database and table to select rows in my Soap webservice, it gives me error that table doesn't exist. here is the code:
For CreateDB (Works Perfectly fine)
package com.myShop.www.ShipmentService;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateDB {
public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
public static final String JDBC_URL = "jdbc:derby:Shipment_DB;create=true";
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Class.forName(DRIVER);
Connection connection = DriverManager.getConnection(JDBC_URL);
String SQLString = "CREATE TABLE SHPMTTYPAV_TBL1(" +
"SHPMTID INT NOT NULL, " +
"NAME VARCHAR(30) NOT NULL, " +
"DESCR VARCHAR(45) NOT NULL, " +
"MINCOST DOUBLE NOT NULL, " +
"COSTPEROUNCE DOUBLE NOT NULL, " +
"ESTDLVYDAYS INT NOT NULL, " +
"PRIMARY KEY(SHPMTID))";
connection.createStatement().execute((SQLString));
String insertSQL = "INSERT INTO SHPMTTYPAV_TBL1 VALUES(5, 'Priority Mail', 'Any item can be shipped', 5, .02, 1)";
connection.createStatement().execute(insertSQL);
// invoke sql query
String selectSQL = "SELECT * From SHPMTTYPAV_TBL1";
Statement prepStmt = connection.createStatement();
ResultSet rs = prepStmt.executeQuery(selectSQL);
while (rs.next()){
System.out.println(rs.getInt("SHPMTID") + rs.getString("NAME") + rs.getString("DESCR") + rs.getDouble("MINCOST") + " " + rs.getDouble("COSTPEROUNCE"));
}
}
}
Code in SOAP service Implementation class: - Error of Table not found
public class ShipmentServiceBindingImpl implements ShipmentServicePortType{
public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
public static final String JDBC_URL = "jdbc:derby:Shipment_DB;create=true";
public com.myShop.www.ShipmentService.ItemsInOrderResponseType[] getShpmtTps_Csts_DlvyDts(com.myShop.www.ShipmentService.ItemsInOrderType[] itemsInOrder) throws java.rmi.RemoteException, ClassNotFoundException, SQLException {
Class.forName(DRIVER);
Connection connection = DriverManager.getConnection(JDBC_URL);
String selectSQL = "SELECT * From SHPMTTYPAV_TBL1";
Statement prepStmt = connection.createStatement();
ResultSet rs = prepStmt.executeQuery(selectSQL);
int len = itemsInOrder.length;
ItemsInOrderResponseType[] res = new ItemsInOrderResponseType[len];
for(int i = 0; i < len; i++){
rs.next();
res[i] = new ItemsInOrderResponseType();
res[i].setShpmtID(rs.getInt("SHPMTID"));
res[i].setShpmtName(rs.getString("NAME"));
res[i].setCost(rs.getDouble("COSTPEROUNCE"));
res[i].setEstDlvyDays(rs.getDouble("MINCOST"));
}
if (rs != null) rs.close();
if (prepStmt != null) prepStmt.close();
if (connection != null) connection.close();
return res;
}
}
Try setting derby.system.home to the same value in both cases. If unset Derby uses the current directory which is likely different in the two cases.

Can anyone see why this SQL insert will not work? (Exception to String and Date to String Inserted) - JDBC

Im trying to basically make a log of errors generated from a Java program and save them in an SQL table with 2 colums( Error and Date )
Try and catch sends the error to the method which should hopefully insert into database but its failing somewhere. Im new enough to Java so im not sure how to debug it properly or get more details on wahts going wrong and where
Heres the method.
public void createErrorLog(Exception error) throws SQLException{
// Error as String
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
error.printStackTrace(pw);
sw.toString(); // stack trace as a string
String errorOne = sw.toString();
System.out.println(errorOne);
// Date
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
String dateString = dateFormat.format(date);
Connection conn = null;
try {
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
System.out.println("Connected to database.");
String insertSaleProperty = "INSERT INTO ErrorLogs"
+ "(Error, "
+ "Time, "
+ "(?,?)";
PreparedStatement preparedStatement = conn.prepareStatement(insertSaleProperty);
preparedStatement.setString(1, errorOne);
preparedStatement.setString(2, dateString);
// execute insert SQL statement
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.err.println("Cannot write to database.");
} finally {
if(conn != null){
conn.close();
}
}
}
Try the following.
String insertSaleProperty = "INSERT INTO ErrorLogs"
+ "(Error,Time) values "
+ "(?,?)";
String insertSaleProperty = "INSERT INTO ErrorLogs"
+ "(Error, "
+ "Time, "
+ "(?,?)";
Should probably be:
String insertSaleProperty = "INSERT INTO ErrorLogs "
+ "(Error, Time) "
+ "VALUES (?,?)";

Increasing the size of Varchar2() in oracle

Is it possible to increment the size of a column (say varchar2(25)) by 50? To be precise, I am not looking for something like this:
ALTER TABLE <Table_name> modify <Column_name> varchar2(75);
Rather, I am inquisitive about something that will increase the size by 50 or some other integer constant without the explicit calculation on the programmer part.
PS: Please comment if I am not clear.
Just to be clear, it appears you're asking for a way to add a fixed value to the column size without knowing what the original size is (hence asking how to add 50 and disallowing setting it directly to 75, which would require knowing it was 25 to start with).
Most databases provide system tables or views which give you the metadata about various objects. For example, DB2/z has sysibm.syscolumns and Oracle has all_tab_columns as shown in this link.
If you wanted to expand the column by 50 without knowing in advance what the size was, you could simply consult the metadata to get the current size and just add 50, constructing a statement to do it for you.
In other words, use something like:
select char_length from all_tab_columns
where owner = '<Table_owner>'
and table_name = '<Table_name>'
and column_name = '<Column_name>'
then extract that number from the recordset, add 50, and use that to dynamically construct and execute an alter table statement, similar to the one in your question that assumes you already know the length you want.
You can also use the user_tab_columns view if you're only concerned with your own tables rather than all those you can see. In that case, you don't need to concern yourself with the where owner = clause.
Although this sample code is specific to the DB2/z metadata, it wouldn't take much to convert it to the corresponding Oracle version:
import java.io.*;
import java.util.*;
import java.sql.*;
class chgcolsz {
public void chgcolsz() {}
public static void main (String args[]) {
Connection conn;
try {
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
conn = DriverManager.getConnection(
"jdbc:db2://MyBox:9999/MyInstance", "Me", "MyPassword");
conn.setAutoCommit (true);
} catch (Exception e) {
System.out.println ("** Error: DB connect: " + e);
e.printStackTrace();
return;
}
String cmd =
"select length from sysibm.syscolumns" +
" where tbcreator = 'PAX'" +
" and tbname = 'XYZZY'" +
" and name = 'COLUMN1'";
ResultSet rs;
try {
Statement sttmnt = conn.createStatement();
rs = sttmnt.executeQuery (cmd);
} catch (Exception e) {
rs = null;
System.out.println ("** Warning: rowset create: '" +
cmd + "': " + e);
e.printStackTrace();
}
int sz = -1;
if (rs != null) {
try {
rs.next();
sz = rs.getInt(1);
rs.close();
} catch (Exception e) {
System.out.println ("** Warning: rowset close: " + e);
e.printStackTrace();
};
}
if (sz != -1) {
System.out.println ("Current size is " + sz);
cmd = "alter table pax.xyzzy" +
" alter column column1" +
" set data type" +
" varchar(" + (sz + 50) + ")";
System.out.println ("Executing: " + cmd);
try {
Statement sttmnt = conn.createStatement();
sttmnt.execute (cmd);
} catch (Exception e) {
System.out.println ("** Warning: table alter: '" +
cmd + "': " + e);
e.printStackTrace();
}
}
try {
conn.close();
} catch (Exception e) {
System.out.println ("** Warning: DB close: " + e);
e.printStackTrace();
};
}
}
You can see from subsequent runs of this program that it's increasing the column width by 50 each time:
pax> java chgcolsz
Current size is 50
Executing: alter table pax.xyzzy alter column column1 set data type varchar(100)
pax> java chgcolsz
Current size is 100
Executing: alter table pax.xyzzy alter column column1 set data type varchar(150)
pax> java chgcolsz
Current size is 150
Executing: alter table pax.xyzzy alter column column1 set data type varchar(200)