Insert data into HIVE over HADOOP - java

I am using hadoop-1.0.4 and hive-0.10.0 in redhat5. Service start successfully. I am able to create, drop, select table easily but I don't know how to insert data.
For example I have two text box and on button click I want to store data in table (userInfo). I have no clue how to store textbox vaue in userInfo(id,password).
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/enggheads","", "");
Statement stmt = con.createStatement();
String tableName = "testHiveDriverTable";
stmt.executeQuery("drop table " + tableName);
ResultSet res = stmt.executeQuery("create table " + tableName + " (key int, value string)");
// show tables
String sql = "show tables '" + tableName + "'";
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
}
It's Java, but I don't know how to insert two field value because Hive insertion is different than MySQL or other database syntax.

Create a dummy table in hive like below
create table dummy(dummy string) location '/path';
Above path will have a file which contains data X
Now run insert query from jdbc driver like below.
insert into table tblname select forntendvalue1,frontendvalue2 from dual;

Related

How to Properly use Select Max() in Eclipse

I am working on a school project where I must interact with an access database. I'm attempting to
SELECT Max(GameID) AS MaxID
FROM Games
However, This query when ran through the Eclipse application I built only returns in the console
SQL Exception: UCAExc:::4.0.3 Column not found: GameID
SQL State: S1000
Vendor Error: -421
I have checked the access database and the column DEFINITELY EXISTS. I ran the query in the access database and it worked in there as well. I'm not sure what I'm missing or if this is possible. How can i grab the highest value of gameID's?
here is the connection to the database
ResultSet rs = null; //will hold record that get returned
Statement stmt = null; //will hold the SQL statement we want to run
try
{
//2. Establish the connection
Connection conn = DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Public/ZaccaroBlottoDB.accdb");
//3. Create the statement
stmt = conn.createStatement();
String theQuery = "SELECT Max("
+ "GameID)"
+ " As MaxID"
+ " FROM Games"
+ " WHERE (1=1)";
//4. Execute the statement
rs = stmt.executeQuery(theQuery);
//5. Process the results
while (rs.next())
{
int gameID = rs.getInt("GameID"); //note the type and the field name from the DB
System.out.println(gameID);
//addGameIDFTF.setText(Integer.toString(gameID +1));
}//while
//6. Close the Connection
rs.close();
conn.close();
}
catch (SQLException ex)
{
System.out.println("SQL Exception: " + ex.getMessage());
System.out.println("SQL State: " + ex.getSQLState());
System.out.println("Vendor Error: " + ex.getErrorCode());
ex.printStackTrace();
} //catch
I think the issue is the value that you are retrieving. As you have mentioned the alias name to be MaxID you should get MaxID from result_set instead of GameID
Hence, It should be
int gameID = rs.getInt("MaxID");
instead of
int gameID = rs.getInt("GameID");

Get the value of an auto-increment-field within the transaction [duplicate]

This question already has answers here:
How to get the insert ID in JDBC?
(14 answers)
Closed 5 years ago.
I'm using H2DB for a litthe cuesheet-database. I'm inserting several records to a table with anj auto-increment field for the primary key ID. For each record I want to know the value of the ID-field after insert, i.e. before committing. How do I get this value?
In short:
use Statement.RETURN_GENERATED_KEYS as second parameter when preparing the insert statement
get ResultSet from statement after insert with .getGeneratedKeys()
get generated id from ResultSet
This should also work when using transactions.
The following example demonstrates this:
try {
// register driver
Class.forName("org.h2.Driver");
// open connection, in-memory database
Connection conn = DriverManager.getConnection("jdbc:h2:mem:");
conn.setAutoCommit(false);
// create table
PreparedStatement createSample = conn.prepareStatement("CREATE TABLE sample (id int not null auto_increment, txt varchar(128))");
createSample.executeUpdate();
createSample.close();
// prepare insert statement
PreparedStatement insertStatement = conn.prepareStatement("INSERT INTO sample (txt) VALUES (?)", Statement.RETURN_GENERATED_KEYS);
// dummy list with texts
List<String> dummyTexts = Arrays.asList("Entry A", "Entry B", "Entry C", "Entry D", "Entry E");
// insert data
for (String dummyText : dummyTexts) {
insertStatement.setString(1, dummyText);
insertStatement.executeUpdate();
// get generated key
ResultSet generatedKeys = insertStatement.getGeneratedKeys();
if ((generatedKeys != null) && (generatedKeys.next())) {
int generatedKey = generatedKeys.getInt(1);
System.out.println("generated key " + generatedKey + " for entry '" + dummyText + "'");
}
}
// commit
conn.commit();
insertStatement.close();
// select data
PreparedStatement selection = conn.prepareStatement("SELECT id, txt FROM sample");
ResultSet selectionResult = selection.executeQuery();
while (selectionResult.next()) {
System.out.println("id: " + selectionResult.getInt(1) + ", txt: '" + selectionResult.getString(2) + "'");
}
selectionResult.close();
selection.close();
// close connection
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}

Change datatype using java jdbc of MySQL table

How to change the datatype from varchar to int using JDBC connection with Java of a table in MySQL?
I tried to use the query:
Alter table table_name CHANGE 'col 1' 'col 1' int (5 );
The file I am importing using the import option has the values in varchar, but I only required integer values from that table. The values of the imported table are not in ordered but the integer values are useful for me.
When I used the alter query in the phpmyadmin it works absolutely fine, but when I tried to alter in jdbc it gives me the error:
Incorrect integer value: at 'col 1';
jdbc code was as:
try {
Connection co=DriverManager.getConnection("jdbc:mysql://localhost:3306/csv_db","root",null);
String c= " ALTER TABLE tpf1 MODIFY `col 3` int(5)";
String t="Alter table table_name CHANGE 'col 1' 'col 1' int (5 );";
Statement stmt=co.createStatement();
stmt.executeUpdate(t);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, ex);
}
Edit
try {
Connection co=DriverManager.getConnection("jdbc:mysql://localhost:3306/csv_db","root",null);
String c= " ALTER TABLE tpf1 MODIFY `col ` int(20)";
Statement stmt=co.createStatement();
stmt.executeUpdate(t);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, ex);
}
The table screenshot is in the Photo:
This is actually a pdf file and converted it into .csv and imported to my database
And i only need integer values from the table not any other alphabets and symbol.
This code is only for one column and i need to alter all the three columns.So what should i do to get only integer values using jdbc? :| :)
Like #Mark Rotteveel said in comment, you have a problem in your Query so the name of your columns should be between two `` and not between two '' or "" this not work if your column name contain more then one part so to change the type of your column you should to use this :
//change column type
String query = "ALTER TABLE tablename MODIFY `col 1` INT(5)";
Statement stmt = co.createStatement();
int rslt = stmt.executeUpdate(query);
To change the name or your column use this :
//change column name
query = "ALTER TABLE tablename CHANGE `old name` `new name` INT(5);";
stmt = co.createStatement();
rslt = stmt.executeUpdate(query);
so you can check if your query executed successfully like this :
if (rslt > 0) {
System.out.println("Success");
} else {
System.out.println("Failed");
}
Note
If you change the type this will change all the column, and set it to 0
I suggest to avoid two use a name with two parts you can use col_name instead because this make a problem for example if you are using JPA.
EDIT
I try this piece of code and it work fine :
public class UpdateColumn {
static String driver = "com.mysql.jdbc.Driver";
static String DB_username = "root";
static String DB_password = "password";
static String DB_URL = "jdbc:mysql://localhost:3306/db_name";
public static void main(String args[]) {
/*read from the file*/
try {
Class.forName(driver);
java.sql.Connection co = DriverManager.getConnection(DB_URL, DB_username, DB_password);
//change column type
String query = "ALTER TABLE tablename MODIFY `c 1` INT(5);";
Statement stmt = co.createStatement();
int rslt = stmt.executeUpdate(query);
System.out.println(rslt);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
MySQL
create database db_name;
use db_name;
drop table t1;
create table tablename(
`c 1` varchar(5),
`c 2` int
);
INSERT INTO tablename values('java', 1);
INSERT INTO tablename values('mysql', 2);
Good luck.
statement.executeUpdate("ALTER TABLE table_name ALTER COLUMN column_name datatype");

H2 Embedded Database tables not shown in Intellij Database Client

I'm learning how to implement a database into a music player application, and decided to use the embedded H2 database for my implementation.
public class H2EmbeddedDB {
public H2EmbeddedDB() {
try {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/data/musicAppDB", "sa", "");
Statement st = conn.createStatement();
/*
String createTableSQL = "CREATE TABLE PLAYLISTS " +
"(playlistID INTEGER not NULL, " +
" playlistName VARCHAR(255), " +
" musicToContinuePath VARCHAR(255), " +
" durationToContinue INTEGER)";
st.execute(createTableSQL);
*/
String sql = "INSERT INTO PLAYLISTS " + "VALUES (123456789, 'Zara', 'C:\\Music', 18)";
st.executeUpdate(sql);
ResultSet rs;
rs = st.executeQuery("select * from playlists");
while (rs.next()) {
System.out.println(rs.getString("playlistName"));
}
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Just a simple test class, I commented out the create table after the table is created. Everything works fine, the application prints out 'Zara' like it should, but I cannot find the table in the Intellij Database Tool.
[How I'm importing the database]
https://i.gyazo.com/e2c1360729d53f837ffe3195290db7fa.png
[Not showing anything in the DB]
https://gyazo.com/4ddc9cea065bd928471aff0805130f73
I know where it is storing the database, it's in my user folder/data, and I've tried putting the absolute path to it when connecting with intellij client. The connection test succeeds as well.

not getting data into mysql from java

I'm not getting data into mysql
void connectionDB() {
try {
Class.forName(fileReader.getdriver());
String url = "jdbc:mysql://"+ fileReader.gethost() + ":" + fileReader.getDBport() +"/" + fileReader.getdbname();
conn = DriverManager.getConnection(url, fileReader.getusername(),fileReader.getpasswd());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException se) {
se.printStackTrace();
}
}
public void snmp_mysql(String ipv6Address, String[] resString) {
try {
stat = conn.createStatement();
String sql =
("INSERT INTO Statistics3 VALUES ('" + ipv6Address + "','"
+ dateTime.trim() + "'," + battpercent + ")");
stat.executeUpdate(sql);
stat.close();
System.out.println("updating");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("SQL statement is not executed!");
}
}
The code is not showing any error and it is showing Empty set (0.01 sec) in MySQL. Previously it worked properly and got the output. I didn't make any changes. I do not know why its not working.
I have taken another class in another project and added some columns to the existing table Statistics3,and used mysql for that class.I didnt make any changes in this class.That doesn't effect mysql know
While i'm running the project,i'm getting these errors in the middle of the output
java.sql.SQLException: Column count doesn't match value count at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
You can explicitly specify the column into which you want to insert data.. As you can see, you can also skip some column..
String sql =
"INSERT INTO Statistics3 (column1, column2, column4) VALUES ('" + ipv6Address
+ "','" + dateTime.trim() + "'," + battpercent + ")";
column1, column2, and column4 are columns corresponding to your values - ipv6Address, datetime, and battpercent.. in your table..
So, if you have inserted a column - column3 , then you can just skip it.. It will get the default value as you have set..
added some columns to the existing table Statistics3
There is your problem. Number of values does not match with number of columns present.
You should use version of SQL statement
insert into table (col1,col2) values (val1,val2)
The values given in the insert query doesn't match with the columns in the DB. Make sure that you have values for all the columns(in your table) in your query.

Categories