- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将 RDD 更改为 DataFrame 并将结果与我使用 read.csv 导入的另一个 DataFrame 进行比较,但两种方法的浮点精度不同。我感谢您的帮助。
我使用的数据来自 here .
from pyspark.sql import Row
from pyspark.sql.types import *
orders = sc.textFile("retail_db/orders")
order_items = sc.textFile('retail_db/order_items')
orders_comp = orders.filter(lambda line: ((line.split(',')[-1] == 'CLOSED') or (line.split(',')[-1] == 'COMPLETE')))
orders_compMap = orders_comp.map(lambda line: (int(line.split(',')[0]), line.split(',')[1]))
order_itemsMap = order_items.map(lambda line: (int(line.split(',')[1]),
(int(line.split(',')[2]), float(line.split(',')[4])) ))
joined = orders_compMap.join(order_itemsMap)
joined2 = joined.map(lambda line: ((line[1][0], line[1][1][0]), line[1][1][1]))
joined3 = joined2.reduceByKey(lambda a, b : a +b).sortByKey()
df1 = joined3.map(lambda x:Row(date = x[0][0], product_id = x[0][1], total = x[1])).toDF().select(['date','product_id', 'total'])
schema = StructType([StructField('order_id', IntegerType(), True),
StructField('date', StringType(), True),
StructField('customer_id', StringType(), True),
StructField('status', StringType(), True)])
orders2 = spark.read.csv("retail_db/orders",schema = schema)
schema = StructType([StructField('item_id', IntegerType(), True),
StructField('order_id', IntegerType(), True),
StructField('product_id', IntegerType(), True),
StructField('quantity', StringType(), True),
StructField('sub_total', FloatType(), True),
StructField('product_price', FloatType(), True)])
orders_items2 = spark.read.csv("retail_db/order_items", schema = schema)
orders2.registerTempTable("orders2t")
orders_items2.registerTempTable("orders_items2t")
df2 = spark.sql('select o.date, oi.product_id, sum(oi.sub_total) \
as total from orders2t as o inner join orders_items2t as oi on
o.order_id = oi.order_id \
where o.status in ("CLOSED", "COMPLETE") group by o.date,
oi.product_id order by o.date, oi.product_id')
df1.registerTempTable("df1t")
df2.registerTempTable("df2t")
spark.sql("select d1.total - d2.total as difference from df1t as d1 inner
join df2t as d2 on d1.date = d2.date \
and d1.product_id =d2.product_id ").show(truncate = False)
最佳答案
忽略转换中的精度损失是不一样的。
Almost all machines today (November 2000) use IEEE-754 floating point arithmetic, and almost all platforms map Python floats to IEEE-754 “double precision”. 754 doubles contain 53 bits of precision,
FloatType
uses 32 bit representation :
FloatType
: Represents 4-byte single-precision floating point numbers.
DoubleType
可能更接近:
DoubleType
: Represents 8-byte double-precision floating point numbers.
DecimalTypes
具有明确定义的精度。
关于apache-spark - 与 RDD 和 DataFrame 不同的浮点精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48465055/
我是 Pyspark 新手,我使用的是 Spark 2.0.2。 我有一个名为 Test_RDD 的 RDD,其结构如下: U-Key || V1 || V2 || V3 || ----
我正在寻找一种方法将一个 RDD 拆分为两个或多个 RDD,并将获得的结果保存为两个单独的 RDD。例如: rdd_test = sc.parallelize(range(50), 1) 我的代码:
我有一个结构如下的RDD: ((user_id,item_id,rating)) 让我们将此 RDD 称为训练 然后还有另一个具有相同结构的rdd: ((user_id,item_id,rating)
已经有人问过类似的问题。最相似的是这个: Spark: How to split an RDD[T]` into Seq[RDD[T]] and preserve the ordering 但是,我不
我正在使用 spark 来处理数据。但是我不知道如何将新数据保存到Hive 我从 Hive 加载 rdd,然后运行 map 函数来清理数据。 result = myRdd.map(lambda x
我有一个名为 index 的 rdd:RDD[(String, String)],我想用 index 来处理我的文件。 这是代码: val get = file.map({x => val tmp
我有两个 RDD: **rdd1** id1 val1 id2 val2 **rdd2** id1 v1 id2 v2 id1 v3 id8 v7 id1 v4 id3 v5 id6 v6 我想过滤
我有一个 RDD,需要从另一个 RDD 访问数据。但是,我总是收到任务不可序列化错误。我已经扩展了 Serialized 类,但它没有起作用。代码是: val oldError = rddOfRati
我有一个 RDD,需要从另一个 RDD 访问数据。但是,我总是收到任务不可序列化错误。我已经扩展了 Serialized 类,但它没有起作用。代码是: val oldError = rddOfRati
我有一个 RDD 对: (105,918) (105,757) (502,516) (105,137) (516,816) (350,502) 我想将它分成两个 RDD,这样第一个只有具有非重复值的对
我正在尝试使用 spark 执行 K 最近邻搜索。 我有一个 RDD[Seq[Double]] 并且我打算返回一个 RDD[(Seq[Double],Seq[Seq[Double]])] 带有实际行和
我是Spark和Scala语言的新手,并且希望将所有RDD合并到一个List中,如下所示(List to RDD): val data = for (item {
我找不到只参与 rdd 的方法. take看起来很有希望,但它返回 list而不是 rdd .我当然可以将其转换为 rdd ,但这似乎既浪费又丑陋。 my_rdd = sc.textFile("my
我正在寻找一种将 RDD 拆分为两个或更多 RDD 的方法。我见过的最接近的是 Scala Spark: Split collection into several RDD?这仍然是一个单一的 RDD
我有一个RDD[String],wordRDD。我还有一个从字符串/单词创建 RDD[String] 的函数。我想为 wordRDD 中的每个字符串创建一个新的 RDD。以下是我的尝试: 1) 失败,
我刚刚开始使用 Spark 和 Scala 我有一个包含多个文件的目录我使用 成功加载它们 sc.wholeTextFiles(directory) 现在我想升一级。我实际上有一个目录,其中包含包含文
我想从另一个 RDD 中减去一个 RDD。我查看了文档,发现 subtract可以这样做。实际上,当我测试时 subtract , 最终的 RDD 保持不变,值不会被删除! 有没有其他功能可以做到这一
我在 HDFS 中有如下三个文件中的数据 EmployeeManagers.txt (EmpID,ManagerID) 1,5 2,4 3,4 4,6 5,6 EmployeeNames.txt (E
我正在开发一个应用程序,我需要对 RDD 中具有相同键的每对行执行计算,这是 RDD 结构: List>> dat2 = new ArrayList<>(); dat2.add(new Tuple2>
我在 spark 集群中有两个文件,foo.csv 和 bar.csv,它们都有 4 列和完全相同的字段:时间、用户、url、类别。 我想通过 bar.csv 的某些列过滤掉 foo.csv。最后,我
我是一名优秀的程序员,十分优秀!