1. 程式人生 > >TableAu TDE檔案建立與上傳

TableAu TDE檔案建立與上傳

TableAu官方案例,環境設定等

功能實現

  1. 將資料庫的查詢結果生成TDE檔案(TableAu資料來源)
  2. 並上傳至伺服器
  3. 可實時替換TableAu Service儀表盤中的資料.

環境設定

需要安裝TableAu SDK,否則無法生成TDE檔案,異常為:找不到模組
根據系統版本下載SDK,配置環境變數
詳情參考上面的連線!

TableAu Jar

#TableAu 需要的Jar包,連線上有下載地址.
jna.jar
tableaucommon.jar
tableauextract.jar
tableauserver.jar

全部程式碼(供參考)

package com.tenly.common.sql.util;

/**
 * -----------------------------------------------------------------------------
 * 
 * 該類用與匯出TDE檔案(TableAu的離線資料格式)
 * 
 * -----------------------------------------------------------------------------
 */
import java.io.File;
import java.sql.Connection;
import
java.sql.PreparedStatement; import java.sql.ResultSet; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; import com.tableausoftware.TableauException; import com.tableausoftware.common.Collation; import com.tableausoftware.common.Result; import
com.tableausoftware.common.Type; import com.tableausoftware.extract.Extract; import com.tableausoftware.extract.ExtractAPI; import com.tableausoftware.extract.Row; import com.tableausoftware.extract.Table; import com.tableausoftware.extract.TableDefinition; import com.tableausoftware.server.ServerAPI; import com.tableausoftware.server.ServerConnection; public final class CreateTableAuFile { private static Map<String, Type> typeMap=getTypeMap(); private static SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); private static Calendar cal = Calendar.getInstance(); /** *------------------------------------------------------------------------ *該類用於建立TDE檔案中的列 *----------------------------------------------------------------------- * @throws Exception */ private static TableDefinition makeTableDefinition(String tableName) throws Exception { Connection conn=null; TableDefinition tableDef = new TableDefinition(); try{ conn= HiveExcutor.getConn(); ResultSet set= conn.getMetaData().getColumns(null, null, tableName, null); if(set!=null){ tableDef.setDefaultCollation(Collation.EN_GB); while(set.next()){ tableDef.addColumn(set.getString("COLUMN_NAME"), typeMap.get(set.getString("TYPE_NAME"))); } } }finally{ if(conn!=null){ conn.close(); conn=null; } } return tableDef; } /** *------------------------------------------------------------------------ *插入資料 *----------------------------------------------------------------------- * @throws Exception */ private static void insertData(Table table,String tableName) throws Exception { Connection conn= null; PreparedStatement ps =null; try{ conn= HiveExcutor.getConn(); TableDefinition tableDef = table.getTableDefinition(); Row row = new Row(tableDef); int countColumn=tableDef.getColumnCount(); String column=""; if(countColumn==0) return; for (int i = 0; i < countColumn; i++) { column=column+tableDef.getColumnName(i)+","; } column=column.substring(0, column.length()-1); String sql="select "+column+" from "+tableName; ps = conn.prepareStatement(sql); ResultSet resultSet = ps.executeQuery(); while(resultSet.next()){ for (int i = 0; i < countColumn; i++) { if("INTEGER".equals(tableDef.getColumnType(i).toString())){ row.setInteger(i, resultSet.getInt(i+1)); }else if("DOUBLE".equals(tableDef.getColumnType(i).toString())){ row.setDouble(i, resultSet.getDouble(i+1)); }else if("BOOLEAN".equals(tableDef.getColumnType(i).toString())){ row.setBoolean(i, resultSet.getBoolean(i+1)); }else if("UNICODE_STRING".equals(tableDef.getColumnType(i).toString())){ row.setString(i, resultSet.getString(i+1)); }else if("DATE".equals(tableDef.getColumnType(i).toString())){ cal.setTime(resultSet.getDate(i+1)); row.setDate(i, cal.get(cal.YEAR),cal.get(cal.MONTH)+1,cal.get(cal.DAY_OF_MONTH)); } } table.insert(row); } }finally{ if( ps!=null){ ps.close(); ps=null; } if(conn!=null) { conn.close(); conn=null; } } } /** *------------------------------------------------------------------------ * 生成檔案的主方法 * 檔案儲存路徑在:<專案路徑>\WebRoot\TableAuFile * 檔名格式: 2016517TableName.tde *----------------------------------------------------------------------- * @throws Exception */ public void createFile(String tableName) throws Exception{ String sysName=System.getProperty("os.name").toUpperCase(); String relativelyPath=System.getProperty("user.dir"); File file=null; String url=null; String fileName=format.format(new Date())+"-"+tableName+".tde"; if(sysName.contains("WINDOW")){ relativelyPath=relativelyPath.substring(0,relativelyPath.lastIndexOf("\\bin")); url=relativelyPath+"\\webapps\\gtdss\\WEB-INF\\classes\\TableAuFile"; file=new File(url+"\\"+fileName); if(file.exists()){ file.delete(); } /**Extract為必須的,不可設定其他替代字元*/ try ( Extract extract = new Extract(url+"\\"+fileName)) { Table table; if (!extract.hasTable("Extract")) { TableDefinition tableDef = makeTableDefinition(tableName); table = extract.addTable("Extract", tableDef); }else{ table = extract.openTable("Extract"); } insertData(table,tableName); } }else{ //usr/local/tomcat/bin System.err.println(relativelyPath); System.out.println("/bin"); System.err.println(relativelyPath.lastIndexOf("/bin")); relativelyPath=relativelyPath.substring(0,relativelyPath.lastIndexOf("/bin")); url=relativelyPath+"/webapps/gtdss/WEB-INF/classes/TableAuFile"; file=new File(url+"/"+fileName); if(file.exists()){ file.delete(); } /**Extract為必須的,不可設定其他替代字元*/ try ( Extract extract = new Extract(url+"/"+fileName)) { Table table; if (!extract.hasTable("Extract")) { TableDefinition tableDef = makeTableDefinition(tableName); table = extract.addTable("Extract", tableDef); }else{ table = extract.openTable("Extract"); } insertData(table,tableName); } } ExtractAPI.initialize(); ExtractAPI.cleanup(); } /** *------------------------------------------------------------------------ *檔案上傳伺服器 *tableAu service的連線資訊 *path:172.*.*.*:8000 userName:**** password:**** *----------------------------------------------------------------------- */ public static void uploadFile(String theme,String tableName,String path, String userName, String password){ ServerConnection serverConnection =null; try { ServerAPI.initialize(); serverConnection = new ServerConnection(); serverConnection.connect( path, userName, password, ""); String sysName=System.getProperty("os.name").toUpperCase(); String fileName=format.format(new Date())+"-"+tableName; String url=null; String relativelyPath=null; if(sysName.contains("WINDOW")){ relativelyPath=System.getProperty("user.dir"); relativelyPath=relativelyPath.substring(0,relativelyPath.lastIndexOf("\\bin")); url=relativelyPath+"\\webapps\\gtdss\\WEB-INF\\classes\\TableAuFile"; String filePath=url+"\\"+fileName+".tde"; System.out.println("系統版本"+sysName+"-地址:"+filePath); /**檔案地址,default分組,名稱,是否覆蓋*/ serverConnection.publishExtract(filePath, "default",theme, true); }else{ relativelyPath=System.getProperty("user.dir"); relativelyPath=relativelyPath.substring(0,relativelyPath.lastIndexOf("/bin")); url=relativelyPath+"/webapps/gtdss/WEB-INF/classes/TableAuFile"; String filePath=url+"/"+fileName+".tde"; System.out.println("系統版本"+sysName+"-地址:"+filePath); /**檔案地址,default分組,名稱,是否覆蓋*/ serverConnection.publishExtract(filePath, "default",theme, true); } } catch (TableauException e) { switch(Result.enumForValue(e.getErrorCode())) { case INTERNAL_ERROR: System.err.println("INTERNAL_ERROR - Could not parse the response from the server."); break; case INVALID_ARGUMENT: System.err.println("INVALID_ARGUMENT - " + e.getMessage()); break; case CURL_ERROR: System.err.println("CURL_ERROR - " + e.getMessage()); break; case SERVER_ERROR: System.err.println("SERVER_ERROR - " + e.getMessage()); break; case NOT_AUTHENTICATED: System.err.println("NOT_AUTHENTICATED - " + e.getMessage()); break; case BAD_PAYLOAD: System.err.println("BAD_PAYLOAD - Unknown response from the server. Make sure this version of Tableau API is compatible with your server."); break; case INIT_ERROR: System.err.println("INIT_ERROR - " + e.getMessage()); break; case UNKNOWN_ERROR: default: System.err.println("An unknown error occured."); break; } e.printStackTrace(System.err); }finally{ try { serverConnection.disconnect(); serverConnection.close(); ServerAPI.cleanup(); } catch (TableauException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** *------------------------------------------------------------------------ * TableAU中所有型別:INTEGER,DOUBLE,BOOLEAN,DATE,DATETIME,DURATION,CHAR_STRING,UNICODE_STRING * 對應Hive中的所有型別 *----------------------------------------------------------------------- */ public static Map<String, Type> getTypeMap(){ Map<String, Type> map=new HashMap<String,Type>(); map.put("TINYINT",Type.INTEGER); map.put("SMALLINT", Type.INTEGER); map.put("BIGINT", Type.UNICODE_STRING); map.put("BOOLEAN", Type.BOOLEAN); map.put("FLOAT", Type.DOUBLE); map.put("DOUBLE", Type.DOUBLE); map.put("STRING", Type.UNICODE_STRING); map.put("BINARY", Type.UNICODE_STRING); map.put("TIMESTAMP", Type.UNICODE_STRING); map.put("DECIMAL", Type.UNICODE_STRING); map.put("DATE", Type.DATE); map.put("CHAR", Type.UNICODE_STRING); map.put("VARCHAR", Type.UNICODE_STRING); return map; } }