Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在条件下在spark dataframe中创建新列_Dataframe_Apache Spark_Pyspark_Apache Spark Sql - Fatal编程技术网

在条件下在spark dataframe中创建新列

在条件下在spark dataframe中创建新列,dataframe,apache-spark,pyspark,apache-spark-sql,Dataframe,Apache Spark,Pyspark,Apache Spark Sql,还有一个问题!类似的是 我有一个关于连接日志的数据框,其中包含ID,targetIP,targetPort,Time。此数据帧中的每个记录都是到一个系统的连接事件ID表示此连接,targetIP表示此次的目标IP地址,targetPort表示目标端口,time表示连接时间。价值观: 身份证件 时间 目标 目标港 1. 1. 192.163.0.1 53 2. 2. 192.163.0.2 54 3. 3. 192.163.0.1 1028 4. 5. 192.163.0.1 1028 5. 6.

还有一个问题!类似的是

我有一个关于连接日志的数据框,其中包含
ID
targetIP
targetPort
Time
。此数据帧中的每个记录都是到一个系统的连接事件
ID
表示此连接,
targetIP
表示此次的目标IP地址,
targetPort
表示目标端口,
time
表示连接时间。价值观:

身份证件 时间 目标 目标港 1. 1. 192.163.0.1 53 2. 2. 192.163.0.2 54 3. 3. 192.163.0.1 1028 4. 5. 192.163.0.1 1028 5. 6. 192.163.0.2 63 6. 7. 192.163.0.2 64 7. 8. 192.163.0.2 63
您可以使用窗口函数添加两列ip_count和port_count,然后将它们除以以获得比率:

从pyspark.sql导入函数为F,窗口
结果=df.withColumn(
“ip_计数”,
F.count('*').over(Window.partitionBy('targetIP').orderBy('Time').rangeBetween(-2,-1))
).withColumn(
“端口计数”,
F.count('*')。over(Window.partitionBy('targetIP','targetPort')。orderBy('Time')。rangeBetween(-2,-1))
).withColumn(
“结果”,
F.when(F.col('ip\u count')!=0,F.col('port\u count')/F.col('ip\u count'))。否则(0)
).orderBy('ID'))
result.show()
+---+----+-----------+----------+--------+----------+------+
|ID |时间|目标ip |目标端口| ip | U计数|端口|计数|结果|
+---+----+-----------+----------+--------+----------+------+
|  1|   1|192.163.0.1|        53|       0|         0|   0.0|
|  2|   2|192.163.0.2|        54|       0|         0|   0.0|
|  3|   3|192.163.0.1|      1028|       1|         0|   0.0|
|  4|   5|192.163.0.1|      1028|       1|         1|   1.0|
|  5|   6|192.163.0.2|        63|       0|         0|   0.0|
|  6|   7|192.163.0.2|        64|       1|         0|   0.0|
|  7|   8|192.163.0.2|        63|       2|         1|   0.5|
+---+----+-----------+----------+--------+----------+------+

您可以使用窗口函数添加两列ip\u计数和端口\u计数,并将它们除以以获得比率:

从pyspark.sql导入函数为F,窗口
结果=df.withColumn(
“ip_计数”,
F.count('*').over(Window.partitionBy('targetIP').orderBy('Time').rangeBetween(-2,-1))
).withColumn(
“端口计数”,
F.count('*')。over(Window.partitionBy('targetIP','targetPort')。orderBy('Time')。rangeBetween(-2,-1))
).withColumn(
“结果”,
F.when(F.col('ip\u count')!=0,F.col('port\u count')/F.col('ip\u count'))。否则(0)
).orderBy('ID'))
result.show()
+---+----+-----------+----------+--------+----------+------+
|ID |时间|目标ip |目标端口| ip | U计数|端口|计数|结果|
+---+----+-----------+----------+--------+----------+------+
|  1|   1|192.163.0.1|        53|       0|         0|   0.0|
|  2|   2|192.163.0.2|        54|       0|         0|   0.0|
|  3|   3|192.163.0.1|      1028|       1|         0|   0.0|
|  4|   5|192.163.0.1|      1028|       1|         1|   1.0|
|  5|   6|192.163.0.2|        63|       0|         0|   0.0|
|  6|   7|192.163.0.2|        64|       1|         0|   0.0|
|  7|   8|192.163.0.2|        63|       2|         1|   0.5|
+---+----+-----------+----------+--------+----------+------+

您可以使用两个窗口,一个窗口由
targetIP
targetPort
分区,另一个窗口仅由
targetIP
分区,然后将计数除以
w1
除以计数除以
w2
,得到最后2个时间单位的百分比:

from pyspark.sql import Window
from pyspark.sql import functions as F


w = Window.partitionBy("targetIP", "targetPort").orderBy(F.col("Time")).rangeBetween(-2, -1)
w2 = Window.partitionBy("targetIP").orderBy(col("Time")).rangeBetween(-2, -1)

df1 = df.withColumn(
    "result",
    F.coalesce(F.count("*").over(w) / F.count("*").over(w2), F.lit(0))
).orderBy("ID")

df1.show()

#+---+----+-----------+----------+------+
#| ID|Time|   targetIP|targetPort|result|
#+---+----+-----------+----------+------+
#|  1|   1|192.163.0.1|        53|   0.0|
#|  2|   2|192.163.0.2|        54|   0.0|
#|  3|   3|192.163.0.1|      1028|   0.0|
#|  4|   5|192.163.0.1|      1028|   1.0|
#|  5|   6|192.163.0.2|        63|   0.0|
#|  6|   7|192.163.0.2|        64|   0.0|
#|  7|   8|192.163.0.2|        63|   0.5|
#+---+----+-----------+----------+------+

您可以使用两个窗口,一个按
targetIP
targetPort
分区,另一个仅按
targetIP
分区,然后将
w1
上的计数除以
w2
上的计数,以获得最后2个时间单位的百分比:

from pyspark.sql import Window
from pyspark.sql import functions as F


w = Window.partitionBy("targetIP", "targetPort").orderBy(F.col("Time")).rangeBetween(-2, -1)
w2 = Window.partitionBy("targetIP").orderBy(col("Time")).rangeBetween(-2, -1)

df1 = df.withColumn(
    "result",
    F.coalesce(F.count("*").over(w) / F.count("*").over(w2), F.lit(0))
).orderBy("ID")

df1.show()

#+---+----+-----------+----------+------+
#| ID|Time|   targetIP|targetPort|result|
#+---+----+-----------+----------+------+
#|  1|   1|192.163.0.1|        53|   0.0|
#|  2|   2|192.163.0.2|        54|   0.0|
#|  3|   3|192.163.0.1|      1028|   0.0|
#|  4|   5|192.163.0.1|      1028|   1.0|
#|  5|   6|192.163.0.2|        63|   0.0|
#|  6|   7|192.163.0.2|        64|   0.0|
#|  7|   8|192.163.0.2|        63|   0.5|
#+---+----+-----------+----------+------+