Hive table sizes using Java/JDBC - java

Is it possible to find the sizes in GB of the hive tables using Java/jdbc? I don't want to depend on the hive warehouse folder in HDFS (as described in link) as different tables may have different locations

If you are mention 'totalSize' from 'tblproperties' then it is possible with similar approach:
String driverName = "org.apache.hive.jdbc.HiveDriver";
String connectionURL = "jdbc:hive2://HOSTNAME:PORT/default";
try {
Class.forName(driverName);
Connection connection = DriverManager.getConnection( connectionURL, "", "");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Statement stmt = connection.createStatement("show tblproperties TABLENAME");
ResultSet rs = stmt.executeQuery(stmt);
while(rs.next()){
//doWhatYouWant
}

Currently not be possible but it can be done .
To get file size you have to run on file system( HDFS) commands .
In case of RDMS data bases ie sql server have encapsulated file system commands in SYS views and SYS functions (DMF and DMVs) .
If some one write or develop such UDF it will be possible but Internally the UDF will be calling same comand.

Related

Loading SQLite db to memory in Java using JDBC?

Usually a SQLite db is read from a file. Each operation can require a file I/O operation if the db is large enough, and this can be slow.
However SQLite provides a way to load the db to memory - where it can be accessed using a JDBC url which looks something likejdbc:memory...
For example, this question describes how to achieve this using python: How to load existing db file to memory in Python sqlite3?
However I can't figure out how to achieve the same thing in Java or Scala using JDBC.
Any ideas?
I just tried with Xerial's sqlite-jdbc-3.27.2.1.jar and it appears that they let us restore from a native (binary) SQLite database file into a :memory: database like so:
try (Connection conn = DriverManager.getConnection("jdbc:sqlite::memory:")) {
try (Statement st = conn.createStatement()) {
st.execute("restore from C:\\__tmp\\thing.sqlite");
// let's see if we can read one of the tables
ResultSet rs = st.executeQuery("SELECT * FROM Table1");
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
} catch (Throwable e) {
e.printStackTrace(System.err);
}

accessing excel as database with jdbc

I have to design an application for accessing excel application as database. My problem is I have to create multiple connection for each transaction and if I miss any of closing it, the excel is not being update.
I want to design pattern where i am able to access the excel. Any one help me in designing a common pattern through which i wont be having problem. I want something like this, but we are not able to use it to access excel.
Thanks in advance!
i have this method in utility class
static ResultSet getExcelData(String filePath,String sqlQuery){
ResultSet rs=null;
try{
conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ="+filePath+";READONLY=false");
stmt= conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery( sqlQuery );
}catch (Exception e) {
e.printStackTrace();
return null;
// TODO: handle exception
}finally{
}
return rs;
}
and i am calling it this way
ResultSet rs=JdbcUtil.getExcelData("D:\\AB_demo\\AB_demo\\test.xls", "Select max(int(ID)) from [MAIN$] where HEADER_IND is not Null AND int(ID)<"+excelId);
int databaseId = 0;
if(rs.next())
{
databaseId=rs.getInt(1);
}
ResultSet rs1=JdbcUtil.getExcelData("D:\\AB_demo\\AB_demo\\test.xls", "SELECT * from [MAIN$] where id= '"+databaseId+"'or id='"+excelId+"'");
i am calling this method twice after which im updating the excel file by using
stmt.executeUpdate(sql);
its returning the integer 1 but its not reflecting in excel.when i use process explorer the file is still in use.i need a design pattern or come code to overcome his kind of problem.
I think more right way is generate Excel file from database. Otherwise you must create server side for ensure transactions and connections control.
Main problem of your task - Excel is
not database
not network database
Other words you must use other tools, other approach for your tasks.

Platform independent Connection String for EXCEL in java

Is there a PLATFORM INDEPENDENT connection string for EXCEL file in java. jdbc:odbc is platform dependent. Is there anything else ??
In this post you could see an example of using a connection string without the ODBC. I suppose this is what you are looking for...
Class.forName("com.hxtt.sql.excel.ExcelDriver").newInstance();
String url = "jdbc:Excel:///E:/JavaWithExcel/Feedback.xlsx";
String sql = "select * from [Sheet1]";
Connection con = DriverManager.getConnection(url, "", "");
Statement stmt = con.createStatement();
System.out.println(con);
System.out.println(stmt);
stmt.close();
con.close();
Anyway, you have to understand that the connection string is always specific to the underlying environment. It could depend on the operating system, on the file system or something else... I would just use different configuration files for each environment.
Hope I helped!

Getting Unicode Data from MS Access output will be " ???????????????????"

I am using MS Access & MySQL ,in access input this word
کوردستان ی عیراق (it's kurdish language using unicode )
my code is :
try{
String path ="src\\Database.accdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection c = DriverManager.getConnection(""
+ "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ="+path);
Statement s = c.createStatement();
ResultSet rs = s.executeQuery("select * from mytable");
rs.next();
jTextArea1.setText(rs.getString(1));
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
with access the output is ??????????????
but with MYSQL and the output is کوردستان ی عیراق
Why ??
thanks
If you are just trying to get data from an MS Access db and you don't need to run complex queries, you might want to check out the Jackcess project, which is a native, cross-platform Java API for opening MS Access files. it doesn't currently have support for running SQL queries, but it does give you access to all the data without going through the (flaky) jdbc-odbc bridge. it also has support for looking up data using indexes (via an IndexCursor).
(disclaimer, i am the primary author).
You should set a appropriate charset for the properties when you try to establish the connection, e.g.:
java.util.Properties prop = new java.util.Properties();
prop.put("charSet", "UTF8"); // Not tested..
Connection c = DriverManager.getConnection(
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ="+path, prop);
Look into the documentation for the JDBC-ODBC Bridge for further details
From microsoft forums:
If your connection character set is utf8, then you should run this query right
after connecting to DB:
SET NAMES 'utf8';
Also your DB and tables and columns should have utf8_general_ci or other type of
utf8 collation.
Hope this helps

How to access a java Derby database? I could really do with a few helpful pointers

I am new to using Derby and databses in eclipse, and I have become a tad lost and in need to a bit of help. I have established a database connection, created a new database, and a new schema, within which I have some tables containing some test data. I don't have any problem with the sql queries to select the relevant data. The problem I have is getting to a point where I can use queries. I am trying to create a class which connects to the database, and for testing purposes, uses a simple query to select some data. This is what I have so far:
public void getExerciseInfo() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
connect = DriverManager.getConnection("jdbc:derby://localhost/c:/TestDatabase");
PreparedStatement statement = connect.prepareStatement("SELECT * from TESTSCHEMA.TESTTABLE");
resultSet = statement.executeQuery();
while (resultSet.next()) {
String name= resultSet.getString("NAME");
String type = resultSet.getString("TYPE");
System.out.println(name);
System.out.println(type);
}
} catch (Exception e) {
} finally {
close();
}
}
All I am trying to do is output the data in the table to the console, but I cant even do this simple task :( Im guessing my connection url is invalid, is it supposed to be the file path to the database folder in my eclipse workspace?
Anyhow, I am very lost, and any help would be greatly appreciated.
Did you take a look over: http://db.apache.org/derby/integrate/plugin_help/derby_app.html ? You seem to be using the network server but your db URL is wrong.
If you are not running the Derby server, you can establish an embedded database connection or use an EmbeddedDataSource, shown here.

Categories

Resources