Пример #1
0
        public autoFitTableAppender(string dbUrl, string tableName, bool asynTask, DBConnection conn)
        {
            this.dbUrl     = dbUrl;
            this.tableName = tableName;
            this.asynTask  = asynTask;
            this.conn      = conn;
            conn.setasynTask(false);
            BasicDictionary tableInfo = (BasicDictionary)conn.run("schema(loadTable(\"" + dbUrl + "\", \"" + tableName + "\"))");
            BasicTable      colDefs;

            colDefs = ((BasicTable)tableInfo.get(new BasicString("colDefs")));
            BasicStringVector names = (BasicStringVector)colDefs.getColumn("name");
            BasicIntVector    types = (BasicIntVector)colDefs.getColumn("typeInt");
            int rows = names.rows();


            this.schema = new Dictionary <string, DATA_TYPE>();
            for (int i = 0; i < rows; ++i)
            {
                schema.Add(names.getString(i), (DATA_TYPE)types.getInt(i));
            }
            conn.setasynTask(asynTask);
        }
        /**
         * If fail to connect to the specified DolphinDB server, this function throw an exception.
         */
        public MultithreadedTableWriter(string hostName, int port, string userId, string password,
                                        string dbName, string tableName, bool useSSL, bool enableHighAvailability = false, string[] pHighAvailabilitySites = null,
                                        int batchSize = 1, float throttle = 0.01f, int threadCount = 5, string partitionCol = "", int[] pCompressMethods = null)
        {
            hostName_          = hostName;
            port_              = port;
            userId_            = userId;
            password_          = password;
            useSSL_            = useSSL;
            dbName_            = dbName;
            tableName_         = tableName;
            batchSize_         = batchSize;
            throttleMilsecond_ = (int)throttle * 1000;
            isExiting_         = false;
            if (threadCount < 1)
            {
                throw new Exception("The parameter threadCount must be greater than or equal to 1.");
            }
            if (batchSize < 1)
            {
                throw new Exception("The parameter batchSize must be greater than or equal to 1.");
            }
            if (throttle < 0)
            {
                throw new Exception("The parameter throttle must be positive.");
            }
            if (threadCount > 1 && partitionCol == String.Empty)
            {
                throw new Exception("The parameter partitionCol must be specified when threadCount is greater than 1.");
            }
            DBConnection pConn = new DBConnection(false, useSSL_, pCompressMethods != null);
            bool         ret   = pConn.connect(hostName_, port_, userId_, password_, "", enableHighAvailability, pHighAvailabilitySites);

            if (!ret)
            {
                throw new Exception(string.Format("Failed to connect to server {0}:{1}. ", hostName, port));
            }
            BasicDictionary schema;

            if (tableName == "")
            {
                schema = (BasicDictionary)pConn.run("schema(" + dbName + ")");
            }
            else
            {
                schema = (BasicDictionary)pConn.run("schema(loadTable(\"" + dbName + "\",\"" + tableName + "\"))");
            }
            IEntity partColNames = null;

            if (schema.ContainsKey("partitionColumnName"))
            {
                partColNames      = schema.get(new BasicString("partitionColumnName"));
                isPartionedTable_ = true;
            }
            else
            {
                isPartionedTable_ = false;
                if (tableName != "")
                {
                    if (threadCount > 1)
                    {//只有多线程的时候需要
                        throw new Exception("The parameter threadCount must be 1 for a dimension table.");
                    }
                }
            }
            BasicTable colDefs = (BasicTable)schema.get("colDefs");

            BasicIntVector colDefsTypeInt = (BasicIntVector)colDefs.getColumn("typeInt");

            BasicStringVector colDefsName       = (BasicStringVector)colDefs.getColumn("name");
            BasicStringVector colDefsTypeString = (BasicStringVector)colDefs.getColumn("typeString");

            colTypes_      = new List <DATA_TYPE>();
            colNames_      = new List <string>();
            colTypeString_ = new List <string>();
            int columnSize = colDefsName.rows();

            if (pCompressMethods != null)
            {
                if (columnSize != pCompressMethods.Length)
                {
                    throw new Exception(string.Format("The number of elements in parameter compressMethods does not match the column size {0}. ", columnSize));
                }
                this.compressTypes_ = new int[columnSize];
                Array.Copy(pCompressMethods, this.compressTypes_, columnSize);
            }
            for (int i = 0; i < columnSize; i++)
            {
                colNames_.Add(colDefsName.getString(i));
                colTypes_.Add((DATA_TYPE)colDefsTypeInt.getInt(i));
                colTypeString_.Add(colDefsTypeString.getString(i));
                if (compressTypes_ != null)
                {
                    AbstractVector.checkCompressedMethod(colTypes_[i], compressTypes_[i]);
                }
            }

            if (threadCount > 1)
            {
                if (isPartionedTable_)
                {
                    IEntity partitionSchema;
                    int     partitionType;
                    if (partColNames.isScalar())
                    {
                        if (partColNames.getString() != partitionCol)
                        {
                            throw new Exception(string.Format("The parameter partionCol must be the partitioning column \"{0}\" in the table. ", partitionCol));
                        }
                        partitionColumnIdx_ = ((BasicInt)schema.get("partitionColumnIndex")).getInt();
                        partitionSchema     = schema.get("partitionSchema");
                        partitionType       = ((BasicInt)schema.get("partitionType")).getInt();
                    }
                    else
                    {
                        int dims = ((BasicStringVector)partColNames).rows();
                        if (dims > 1 && partitionCol == "")
                        {
                            throw new Exception("The parameter partitionCol must be specified when threadCount is greater than 1.");
                        }
                        int index = -1;
                        for (int i = 0; i < dims; ++i)
                        {
                            if (((BasicStringVector)partColNames).getString(i) == partitionCol)
                            {
                                index = i;
                                break;
                            }
                        }
                        if (index < 0)
                        {
                            throw new Exception(string.Format("The parameter partionCol must be the partitioning column \"{0}\" in the table. ", partitionCol));
                        }
                        partitionColumnIdx_ = ((BasicIntVector)schema.get("partitionColumnIndex")).getInt(index);
                        partitionSchema     = ((BasicAnyVector)schema.get("partitionSchema")).get(index);
                        partitionType       = ((BasicIntVector)schema.get("partitionType")).getInt(index);
                    }
                    DATA_TYPE partitionColType = colTypes_[partitionColumnIdx_];
                    partitionDomain_ = DomainFactory.createDomain((PARTITION_TYPE)partitionType, partitionColType, partitionSchema);
                }
                else
                {//isPartionedTable_==false
                    if (partitionCol != "")
                    {
                        int threadcolindex = -1;
                        for (int i = 0; i < colNames_.Count; i++)
                        {
                            if (colNames_[i] == partitionCol)
                            {
                                threadcolindex = i;
                                break;
                            }
                        }
                        if (threadcolindex < 0)
                        {
                            throw new Exception(string.Format("No match found for {0}. ", partitionCol));
                        }
                        threadByColIndexForNonPartion_ = threadcolindex;
                    }
                }
            }

            // init done, start thread now.
            isExiting_ = false;
            threads_   = new List <WriterThread>(threadCount);
            for (int i = 0; i < threadCount; i++)
            {
                WriterThread writerThread = new WriterThread(this, pConn);
                if (i == 0)
                {
                    writerThread.conn_ = pConn;
                }
                else
                {
                    writerThread.conn_ = new DBConnection(useSSL_, false);
                    if (writerThread.conn_.connect(hostName_, port_, userId_, password_, "", enableHighAvailability, pHighAvailabilitySites) == false)
                    {
                        throw new Exception(string.Format("Failed to connect to server {0}:{1}. ", hostName, port));
                    }
                }
                threads_.Add(writerThread);
            }
        }