java使用Extract API 2.0生成.hyper檔案
阿新 • • 發佈:2019-01-10
突然接到一個任務,需要用java使用Extract API 2.0實現抽取資料庫的數並生成.hyper檔案。沒辦法,只能一邊看文件一邊寫個demo先試試看能不能行得通。
文件:https://onlinehelp.tableau.com/current/api/extract_api/en-us/Extract/extract_api.htm
Extract API 2.0安裝包下載:https://onlinehelp.tableau.com/current/api/extract_api/en-us/Extract/extract_api_installing.htm#downloading
需要注意的是java使用Extract API 2.0需要把下載的Extract API 2.0包裡的dll檔案放到jdk的bin資料夾裡,並把相關jar包匯入到專案當中去。
由於是測試demo,所需我隨便用了一個mysql的資料庫,看是否能把資料抽取到.hyper檔案裡去。
下面是程式碼:
package com.app.dataController; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.HashMap; import com.mysql.jdbc.Connection; import com.tableausoftware.TableauException; import com.tableausoftware.common.Collation; import com.tableausoftware.common.Type; import com.tableausoftware.hyperextract.Extract; import com.tableausoftware.hyperextract.ExtractAPI; import com.tableausoftware.hyperextract.Row; import com.tableausoftware.hyperextract.Table; import com.tableausoftware.hyperextract.TableDefinition; public class ConnectSource { public static ResultSet consource() { try { Class.forName("com.mysql.jdbc.Driver"); //載入MYSQL JDBC驅動程式 System.out.println("Success loading Mysql Driver!"); } catch (Exception e) { System.out.print("Error loading Mysql Driver!"); e.printStackTrace(); } try { java.sql.Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/talend","root","123456"); //連線URL為 jdbc:mysql//伺服器地址/資料庫名 ,後面的2個引數分別是登陸使用者名稱和密碼 System.out.println("Success connect Mysql server!"); Statement stmt = connect.createStatement(); ResultSet rs = stmt.executeQuery("select * from user"); return rs; } catch (Exception e) { System.out.print("get data error!"); e.printStackTrace(); } return null; } public static void main(String args[]) { HashMap<String, String> options = new HashMap<>(); options.put("filename", "C:\\Users\\Silence\\Desktop\\Silence.hyper"); options.put("build", "1"); if ( options.containsKey( "build" ) ) { try { // Initialize the Tableau Extract API ExtractAPI.initialize(); Extract extract = createOrOpenExtract( options.get( "filename" ), true ,true); populateExtract( extract, options.containsKey( "spatial" ) ); // Flush the Extract to Disk extract.close(); // Close the Tableau Extract API ExtractAPI.cleanup(); }catch ( TableauException e ) { System.err.println( "A fatal error occurred while opening or closing the Extract API:" ); System.err.println( e.getMessage() ); System.err.println( "Printing stack trace now:" ); e.printStackTrace( System.err ); System.err.println( "Exiting now." ); System.exit( -1 ); } catch ( Throwable t ) { System.err.println( "An unknown error occured while opening or closing the Extract API:" ); System.err.println( "Printing stack trace now:" ); t.printStackTrace( System.err ); System.err.println( "Exiting now." ); System.exit( -1 ); } } } private static Extract createOrOpenExtract(String filename,boolean useSpatial,boolean createMultipleTables) { Extract user = null; Table table = null; try { // Create Extract Object // (NOTE: TabExtractCreate() opens an existing extract with the given // filename if one exists or creates a new extract with the given filename // if one does not) user = new Extract( filename ); // Define Table Schema (If we are creating a new extract) // (NOTE: in Tableau Data Engine, all tables must be named "Extract") if ( !user.hasTable( "Extract" ) ) { TableDefinition schema = new TableDefinition(); schema.setDefaultCollation( Collation.EN_GB ); schema.addColumn( "ID",Type.INTEGER ); schema.addColumn( "Name",Type.CHAR_STRING ); schema.addColumn( "CountryCode",Type.CHAR_STRING ); schema.addColumn( "District",Type.CHAR_STRING ); schema.addColumn( "Population",Type.CHAR_STRING ); table = user.addTable( "Extract", schema ); if ( table == null ) { System.err.println( "A fatal error occured while creating the table" ); System.err.println( "Exiting now." ); System.exit( -1 ); } } } catch ( TableauException e ) { System.err.println( "A fatal error occurred while creating the extract:" ); System.err.println( e.getMessage() ); System.err.println( "Printing stack trace now:" ); e.printStackTrace( System.err ); System.err.println( "Exiting now." ); System.exit( -1 ); } catch ( Throwable t ) { System.err.println( "An unknown error occured while creating the extract" ); System.err.println( "Printing stack trace now:" ); t.printStackTrace( System.err ); System.err.println( "Exiting now." ); System.exit( -1 ); } return user; } private static void populateExtract(Extract user,boolean useSpatial) { try { // Get Schema Table table = user.openTable( "Extract" ); TableDefinition tableDef = table.getTableDefinition(); ResultSet rs = consource(); // Insert Data Row row = new Row( tableDef ); while (rs.next()) { row.setInteger(0, rs.getInt("ID")); // ID row.setCharString( 1, rs.getString("Name")); // Name row.setCharString( 2, rs.getString("CountryCode")); // CountryCode row.setCharString( 3, rs.getString("District")); // District row.setCharString( 4, rs.getString("Population")); // Population table.insert( row ); } } catch ( TableauException e ) { System.err.println( "A fatal error occurred while populating the extract:" ); System.err.println( e.getMessage() ); System.err.println( "Printing stack trace now:" ); e.printStackTrace( System.err ); System.err.println( "Exiting now." ); System.exit( -1 ); } catch ( Throwable t ) { System.err.println( "An unknown error occured while populating the extract" ); System.err.println( "Printing stack trace now:" ); t.printStackTrace( System.err ); System.err.println( "Exiting now." ); System.exit( -1 ); } } }
環境配置好之後可以直接執行main方法即可生成.hyper檔案。
生成的檔案用Tableau開啟效果:
接下來就該開始用java連線RedShift庫,把億級的資料通過多執行緒的方式生成.hyper檔案啦。