TableAu TDE文件创建与上传

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiewendong93/article/details/51492382

TableAu官方案例,环境设置等

http://onlinehelp.tableau.com/current/api/sdk/zh-cn/help.htm#SDK/tableau_sdk_installing.htm%3FTocPath%3D_____3

功能实现

  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;
    }
}

猜你喜欢

转载自blog.csdn.net/xiewendong93/article/details/51492382