Java 类名:com.alibaba.alink.operator.stream.timeseries.LookupVectorInTimeSeriesStreamOp
Python 类名:LookupVectorInTimeSeriesStreamOp

功能介绍

在时间序列中查找对应时间的值。

参数说明

| 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- |

| outputCol | 输出结果列列名 | 输出结果列列名,必选 | String | ✓ | | |

| timeCol | 时间戳列(TimeStamp) | 时间戳列(TimeStamp) | String | ✓ | 所选列类型为 [TIMESTAMP] | |

| timeSeriesCol | 时间序列列 | 时间序列列,是特殊的MTable类型,一列是时间,一列是值 | String | ✓ | 所选列类型为 [M_TABLE] | |

| reservedCols | 算法保留列名 | 算法保留列 | String[] | | | null |

| numThreads | 组件多线程线程个数 | 组件多线程线程个数 | Integer | | | 1 |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. import time, datetime
  5. import numpy as np
  6. import pandas as pd
  7. data = pd.DataFrame([
  8. [1, datetime.datetime.fromtimestamp(1), "10.0"],
  9. [1, datetime.datetime.fromtimestamp(2), "11.0"],
  10. [1, datetime.datetime.fromtimestamp(3), "12.0"],
  11. [1, datetime.datetime.fromtimestamp(4), "13.0"],
  12. [1, datetime.datetime.fromtimestamp(5), "14.0"],
  13. [1, datetime.datetime.fromtimestamp(6), "15.0"],
  14. [1, datetime.datetime.fromtimestamp(7), "16.0"],
  15. [1, datetime.datetime.fromtimestamp(8), "17.0"],
  16. [1, datetime.datetime.fromtimestamp(9), "18.0"],
  17. [1, datetime.datetime.fromtimestamp(10), "19.0"]
  18. ])
  19. source = dataframeToOperator(data, schemaStr='id int, ts timestamp, val string', op_type='stream')
  20. source.link(
  21. OverCountWindowStreamOp()
  22. .setGroupCols(["id"])
  23. .setTimeCol("ts")
  24. .setPrecedingRows(5)
  25. .setClause("mtable_agg_preceding(ts, val) as data")
  26. ).link(
  27. ShiftStreamOp()
  28. .setValueCol("data")
  29. .setShiftNum(7)
  30. .setPredictNum(12)
  31. .setPredictionCol("predict")
  32. ).link(
  33. LookupVectorInTimeSeriesStreamOp()
  34. .setTimeCol("ts")
  35. .setTimeSeriesCol("predict")
  36. .setOutputCol("out")
  37. ).print()
  38. StreamOperator.execute()

Java 代码

  1. package com.alibaba.alink.operator.stream.timeseries;
  2. import org.apache.flink.types.Row;
  3. import com.alibaba.alink.common.MTable;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import com.alibaba.alink.operator.batch.timeseries.LookupVectorInTimeSeriesBatchOp;
  6. import com.alibaba.alink.operator.stream.StreamOperator;
  7. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  8. import org.junit.Test;
  9. import java.sql.Timestamp;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. public class LookupVectorInTimeSeriesStreamOpTest {
  13. @Test
  14. public void test() throws Exception {
  15. List <Row> mTableData = Arrays.asList(
  16. Row.of(new Timestamp(1), "10.0 21.0"),
  17. Row.of(new Timestamp(2), "11.0 22.0"),
  18. Row.of(new Timestamp(3), "12.0 23.0"),
  19. Row.of(new Timestamp(4), "13.0 24.0"),
  20. Row.of(new Timestamp(5), "14.0 25.0"),
  21. Row.of(new Timestamp(6), "15.0 26.0"),
  22. Row.of(new Timestamp(7), "16.0 27.0"),
  23. Row.of(new Timestamp(8), "17.0 28.0"),
  24. Row.of(new Timestamp(9), "18.0 29.0"),
  25. Row.of(new Timestamp(10), "19.0 30.0")
  26. );
  27. MemSourceStreamOp source = new MemSourceStreamOp(mTableData, new String[] {"ts", "val"});
  28. source.link(
  29. new OverCountWindowStreamOp()
  30. .setTimeCol("ts")
  31. .setPrecedingRows(5)
  32. .setClause("mtable_agg(ts, val) as data")
  33. ).link(
  34. new ShiftStreamOp()
  35. .setValueCol("data")
  36. .setShiftNum(7)
  37. .setPredictNum(12)
  38. .setPredictionCol("predict")
  39. ).link(
  40. new LookupVectorInTimeSeriesStreamOp()
  41. .setTimeCol("ts")
  42. .setTimeSeriesCol("predict")
  43. .setOutputCol("out")
  44. .setReservedCols("ts")
  45. ).print();
  46. StreamOperator.execute();
  47. }
  48. }

运行结果

| ts | out | | —- | —- |

| 1970-01-01 08:00:00.001 | null |

| 1970-01-01 08:00:00.003 | 10.0 21.0 |

| 1970-01-01 08:00:00.002 | 10.0 21.0 |

| 1970-01-01 08:00:00.005 | 10.0 21.0 |

| 1970-01-01 08:00:00.004 | 10.0 21.0 |

| 1970-01-01 08:00:00.007 | 11.0 22.0 |

| 1970-01-01 08:00:00.006 | 10.0 21.0 |

| 1970-01-01 08:00:00.009 | 13.0 24.0 |

| 1970-01-01 08:00:00.008 | 12.0 23.0 |

| 1970-01-01 08:00:00.01 | 14.0 25.0 |