Search in sources :

Example 1 with DataBridgeModelSource

use of com.alibaba.alink.common.model.DataBridgeModelSource in project Alink by alibaba.

the class ModelMapStreamOp method linkFrom.

@Override
public T linkFrom(StreamOperator<?>... inputs) {
    checkMinOpSize(1, inputs);
    StreamOperator<?> in = inputs[0];
    TableSchema modelSchema = this.model.getSchema();
    try {
        DataBridge modelDataBridge = DirectReader.collect(model);
        final DataBridgeModelSource modelSource = new DataBridgeModelSource(modelDataBridge);
        final ModelMapper mapper = this.mapperBuilder.apply(modelSchema, in.getSchema(), this.getParams());
        DataStream<Row> resultRows;
        DataStream<Row> modelStream = null;
        TableSchema modelStreamSchema = null;
        if (ModelStreamUtils.useModelStreamFile(getParams())) {
            StreamOperator<?> modelStreamOp = new ModelStreamFileSourceStreamOp().setFilePath(getModelStreamFilePath()).setScanInterval(getModelStreamScanInterval()).setStartTime(getModelStreamStartTime()).setSchemaStr(CsvUtil.schema2SchemaStr(modelSchema)).setMLEnvironmentId(getMLEnvironmentId());
            modelStreamSchema = modelStreamOp.getSchema();
            modelStream = modelStreamOp.getDataStream();
        }
        if (inputs.length > 1) {
            StreamOperator<?> localModelStreamOp = inputs[1];
            if (modelStream == null) {
                modelStreamSchema = localModelStreamOp.getSchema();
                modelStream = localModelStreamOp.getDataStream();
            } else {
                localModelStreamOp = localModelStreamOp.select(modelStreamSchema.getFieldNames());
                modelStream = modelStream.union(localModelStreamOp.getDataStream());
            }
        }
        if (modelStream != null) {
            resultRows = in.getDataStream().connect(ModelStreamUtils.broadcastStream(modelStream)).flatMap(new PredictProcess(modelSchema, in.getSchema(), getParams(), mapperBuilder, modelDataBridge, ModelStreamUtils.findTimestampColIndexWithAssertAndHint(modelStreamSchema), ModelStreamUtils.findCountColIndexWithAssertAndHint(modelStreamSchema)));
        } else if (getParams().get(ModelMapperParams.NUM_THREADS) <= 1) {
            resultRows = in.getDataStream().map(new ModelMapperAdapter(mapper, modelSource));
        } else {
            resultRows = in.getDataStream().flatMap(new ModelMapperAdapterMT(mapper, modelSource, getParams().get(ModelMapperParams.NUM_THREADS)));
        }
        TableSchema resultSchema = mapper.getOutputSchema();
        this.setOutput(resultRows, resultSchema);
        return (T) this;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : TableSchema(org.apache.flink.table.api.TableSchema) PredictProcess(com.alibaba.alink.operator.common.stream.model.PredictProcess) ModelMapper(com.alibaba.alink.common.mapper.ModelMapper) ModelMapperAdapterMT(com.alibaba.alink.common.mapper.ModelMapperAdapterMT) ModelMapperAdapterMT(com.alibaba.alink.common.mapper.ModelMapperAdapterMT) DataBridgeModelSource(com.alibaba.alink.common.model.DataBridgeModelSource) Row(org.apache.flink.types.Row) ModelStreamFileSourceStreamOp(com.alibaba.alink.operator.stream.source.ModelStreamFileSourceStreamOp) ModelMapperAdapter(com.alibaba.alink.common.mapper.ModelMapperAdapter) DataBridge(com.alibaba.alink.common.io.directreader.DataBridge)

Example 2 with DataBridgeModelSource

use of com.alibaba.alink.common.model.DataBridgeModelSource in project Alink by alibaba.

the class BaseRecommStreamOp method linkFrom.

@Override
public T linkFrom(StreamOperator<?>... inputs) {
    StreamOperator<?> in = checkAndGetFirst(inputs);
    TableSchema modelSchema = this.model.getSchema();
    try {
        DataBridge modelDataBridge = DirectReader.collect(model);
        DataBridgeModelSource modelSource = new DataBridgeModelSource(modelDataBridge);
        RecommMapper mapper = new RecommMapper(this.recommKernelBuilder, this.recommType, modelSchema, in.getSchema(), this.getParams());
        DataStream<Row> resultRows;
        if (getParams().get(ModelMapperParams.NUM_THREADS) <= 1) {
            resultRows = in.getDataStream().map(new RecommAdapter(mapper, modelSource));
        } else {
            resultRows = in.getDataStream().flatMap(new RecommAdapterMT(mapper, modelSource, getParams().get(ModelMapperParams.NUM_THREADS)));
        }
        TableSchema outputSchema = mapper.getOutputSchema();
        this.setOutput(resultRows, outputSchema);
        return (T) this;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : RecommAdapter(com.alibaba.alink.operator.common.recommendation.RecommAdapter) TableSchema(org.apache.flink.table.api.TableSchema) RecommAdapterMT(com.alibaba.alink.operator.common.recommendation.RecommAdapterMT) RecommAdapterMT(com.alibaba.alink.operator.common.recommendation.RecommAdapterMT) DataBridgeModelSource(com.alibaba.alink.common.model.DataBridgeModelSource) Row(org.apache.flink.types.Row) RecommMapper(com.alibaba.alink.operator.common.recommendation.RecommMapper) DataBridge(com.alibaba.alink.common.io.directreader.DataBridge)

Aggregations

DataBridge (com.alibaba.alink.common.io.directreader.DataBridge)2 DataBridgeModelSource (com.alibaba.alink.common.model.DataBridgeModelSource)2 TableSchema (org.apache.flink.table.api.TableSchema)2 Row (org.apache.flink.types.Row)2 ModelMapper (com.alibaba.alink.common.mapper.ModelMapper)1 ModelMapperAdapter (com.alibaba.alink.common.mapper.ModelMapperAdapter)1 ModelMapperAdapterMT (com.alibaba.alink.common.mapper.ModelMapperAdapterMT)1 RecommAdapter (com.alibaba.alink.operator.common.recommendation.RecommAdapter)1 RecommAdapterMT (com.alibaba.alink.operator.common.recommendation.RecommAdapterMT)1 RecommMapper (com.alibaba.alink.operator.common.recommendation.RecommMapper)1 PredictProcess (com.alibaba.alink.operator.common.stream.model.PredictProcess)1 ModelStreamFileSourceStreamOp (com.alibaba.alink.operator.stream.source.ModelStreamFileSourceStreamOp)1