我们有绿色区域逻辑,其中作业只能在第一个星期日到星期六之间运行,即从每个月的第一个星期日开始的 7 天。我正在使用下面的 awk 命令来获取它,但它在某处坏了。我只是在尝试前 3 个月,即 1 月到 3 月
seq 75 | awk ' BEGIN {ti=" 0 0 0"}
function dtf(fmt,dy) { return strftime(fmt,mktime("2020 1 " dy ti)) }
{ day=dtf("%A %F",$0);mm=dtf("%m",$0);if(day~/Sunday/ || a[mm]) a[mm]++ ; if(a[mm]<8) print day } '
Run Code Online (Sandbox Code Playgroud)
我的输出如下,这是不正确的:
Wednesday 2020-01-01
Thursday 2020-01-02
Friday 2020-01-03
Saturday 2020-01-04
Sunday 2020-01-05
Monday 2020-01-06
Tuesday 2020-01-07
Wednesday 2020-01-08
Thursday 2020-01-09
Friday 2020-01-10
Saturday 2020-01-11
Saturday 2020-02-01
Sunday 2020-02-02
Monday 2020-02-03
Tuesday 2020-02-04
Wednesday 2020-02-05
Thursday 2020-02-06
Friday 2020-02-07 …Run Code Online (Sandbox Code Playgroud) 当我posexplode()在 Spark SQL 中使用函数时,以下语句生成“pos”和“col”作为默认名称
scala> spark.sql(""" with t1(select to_date('2019-01-01') first_day) select first_day,date_sub(add_months(first_day,1),1) last_day, posexplode(array(5,6,7)) from t1 """).show(false)
+----------+----------+---+---+
|first_day |last_day |pos|col|
+----------+----------+---+---+
|2019-01-01|2019-01-31|0 |5 |
|2019-01-01|2019-01-31|1 |6 |
|2019-01-01|2019-01-31|2 |7 |
+----------+----------+---+---+
Run Code Online (Sandbox Code Playgroud)
在 spark.sql 中覆盖这些默认名称的语法是什么?在数据帧中,这可以通过给出df.explode(select 'arr.as(Seq("arr_val","arr_pos")))
scala> val arr= Array(5,6,7)
arr: Array[Int] = Array(5, 6, 7)
scala> Seq(("dummy")).toDF("x").select(posexplode(lit(arr)).as(Seq("arr_val","arr_pos"))).show(false)
+-------+-------+
|arr_val|arr_pos|
+-------+-------+
|0 |5 |
|1 |6 |
|2 |7 |
+-------+-------+
Run Code Online (Sandbox Code Playgroud)
如何在 SQL 中得到它?我试过
spark.sql(""" with t1(select to_date('2011-01-01') first_day) select first_day,date_sub(add_months(first_day,1),1) last_day, posexplode(array(5,6,7)) as(Seq('p','c')) from t1 """).show(false) …Run Code Online (Sandbox Code Playgroud) 以下代码适用于 Scala-Spark。
scala> val ar = Array("oracle", "java")
ar: Array[String] = Array(oracle, java)
scala> df.withColumn("tags", lit(ar)).show(false)
+------+---+----------+----------+--------------+
|name |age|role |experience|tags |
+------+---+----------+----------+--------------+
|John |25 |Developer |2.56 |[oracle, java]|
|Scott |30 |Tester |5.2 |[oracle, java]|
|Jim |28 |DBA |3.0 |[oracle, java]|
|Mike |35 |Consultant|10.0 |[oracle, java]|
|Daniel|26 |Developer |3.2 |[oracle, java]|
|Paul |29 |Tester |3.6 |[oracle, java]|
|Peter |30 |Developer |6.5 |[oracle, java]|
+------+---+----------+----------+--------------+
Run Code Online (Sandbox Code Playgroud)
如何在 PySpark 中获得相同的行为?我尝试了以下方法,但它不起作用并引发 Java 错误。
from pyspark.sql.types import *
tag = ["oracle", "java"]
df2.withColumn("tags", lit(tag)).show()
Run Code Online (Sandbox Code Playgroud)
:java.lang.RuntimeException:不支持的文字类型类java.util.ArrayList [oracle,java]
在Scala中,我想将每条消息(长度= 20)解析为单个单元.该消息将附加到上一条消息的末尾,而不会出现换行符.我尝试了下面的内容,但欢迎任何优化和改进性能
/* Length.. id=3,name=5,city=8,port=3,indicator=1 */
def layout(rec:String) = {
val id=rec.take(3)
val name=rec.drop(3).take(5)
val city=rec.drop(3+5).take(8)
val port=rec.drop(3+5+8).take(3)
val ind=rec.drop(3+5+8+3).take(1)
println(id,name,city,port,ind)
}
val messages="101Jim Portland990Y102JamesHouston 990X103John Boston 880Y"
messages grouped(20) foreach { x => layout(x) }
In REPL,
scala> :load work.scala
Loading work.scala...
layout: (rec: String)Unit
messages: String = 101Jim Portland990Y102JamesHouston 990X103John Boston 880Y
(101,Jim ,Portland,990,Y)
(102,James,Houston ,990,X)
(103,John ,Boston ,880,Y)
scala>
Run Code Online (Sandbox Code Playgroud) 我有这四个陈述
$ perl -e ' $in="axaxaxhhhh"; $in=~s/a/p/ for 1..2 ; print $in ' # 1. Ok
pxpxaxhhhh
$ perl -e ' $_="axaxaxhhhh"; $_=~s/a/p/ for 1..2 ; print $_ ' # 2. Why not working?
axaxaxhhhh
$ perl -e ' $_="axaxaxhhhh"; $_=~s/a/p/g; print $_ ' # 3.good
pxpxpxhhhh
$ perl -e ' $_="axaxaxhhhh"; $_=~s/a/p/; print $_ ' # 4.good
pxaxaxhhhh
$
Run Code Online (Sandbox Code Playgroud)
第二种情况是怎么回事?为什么不修改$ _?
我想获取固定 8 字节数组中“Long”的字节数组值
scala> Long.MaxValue
res191: Long = 9223372036854775807
scala> val i:Long = Long.MaxValue
i: Long = 9223372036854775807
scala> BigInt(i).toByteArray
res192: Array[Byte] = Array(127, -1, -1, -1, -1, -1, -1, -1)
scala> val i:Long = 100
i: Long = 100
scala> BigInt(i).toByteArray
res193: Array[Byte] = Array(100) // what I want is Array(0, 0, 0, 0, 0, 0, 0, 100)
scala>
Run Code Online (Sandbox Code Playgroud)
我正在做的是
scala> var a:Array[Byte] = Array(0, 0, 0, 0, 0, 0, 0, 0)
a: Array[Byte] = Array(0, 0, 0, 0, …Run Code Online (Sandbox Code Playgroud) 我有一项业务要求,要读取字节长度在1到8之间的带符号整数值。
int值的标准实现占用2 ** n数量级的字节
> perl -e ' $x=chr(253); $y=unpack "c",$x; printf("%d\n",$y) ' # 1 byte
-3
> perl -e ' $x=chr(255).chr(253); $y=unpack "s>",$x; printf("%d\n",$y)' # 2 byte
-3
> perl -e ' $x=chr(255) x 3;$x.=chr(253); $y=unpack "i>",$x; printf("%d\n",$y) ' # 4 byte
-3
> perl -e ' $x=chr(255) x 7;$x.=chr(253); $y=unpack "q>",$x; printf("%d\n",$y) ' # 8 byte
-3
>
Run Code Online (Sandbox Code Playgroud)
对于3、5、6、7个字节,我正在尝试如下
> perl -e ' $x=chr(255) x 2;$x.=chr(253); $y=unpack "i>",$x; printf("%d\n",$y) '
0
>
Run Code Online (Sandbox Code Playgroud)
但这是错误的,我需要-3。
该链接在Perl中解码3字节整数可以回答无符号数字,但不能解决我的问题。
有人可以帮助获取3、5、6、7字节的有符号值吗?
我试图通过保留元素的顺序来获取列表中单词的连续计数
scala> val a = List("she","sells","seashells","by","the","seashore","the", "shells", "she", "sells", "are", "surely", "seashells","where", "are", "the", "shells")
a: List[String] = List(she, sells, seashells, by, the, seashore, the, shells, she, sells, are, surely, seashells, where, are, the, shells)
scala> a.map( x => (x,a.count(_ == x)))
res13: List[(String, Int)] = List((she,2), (sells,2), (seashells,2), (by,1), (the,3), (seashore,1), (the,3), (shells,2), (she,2), (sells,2), (are,2), (surely,1), (seashells,2), (where,1), (are,2), (the,3), (shells,2))
scala>
Run Code Online (Sandbox Code Playgroud)
但是我想要的是
List((she,1), (sells,1), (seashells,1), (by,1), (the,1), (seashore,1), (the,2), (shells,1), (she,2), (sells,2), (are,1), (surely,1), (seashells,2), (where,1), (are,2), …Run Code Online (Sandbox Code Playgroud) 我需要从文件中的每条记录中获取最后 4 个唯一值。文件由逗号分隔,不同记录的列数可以不同。我的输入文件。
$ cat last_cols.txt
F
F,B
F,B,A
F,B,A,F
F,B,A,F,B
F,B,A,F,B,G
F,B,A,F,B,G,E
F,B,A,F,B,G,E,F
F,B,A,F,B,G,E,F,E
F,B,A,F,B,G,E,F,E,B
F,B,A,F,B,G,E,F,E,B,A
F,B,A,F,B,G,E,F,E,B,A,D
F,B,A,F,B,G,E,F,E,B,A,D,F
F,B,A,F,B,G,E,F,E,B,A,D,F,E
F,B,A,F,B,G,E,F,E,B,A,D,F,E,E
F,B,A,F,B,G,E,F,E,B,A,D,F,E,E,D
$
Run Code Online (Sandbox Code Playgroud)
我需要的输出:
F,
B,F,
A,B,F,
A,B,F,
A,B,F,
A,B,F,G,
B,E,F,G,
B,E,F,G,
B,E,F,G,
B,E,F,G,
A,B,E,F,
A,B,D,E,
A,B,D,F,
A,D,E,F,
A,D,E,F,
A,D,E,F,
Run Code Online (Sandbox Code Playgroud)
尝试获取输出
awk -F, ' { split("",arr); for(i=NF;i>0;i--) { if(!$i in arr && length(arr)<4 ) arr[$i]=1 }; for(i in arr) printf("%s,",i); print "" } ' last_cols.txt
Run Code Online (Sandbox Code Playgroud)
我只是得到空行。
由于这将在 bash env 中运行,因此我也欢迎其他解决方案。
我有以下代码片段,常规for循环工作正常。但是对于理解会抛出错误
println("Using for loop")
for( (key, value) <- orec.groupBy(x => x.continent) )
{
println(key + value.length )
}
println("Using for comprehension")
for{
(key, value) <- orec.groupBy(x => x.continent)
println(key + value.length )
} yield (key,value)
Run Code Online (Sandbox Code Playgroud)
错误消息是
Error:(84, 5) '<-' expected but '}' found.
} yield (key,value)
Run Code Online (Sandbox Code Playgroud)
我不明白代码有什么问题。请协助解决问题
我需要将描述性日期格式从日志文件“MMM dd, yyyy hh:mm:ss AM/PM”转换为 spark 时间戳数据类型。我尝试了类似下面的方法,但它给出了空值。
val df = Seq(("Nov 05, 2018 02:46:47 AM"),("Nov 5, 2018 02:46:47 PM")).toDF("times")
df.withColumn("time2",date_format('times,"MMM dd, yyyy HH:mm:ss AM")).show(false)
+------------------------+-----+
|times |time2|
+------------------------+-----+
|Nov 05, 2018 02:46:47 AM|null |
|Nov 5, 2018 02:46:47 PM |null |
+------------------------+-----+
Run Code Online (Sandbox Code Playgroud)
预期输出
+------------------------+----------------------------+
|times |time2 |
+------------------------+-----+----------------------+
|Nov 05, 2018 02:46:47 AM|2018-11-05 02:46:47.000000" |
|Nov 5, 2018 02:46:47 PM |2018-11-05 14:46:47.000000" |
+------------------------+-----+----------------------+
Run Code Online (Sandbox Code Playgroud)
转换这个的正确格式是什么?请注意,DD 可能有前导零。
我正在尝试使用以下功能交换列表中的前2个元素。
def swap_list(a:List[Int]):List[Int]={
a match {
case x::y::Nil => List(y,x)
case List(x,y,rest @ _*) => List(y,x)
case _ => a
}
}
swap_list(List(10,20,30))
Run Code Online (Sandbox Code Playgroud)
这可行。但是,如果我尝试包含,rest则会出现类似以下错误
case List(x,y,rest @ _*) => List(y,x) +: rest
Run Code Online (Sandbox Code Playgroud)
错误如下
Error:(27, 50) type mismatch;
found : Seq[Any]
required: List[Int]
case List(x,y,rest @ _*) => List(y,x) +: rest
Run Code Online (Sandbox Code Playgroud)
在定义中指定函数结果类型时,为什么在错误消息中得到Seq [Any]?
我需要返回List(20,10,30)。如何解决呢?
我使用的是spark-sql 2.3.1,我设置
spark.sql.shuffle.partitions=40
Run Code Online (Sandbox Code Playgroud)
在我的代码中'
val partitioned_df = vals_df.repartition(col("model_id"),col("fiscal_year"),col("fiscal_quarter"))
Run Code Online (Sandbox Code Playgroud)
当我说
println(" Number of partitions : " + partitioned_df.rdd.getNumPartitions)
Run Code Online (Sandbox Code Playgroud)
它给出 40 作为输出,事实上重新分区后理想情况下计数应该在 400 左右,为什么重新分区在这里不起作用?我在这里做错了什么?如何修复它?