mismatched input 'ROW' expecting <EOF> error while creating hive table - java

I am trying to create a hive table using java.
Here is my code:
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveCreateTable {
private static String driverName = "com.facebook.presto.jdbc.PrestoDriver";
public static void main(String[] args) throws SQLException {
// Register driver and create driver instance
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("haiiiiii");
Connection con = DriverManager.getConnection("jdbc:presto://192.168.1.100:8023", "", "");
con.setCatalog("hive");
con.setSchema("log");
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery("create table access_log (c_ip varchar,cs_username varchar,cs_computername varchar,cs_date varchar,cs_code varchar,cs_method varchar,cs_uri_stem varchar,cs_uri_query varchar,cs_status_code varchar,cs_bytes varchar) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\b'LINES TERMINATED BY '\n'STORED AS TEXTFILE");
System.out.println("Table access_log4 created.");
ResultSet res1 = stmt.executeQuery("LOAD DATA LOCAL INPATH '/home/hadoop/access_log.txt'" + "OVERWRITE INTO TABLE access_log4;");
System.out.println("data loaded to access_log4.");
con.close();
}
}
and getting following error:
Exception in thread "main" java.sql.SQLException: Query failed
(#20150805_063004_00002_3dvaz): line 1:214: mismatched input 'ROW'
expecting
If we remove "ROW FORMAT DELIMITED FIELDS TERMINATED BY '\b'LINES TERMINATED BY '\n'STORED AS TEXTFILE" table is creating but data is not loaded.

Related

Invalid operation for read only resultset: moveToInsertRow

I try to insert record without using insert query, but I get:
SQLException - Invalid operation for read only resultset: moveToInsertRow.
I've attached my code:
package com.phoenix.demos;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class RandomAccessData {
public static void main(String[] args) {
//Class.forName("oracle.jdbc.driver.OracleDriver");
try {
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","system","deepa1994");
String query ="Select * from users";
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(query);
rs.next();
rs.last();
rs.next();
rs.absolute(4);
rs.previous();
rs.relative(-2);
System.out.println(rs.getString(1));
//To add a record - here i get error**
rs.moveToInsertRow();;
rs.updateString(1,"B");
rs.updateString(2, "C");
rs.insertRow();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I attached output screen to understand error message:

JDBC SQL Server errors: ClassNotFound & Not Suitable Driver Found

I am very new to Java and am simply trying to connect to my MSSQL database and return a list of customers. When I try the JDBC connection, I get a "no suitable driver found" error. When I add a Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") statement, I get a ClassNotFound error. This seems like it should be a lot easier than it's turning out to be. Please help me either find a suitable driver or how to get access through the Class.forName usage.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
public class DbConn {
public static String getConnString(){
return "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;database=OhHold;";
}
public static void getConnection() {
try
{
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String user = "<USER>";
String pw = "****************";
Connection connection = DriverManager.getConnection(getConnString(), user, pw);
Statement statement = connection.createStatement();
String sql = "select txtCompanyName as company from tblCustomers where intNotActive <> 1";
ResultSet result = statement.executeQuery(sql);
while (result.next()) {
System.out.println(result.getString(1));
}
}
/*
// Handle any errors that may have occurred.
catch (ClassNotFoundException e) {
e.printStackTrace();
}
*/
catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
getConnection();
}
}

Connect Oracle Java stored procedure to Snowflake

I was trying to have snowflake db connection using oracle Java stored procedure. But it's giving me error
ORA-29532: Java call terminated by uncaught Java exception: java.sql.SQLException: No suitable driver found
I have already downloaded snowflake-jdbc-3.6.19-javadoc.jar from related source.
Below is sample code of java class which will be called from oracle java procedure.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class SnowflakeDriverExample
{
public static void testdata() throws Exception
// public static void main(String[] args) throws Exception
{
// get connection
System.out.println("Create JDBC connection");
Connection connection = getConnection();
System.out.println("Done creating JDBC connectionn");
// create statement
System.out.println("Create JDBC statement");
Statement statement = connection.createStatement();
System.out.println("Done creating JDBC statementn");
// query the data
System.out.println("Query demo");
ResultSet resultSet = statement.executeQuery("SELECT DATA_ID FROM TEMP_TABLE");
System.out.println("Metadata:");
System.out.println("================================");
// fetch metadata
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
System.out.println("Number of columns=" +
resultSetMetaData.getColumnCount());
statement.close();
}
private static Connection getConnection()
throws SQLException
{
try
{
Class.forName("net.snowflake.client.jdbc.SnowflakeDriver");
}
catch (ClassNotFoundException ex)
{
System.err.println("Driver not found");
}
// Class.forName("net.snowflake.client.jdbc.SnowflakeDriver");
// build connection properties
Properties properties = new Properties();
properties.put("user", "XXX"); // replace "" with your username
properties.put("password", "XXXX"); // replace "" with your password
properties.put("account", "XXXX"); // replace "" with your account name
properties.put("db", "db1"); // replace "" with target database name
properties.put("schema", "MYSCHEMA"); // replace "" with target schema name
//properties.put("tracing", "on");
// create a new connection
String connectStr = null;//System.getenv("SF_JDBC_CONNECT_STRING");
// use the default connection string if it is not set in environment
if(connectStr == null)
{
connectStr = "jdbc:snowflake:XXXX"; // replace accountName with your account name
}
return DriverManager.getConnection(connectStr, properties);
}
}
How to import/integrate this driver for java stored procedure?

Unable to get column name and column lable in sql server result set meta data

Can anybody help me to get actual column name and its alias using ResultSetMetaData by connecting to sqlserver database.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
class MysqlCon {
public static void main(String args[]) {
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=alcoa", "sa", "test");
Statement stmt = con.createStatement();
String query = "SELECT std_code as \"Student code\" from Student ";
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsm =rs.getMetaData();
for (int i = 1; i <= rsm.getColumnCount(); i++) {
System.out.println(rsm.getColumnLabel(i) + "--" + rsm.getColumnName(i));
}
} catch (Exception e) {
System.out.println(e);
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
output
Student code--Student code
Above code works as expected in other database like mysql and oracle.
I checked code for SQLServerResultSetMetaData. Both methods getColumnLabel and getColumnName are identical.
abzycdxw65, has raised same issue on their github account. it is closed.
Is there any way to get following output:
Student code--std_code
If I get your question you can try the followings to retrieve column names
select name from sys.columns

How store count value from sql query in to a variable for creating a customerid according to the no of customers in the table?

I want to store the count value received into a variable count which then can be appended to a string to prepare a customer id but it is giving me the cursor invalid error. Please have a look below and help me out. thanks in advance. :)
**package components;
import java.awt.geom.GeneralPath;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class genrateid {
public void generateid(){
int count;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:XE",
"system", "tiger");
System.out.println("Connection Successfull");
System.out.println(conn);
//--------------------------------------------------------------------
Statement stmt = conn.createStatement();
String query1= "select count(*) as customercount from customers";
ResultSet rs= stmt.executeQuery(query1);
count = rs.getInt(1);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
genrateid gen = new genrateid();
gen.generateid();
}
}**
I am receiving the following error in in eclipse IDE:
OUTPUT:
Connection Successfull
sun.jdbc.odbc.JdbcOdbcConnection#39b27b
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(Unknown Source)
at components.genrateid.generateid(genrateid.java:29)
at components.genrateid.main(genrateid.java:43)
insert this line
if(rs.next())
before
count = rs.getInt(1);
and you'll be good. But, as Dems said, consider using IDENTITY column (or because you are using Oracle - SEQUENCE)

Categories