Search in sources :

Example 1 with BlockScriptBuffer

use of com.qlangtech.tis.sql.parser.visitor.BlockScriptBuffer in project plugins by qlangtech.

the class BasicDorisStarRocksWriter method generateCreateDDL.

@Override
public StringBuffer generateCreateDDL(IDataxProcessor.TableMap tableMapper) {
    if (!this.autoCreateTable) {
        return null;
    }
    // https://doris.apache.org/master/zh-CN/sql-reference/sql-statements/Data%20Definition/CREATE%20TABLE.html#create-table
    final CreateTableSqlBuilder createTableSqlBuilder = new CreateTableSqlBuilder(tableMapper) {

        @Override
        protected void appendExtraColDef(List<ColWrapper> pks) {
        // if (pk != null) {
        // script.append("  PRIMARY KEY (`").append(pk.getName()).append("`)").append("\n");
        // }
        }

        @Override
        protected List<ColWrapper> preProcessCols(List<ColWrapper> pks, List<ISelectedTab.ColMeta> cols) {
            // 将主键排在最前面
            List<ColWrapper> result = Lists.newArrayList(pks);
            cols.stream().filter((c) -> !c.isPk()).forEach((c) -> {
                result.add(createColWrapper(c));
            });
            return result;
        }

        @Override
        protected void appendTabMeta(List<ColWrapper> pks) {
            script.append(" ENGINE=olap").append("\n");
            if (pks.size() > 0) {
                script.append("PRIMARY KEY(").append(pks.stream().map((pk) -> this.colEscapeChar() + pk.getName() + this.colEscapeChar()).collect(Collectors.joining(","))).append(")\n");
            }
            script.append("DISTRIBUTED BY HASH(");
            if (pks.size() > 0) {
                script.append(pks.stream().map((pk) -> this.colEscapeChar() + pk.getName() + this.colEscapeChar()).collect(Collectors.joining(",")));
            } else {
                List<ISelectedTab.ColMeta> cols = this.getCols();
                Optional<ISelectedTab.ColMeta> firstCol = cols.stream().findFirst();
                if (firstCol.isPresent()) {
                    script.append(firstCol.get().getName());
                } else {
                    throw new IllegalStateException("can not find table:" + getCreateTableName() + " any cols");
                }
            }
            script.append(")\n");
            script.append("BUCKETS 10\n");
            script.append("PROPERTIES(\"replication_num\" = \"1\")");
        }

        @Override
        protected ColWrapper createColWrapper(ISelectedTab.ColMeta c) {
            return new ColWrapper(c) {

                @Override
                public String getMapperType() {
                    return convertType(this.meta);
                }

                @Override
                protected void appendExtraConstraint(BlockScriptBuffer ddlScript) {
                    if (this.meta.isPk()) {
                        ddlScript.append(" NOT NULL");
                    }
                }
            };
        }

        protected String convertType(ISelectedTab.ColMeta col) {
            DataType type = col.getType();
            return type.accept(new DataType.TypeVisitor<String>() {

                @Override
                public String longType(DataType type) {
                    return "BIGINT";
                }

                @Override
                public String doubleType(DataType type) {
                    return "DOUBLE";
                }

                @Override
                public String dateType(DataType type) {
                    return "DATE";
                }

                @Override
                public String timestampType(DataType type) {
                    return "DATETIME";
                }

                @Override
                public String bitType(DataType type) {
                    return "TINYINT";
                }

                @Override
                public String blobType(DataType type) {
                    return varcharType(type);
                }

                @Override
                public String varcharType(DataType type) {
                    return "VARCHAR(" + Math.min(type.columnSize, 65000) + ")";
                }

                @Override
                public String intType(DataType type) {
                    return "INT";
                }

                @Override
                public String floatType(DataType type) {
                    return "FLOAT";
                }

                @Override
                public String decimalType(DataType type) {
                    return "DECIMAL(" + type.columnSize + "," + (type.getDecimalDigits() != null ? type.getDecimalDigits() : 0) + ")";
                }
            });
        }
    };
    return createTableSqlBuilder.build();
}
Also used : Validator(com.qlangtech.tis.plugin.annotation.Validator) StringUtils(org.apache.commons.lang.StringUtils) InitWriterTable(com.qlangtech.tis.plugin.datax.common.InitWriterTable) BlockScriptBuffer(com.qlangtech.tis.sql.parser.visitor.BlockScriptBuffer) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) IDataxContext(com.qlangtech.tis.datax.IDataxContext) FormField(com.qlangtech.tis.plugin.annotation.FormField) Collectors(java.util.stream.Collectors) Context(com.alibaba.citrus.turbine.Context) IFieldErrorHandler(com.qlangtech.tis.runtime.module.misc.IFieldErrorHandler) List(java.util.List) JSON(com.alibaba.fastjson.JSON) Lists(com.google.common.collect.Lists) IOUtils(com.qlangtech.tis.extension.impl.IOUtils) BasicDataXRdbmsWriter(com.qlangtech.tis.plugin.datax.common.BasicDataXRdbmsWriter) DataType(com.qlangtech.tis.plugin.ds.DataType) Optional(java.util.Optional) JSONObject(com.alibaba.fastjson.JSONObject) IDataxProcessor(com.qlangtech.tis.datax.IDataxProcessor) FormFieldType(com.qlangtech.tis.plugin.annotation.FormFieldType) DorisSourceFactory(com.qlangtech.tis.plugin.ds.doris.DorisSourceFactory) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) DataType(com.qlangtech.tis.plugin.ds.DataType) List(java.util.List) BlockScriptBuffer(com.qlangtech.tis.sql.parser.visitor.BlockScriptBuffer)

Aggregations

Context (com.alibaba.citrus.turbine.Context)1 JSON (com.alibaba.fastjson.JSON)1 JSONObject (com.alibaba.fastjson.JSONObject)1 Lists (com.google.common.collect.Lists)1 IDataxContext (com.qlangtech.tis.datax.IDataxContext)1 IDataxProcessor (com.qlangtech.tis.datax.IDataxProcessor)1 IOUtils (com.qlangtech.tis.extension.impl.IOUtils)1 FormField (com.qlangtech.tis.plugin.annotation.FormField)1 FormFieldType (com.qlangtech.tis.plugin.annotation.FormFieldType)1 Validator (com.qlangtech.tis.plugin.annotation.Validator)1 BasicDataXRdbmsWriter (com.qlangtech.tis.plugin.datax.common.BasicDataXRdbmsWriter)1 InitWriterTable (com.qlangtech.tis.plugin.datax.common.InitWriterTable)1 DataType (com.qlangtech.tis.plugin.ds.DataType)1 ISelectedTab (com.qlangtech.tis.plugin.ds.ISelectedTab)1 DorisSourceFactory (com.qlangtech.tis.plugin.ds.doris.DorisSourceFactory)1 IFieldErrorHandler (com.qlangtech.tis.runtime.module.misc.IFieldErrorHandler)1 BlockScriptBuffer (com.qlangtech.tis.sql.parser.visitor.BlockScriptBuffer)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1