pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<i id='y9BjT'><tr id='y9BjT'><dt id='y9BjT'><q id='y9BjT'><span id='y9BjT'><b id='y9BjT'><form id='y9BjT'><ins id='y9BjT'></ins><ul id='y9BjT'></ul><sub id='y9BjT'></sub></form><legend id='y9BjT'></legend><bdo id='y9BjT'><pre id='y9BjT'><center id='y9BjT'></center></pre></bdo></b><th id='y9BjT'></th></span></q></dt></tr></i><div class="s3e8alz" id='y9BjT'><tfoot id='y9BjT'></tfoot><dl id='y9BjT'><fieldset id='y9BjT'></fieldset></dl></div>

<tfoot id='y9BjT'></tfoot>

    1. <legend id='y9BjT'><style id='y9BjT'><dir id='y9BjT'><q id='y9BjT'></q></dir></style></legend>

      • <bdo id='y9BjT'></bdo><ul id='y9BjT'></ul>
    2. <small id='y9BjT'></small><noframes id='y9BjT'>

        如何有效地使用窗口函數根據 N 個先前值來決定

        How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函數根據 N 個先前值來決定接下來的 N 個行)
        <legend id='WCXeD'><style id='WCXeD'><dir id='WCXeD'><q id='WCXeD'></q></dir></style></legend>

          <small id='WCXeD'></small><noframes id='WCXeD'>

            <bdo id='WCXeD'></bdo><ul id='WCXeD'></ul>

              <tbody id='WCXeD'></tbody>
            <i id='WCXeD'><tr id='WCXeD'><dt id='WCXeD'><q id='WCXeD'><span id='WCXeD'><b id='WCXeD'><form id='WCXeD'><ins id='WCXeD'></ins><ul id='WCXeD'></ul><sub id='WCXeD'></sub></form><legend id='WCXeD'></legend><bdo id='WCXeD'><pre id='WCXeD'><center id='WCXeD'></center></pre></bdo></b><th id='WCXeD'></th></span></q></dt></tr></i><div class="d28ewf3" id='WCXeD'><tfoot id='WCXeD'></tfoot><dl id='WCXeD'><fieldset id='WCXeD'></fieldset></dl></div>

            • <tfoot id='WCXeD'></tfoot>
                  本文介紹了如何有效地使用窗口函數根據 N 個先前值來決定接下來的 N 個行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有以下數據.

                  +----------+----+-------+-----------------------+
                  |      date|item|avg_val|conditions             |
                  +----------+----+-------+-----------------------+
                  |01-10-2020|   x|     10|                      0|
                  |02-10-2020|   x|     10|                      0|
                  |03-10-2020|   x|     15|                      1|
                  |04-10-2020|   x|     15|                      1|
                  |05-10-2020|   x|      5|                      0|
                  |06-10-2020|   x|     13|                      1|
                  |07-10-2020|   x|     10|                      1|
                  |08-10-2020|   x|     10|                      0|
                  |09-10-2020|   x|     15|                      1|
                  |01-10-2020|   y|     10|                      0|
                  |02-10-2020|   y|     18|                      0|
                  |03-10-2020|   y|      6|                      1|
                  |04-10-2020|   y|     10|                      0|
                  |05-10-2020|   y|     20|                      0|
                  +----------+----+-------+-----------------------+
                  

                  我正在嘗試基于

                  1. 如果標志值為 0,則新列值將為 0.
                  2. 如果標志為 1,則新列將為 1,接下來的四個 N 行數將為零,即無需檢查下一個 N 值.此過程將應用于每個項目,即按項目分區將起作用.

                  我在這里使用了 N = 4,

                  I have used here N = 4,

                  我已經使用了下面的代碼,但沒有有效的窗口函數是否有任何優化的方法.

                  I have used the below code but not effienntly windowing function is there any optimized way.

                  DROP TEMPORARY TABLE t2;
                  CREATE TEMPORARY TABLE t2
                  SELECT *,
                  MAX(conditions) OVER (PARTITION BY item ORDER BY item,`date` ROWS 4 PRECEDING ) AS new_row
                  FROM record
                  ORDER BY item,`date`;
                  
                   
                  
                   DROP TEMPORARY TABLE t3;
                  CREATE TEMPORARY TABLE t3
                  SELECT *,ROW_NUMBER() OVER (PARTITION BY item,new_row ORDER BY item,`date`) AS e FROM t2;
                  
                   
                  
                  
                  SELECT *,CASE WHEN new_row=1 AND e%5>1 THEN 0 
                  WHEN new_row=1 AND e%5=1 THEN 1 ELSE 0 END AS flag FROM t3;
                  

                  輸出類似于

                  +----------+----+-------+-----------------------+-----+
                  |      date|item|avg_val|conditions             |flag |
                  +----------+----+-------+-----------------------+-----+
                  |01-10-2020|   x|     10|                      0|    0|
                  |02-10-2020|   x|     10|                      0|    0|
                  |03-10-2020|   x|     15|                      1|    1|
                  |04-10-2020|   x|     15|                      1|    0|
                  |05-10-2020|   x|      5|                      0|    0|
                  |06-10-2020|   x|     13|                      1|    0|
                  |07-10-2020|   x|     10|                      1|    0|
                  |08-10-2020|   x|     10|                      0|    0|
                  |09-10-2020|   x|     15|                      1|    1|
                  |01-10-2020|   y|     10|                      0|    0|
                  |02-10-2020|   y|     18|                      0|    0|
                  |03-10-2020|   y|      6|                      1|    1|
                  |04-10-2020|   y|     10|                      0|    0|
                  |05-10-2020|   y|     20|                      0|    0|
                  +----------+----+-------+-----------------------+-----+
                  

                  但是我無法獲得輸出,我嘗試了更多.

                  But i am unable to get the ouput , i have tried more.

                  推薦答案

                  正如評論中所建議的(@nbk 和 @Akina),您將需要某種迭代器來實現邏輯.對于 SparkSQL 和 Spark 2.4+ 版,我們可以使用內置函數 aggregate 并設置一個結構數組和一個計數器作為累加器.下面是一個名為 record 的示例數據框和表(假設 conditions 列中的值為 01):

                  As suggested in the comments(by @nbk and @Akina), you will need some sort of iterator to implement the logic. With SparkSQL and Spark version 2.4+, we can use the builtin function aggregate and set an array of structs plus a counter as the accumulator. Below is an example dataframe and table named record(assume values in conditions column are either 0 or 1):

                  val df = Seq(
                      ("01-10-2020", "x", 10, 0), ("02-10-2020", "x", 10, 0), ("03-10-2020", "x", 15, 1),
                      ("04-10-2020", "x", 15, 1), ("05-10-2020", "x", 5, 0), ("06-10-2020", "x", 13, 1),
                      ("07-10-2020", "x", 10, 1), ("08-10-2020", "x", 10, 0), ("09-10-2020", "x", 15, 1),
                      ("01-10-2020", "y", 10, 0), ("02-10-2020", "y", 18, 0), ("03-10-2020", "y", 6, 1),
                      ("04-10-2020", "y", 10, 0), ("05-10-2020", "y", 20, 0)
                  ).toDF("date", "item", "avg_val", "conditions")
                  
                  df.createOrReplaceTempView("record")
                  

                  SQL:

                  spark.sql("""
                    SELECT t1.item, m.*
                    FROM (
                      SELECT item,
                        sort_array(collect_list(struct(date,avg_val,int(conditions) as conditions,conditions as flag))) as dta
                      FROM record
                      GROUP BY item
                    ) as t1 LATERAL VIEW OUTER inline(
                      aggregate(
                        /* expr: set up array `dta` from the 2nd element to the last 
                         *       notice that indices for slice function is 1-based, dta[i] is 0-based
                         */
                        slice(dta,2,size(dta)),
                        /* start: set up and initialize `acc` to a struct containing two fields:
                         * - dta: an array of structs with a single element dta[0]
                         * - counter: number of rows after flag=1, can be from `0` to `N+1`
                         */
                        (array(dta[0]) as dta, dta[0].conditions as counter),
                        /* merge: iterate through the `expr` using x and update two fields of `acc`
                         * - dta: append values from x to acc.dta array using concat + array functions
                         *        update flag using `IF(acc.counter IN (0,5) and x.conditions = 1, 1, 0)`
                         * - counter: increment by 1 if acc.counter is between 1 and 4
                         *            , otherwise set value to x.conditions
                         */
                        (acc, x) -> named_struct(
                            'dta', concat(acc.dta, array(named_struct(
                                'date', x.date,
                                'avg_val', x.avg_val,
                                'conditions', x.conditions,
                                'flag', IF(acc.counter IN (0,5) and x.conditions = 1, 1, 0)
                              ))),
                            'counter', IF(acc.counter > 0 and acc.counter < 5, acc.counter+1, x.conditions)
                          ),
                        /* finish: retrieve acc.dta only and discard acc.counter */
                        acc -> acc.dta
                      )
                    ) m
                  """).show(50)
                  

                  結果:

                  +----+----------+-------+----------+----+
                  |item|      date|avg_val|conditions|flag|
                  +----+----------+-------+----------+----+
                  |   x|01-10-2020|     10|         0|   0|
                  |   x|02-10-2020|     10|         0|   0|
                  |   x|03-10-2020|     15|         1|   1|
                  |   x|04-10-2020|     15|         1|   0|
                  |   x|05-10-2020|      5|         0|   0|
                  |   x|06-10-2020|     13|         1|   0|
                  |   x|07-10-2020|     10|         1|   0|
                  |   x|08-10-2020|     10|         0|   0|
                  |   x|09-10-2020|     15|         1|   1|
                  |   y|01-10-2020|     10|         0|   0|
                  |   y|02-10-2020|     18|         0|   0|
                  |   y|03-10-2020|      6|         1|   1|
                  |   y|04-10-2020|     10|         0|   0|
                  |   y|05-10-2020|     20|         0|   0|
                  +----+----------+-------+----------+----+
                  

                  地點:

                  1. 使用 groupby 將同一項目的行收集到名為 dta 列的結構數組中,該列具有 4 個字段:dateavg_valconditionsflag 并按 date
                  2. 排序
                  3. 使用aggregate函數遍歷上述結構體數組,根據counterconditions更新flag字段strong>(詳情見上面SQL代碼注釋)
                  4. 使用 Lateral VIEW 和 inline 函數分解來自聚合函數的結果結構數組
                  1. use groupby to collect rows for the same item into an array of structs named dta column with 4 fields: date, avg_val, conditions and flag and sorted by date
                  2. use aggregate function to iterate through the above array of structs, update the flag field based on counter and conditions (details see the above SQL code comments)
                  3. use Lateral VIEW and inline function to explode the resulting array of structs from the aggregate function

                  注意事項:

                  (1) 建議的 SQL 適用于 N=4,其中我們有 acc.counter IN (0,5)acc.counter <;5 在 SQL 中.對于任何 N,將上述調整為:acc.counter IN (0,N+1)acc.counter <;N+1,下圖為N=2 相同樣本數據的結果:

                  (1) the proposed SQL is for N=4, where we have acc.counter IN (0,5) and acc.counter < 5 in the SQL. For any N, adjust the above to: acc.counter IN (0,N+1) and acc.counter < N+1, the below shows the result for N=2 with the same sample data:

                  +----+----------+-------+----------+----+
                  |item|      date|avg_val|conditions|flag|
                  +----+----------+-------+----------+----+
                  |   x|01-10-2020|     10|         0|   0|
                  |   x|02-10-2020|     10|         0|   0|
                  |   x|03-10-2020|     15|         1|   1|
                  |   x|04-10-2020|     15|         1|   0|
                  |   x|05-10-2020|      5|         0|   0|
                  |   x|06-10-2020|     13|         1|   1|
                  |   x|07-10-2020|     10|         1|   0|
                  |   x|08-10-2020|     10|         0|   0|
                  |   x|09-10-2020|     15|         1|   1|
                  |   y|01-10-2020|     10|         0|   0|
                  |   y|02-10-2020|     18|         0|   0|
                  |   y|03-10-2020|      6|         1|   1|
                  |   y|04-10-2020|     10|         0|   0|
                  |   y|05-10-2020|     20|         0|   0|
                  +----+----------+-------+----------+----+
                  

                  (2) 我們使用dta[0] 來初始化acc,它包括其字段的值和數據類型.理想情況下,我們應該確保這些字段的數據類型正確,以便正確進行所有計算.例如在計算 acc.counter 時,如果 conditions 是 StringType,acc.counter+1 將返回一個帶有 DoubleType 值的 StringType

                  (2) we use dta[0] to initialize acc which includes both the values and datatypes of its fields. Ideally, we should make sure data types of these fields right so that all calculations are correctly conducted. for example when calculating acc.counter, if conditions is StringType, acc.counter+1 will return a StringType with a DoubleType value

                  spark.sql("select '2'+1").show()
                  +---------------------------------------+
                  |(CAST(2 AS DOUBLE) + CAST(1 AS DOUBLE))|
                  +---------------------------------------+
                  |                                    3.0|
                  +---------------------------------------+
                  

                  當使用 acc.counter IN (0,5)acc.counter 將其值與整數進行比較時,可能會產生浮點錯誤.5.根據 OP 的反饋,這產生了錯誤的結果,沒有任何警告/錯誤消息.

                  Which could yield floating-point errors when comparing their value with integers using acc.counter IN (0,5) or acc.counter < 5. Based on OP's feedback, this produced incorrect result without any WARNING/ERROR message.

                  • 一種解決方法是在設置聚合函數的第二個參數時使用 CAST 指定確切的字段類型,以便在任何類型不匹配時報告錯誤,見下文:

                  • One workaround is to specify exact field types using CAST when setting up the 2nd argument of aggregate function so it reports ERROR when any types mismatch, see below:

                  CAST((array(dta[0]), dta[0].conditions) as struct<dta:array<struct<date:string,avg_val:string,conditions:int,flag:int>>,counter:int>),
                  

                1. 另一種在創建 dta 列時強制類型的解決方案,在此示例中,請參閱以下代碼中的 int(conditions) as conditions:

                2. Another solution it to force types when creating dta column, in this example, see int(conditions) as conditions in below code:

                  SELECT item,
                    sort_array(collect_list(struct(date,avg_val,int(conditions) as conditions,conditions as flag))) as dta
                  FROM record
                  GROUP BY item
                  

                3. 我們也可以在計算中強制使用數據類型,例如,參見下面的int(acc.counter+1):

                  IF(acc.counter > 0 and acc.counter < 5, int(acc.counter+1), x.conditions)      
                  

                4. 這篇關于如何有效地使用窗口函數根據 N 個先前值來決定接下來的 N 個行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用選擇表達式的結果;條款?)
                  Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函數的 ignore 選項是忽略整個事務還是只是有問題的行?) - IT屋-程序員軟件開發技
                  Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 時出錯,使用 for 循環數組)
                  pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 調用 o23.load 時發生錯誤 沒有合適的驅動程序)
                  How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何將 Apache Spark 與 MySQL 集成以將數據庫表作為 Spark 數據幀讀取?)
                  In Apache Spark 2.0.0, is it possible to fetch a query from an external database (rather than grab the whole table)?(在 Apache Spark 2.0.0 中,是否可以從外部數據庫獲取查詢(而不是獲取整個表)?) - IT屋-程序員軟件開
                5. <i id='JePBC'><tr id='JePBC'><dt id='JePBC'><q id='JePBC'><span id='JePBC'><b id='JePBC'><form id='JePBC'><ins id='JePBC'></ins><ul id='JePBC'></ul><sub id='JePBC'></sub></form><legend id='JePBC'></legend><bdo id='JePBC'><pre id='JePBC'><center id='JePBC'></center></pre></bdo></b><th id='JePBC'></th></span></q></dt></tr></i><div class="cv88eg2" id='JePBC'><tfoot id='JePBC'></tfoot><dl id='JePBC'><fieldset id='JePBC'></fieldset></dl></div>
                  • <tfoot id='JePBC'></tfoot>

                      <legend id='JePBC'><style id='JePBC'><dir id='JePBC'><q id='JePBC'></q></dir></style></legend>

                        <small id='JePBC'></small><noframes id='JePBC'>

                        • <bdo id='JePBC'></bdo><ul id='JePBC'></ul>
                            <tbody id='JePBC'></tbody>

                            主站蜘蛛池模板: 连续油炸机,全自动油炸机,花生米油炸机-烟台茂源食品机械制造有限公司 | 网站建设,北京网站建设,北京网站建设公司,网站系统开发,北京网站制作公司,响应式网站,做网站公司,海淀做网站,朝阳做网站,昌平做网站,建站公司 | 珠海冷却塔降噪维修_冷却塔改造报价_凉水塔风机维修厂家- 广东康明节能空调有限公司 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 药品冷藏箱厂家_低温冰箱_洁净工作台-济南欧莱博电子商务有限公司官网 | 博医通医疗器械互联网供应链服务平台_博医通 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 斗式提升机,斗式提升机厂家-淄博宏建机械有限公司 | 电动手术床,医用护理床,led手术无影灯-曲阜明辉医疗设备有限公司 | 酒水灌装机-白酒灌装机-酒精果酒酱油醋灌装设备_青州惠联灌装机械 | 高效节能电机_伺服主轴电机_铜转子电机_交流感应伺服电机_图片_型号_江苏智马科技有限公司 | EFM 022静电场测试仪-套帽式风量计-静电平板监测器-上海民仪电子有限公司 | 玻璃钢格栅盖板|玻璃钢盖板|玻璃钢格栅板|树篦子-长沙川皖玻璃钢制品有限公司 | 塑料瓶罐_食品塑料瓶_保健品塑料瓶_调味品塑料瓶–东莞市富慷塑料制品有限公司 | 北京银联移动POS机办理_收银POS机_智能pos机_刷卡机_收银系统_个人POS机-谷骐科技【官网】 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 博博会2021_中国博物馆及相关产品与技术博览会【博博会】 | 建大仁科-温湿度变送器|温湿度传感器|温湿度记录仪_厂家_价格-山东仁科 | 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 成都珞石机械 - 模温机、油温机、油加热器生产厂家 | 昆明挖掘机修理厂_挖掘机翻新再制造-昆明聚力工程机械维修有限公司 | 通信天线厂家_室分八木天线_对数周期天线_天线加工厂_林创天线源头厂家 | 【中联邦】增稠剂_增稠粉_水性增稠剂_涂料增稠剂_工业增稠剂生产厂家 | 河南膏药贴牌-膏药代加工-膏药oem厂家-洛阳今世康医药科技有限公司 | 安徽泰科检测科技有限公司【官方网站】 | 代写标书-专业代做标书-商业计划书代写「深圳卓越创兴公司」 | 雷蒙磨,雷蒙磨粉机,雷蒙磨机 - 巩义市大峪沟高峰机械厂 | 仿古瓦,仿古金属瓦,铝瓦,铜瓦,铝合金瓦-西安东申景观艺术工程有限公司 | 上海防爆真空干燥箱-上海防爆冷库-上海防爆冷柜?-上海浦下防爆设备厂家? | TYPE-C厂家|TYPE-C接口|TYPE-C防水母座|TYPE-C贴片-深圳步步精 | 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | 室内室外厚型|超薄型|非膨胀型钢结构防火涂料_隧道专用防火涂料厂家|电话|价格|批发|施工 | 塑料脸盆批发,塑料盆生产厂家,临沂塑料广告盆,临沂家用塑料盆-临沂市永顺塑业 | 云南标线|昆明划线|道路标线|交通标线-就选云南云路施工公司-云南云路科技有限公司 | 钢骨架轻型板_膨石轻型板_钢骨架轻型板价格_恒道新材料 | 空压机网_《压缩机》杂志 | 依维柯自动挡房车,自行式国产改装房车,小型房车价格,中国十大房车品牌_南京拓锐斯特房车 - 南京拓锐斯特房车 | 东莞市天进机械有限公司-钉箱机-粘箱机-糊箱机-打钉机认准东莞天进机械-厂家直供更放心! | 美国PARKER齿轮泵,美国PARKER柱塞泵,美国PARKER叶片泵,美国PARKER电磁阀,美国PARKER比例阀-上海维特锐实业发展有限公司二部 | 深圳美安可自动化设备有限公司,喷码机,定制喷码机,二维码喷码机,深圳喷码机,纸箱喷码机,东莞喷码机 UV喷码机,日期喷码机,鸡蛋喷码机,管芯喷码机,管内壁喷码机,喷码机厂家 | 网络推广公司_网络营销方案策划_企业网络推广外包平台-上海澜推网络 |