请忍受我的英语.
我有一张这样的桌子,
id | category_id | product_id
-----------------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 2 | 3
5 | 1 | 4
6 | 3 | 5
Run Code Online (Sandbox Code Playgroud)
我想要输出,
id | category_id | product_id
----------------------------------
1 | 1 | 1
3 | 2 | 1
6 | 3 | 5
2 | 1 | 2
4 | 2 | 3
5 | 1 | 4
Run Code Online (Sandbox Code Playgroud)
所以简而言之,我需要的是,category_id必须按顺序排列,以便像1, 2, …
我有一个 json 文件:
{
"a": {
"b": 1
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试阅读它:
val path = "D:/playground/input.json"
val df = spark.read.json(path)
df.show()
Run Code Online (Sandbox Code Playgroud)
但是得到一个错误:
线程“main”org.apache.spark.sql.AnalysisException 中的异常:自 Spark 2.3 起,当引用的列仅包含内部损坏的记录列(默认名为 _corrupt_record)时,不允许对原始 JSON/CSV 文件进行查询。例如: spark.read.schema(schema).json(file).filter($"_corrupt_record".isNotNull).count() 和 spark.read.schema(schema).json(file).select("_corrupt_record" )。展示()。相反,您可以缓存或保存解析的结果,然后发送相同的查询。例如, val df = spark.read.schema(schema).json(file).cache() 然后 df.filter($"_corrupt_record".isNotNull).count().;
所以我尝试按照他们的建议缓存它:
val path = "D:/playground/input.json"
val df = spark.read.json(path).cache()
df.show()
Run Code Online (Sandbox Code Playgroud)
但我不断收到同样的错误。
我正在尝试使用解析日期,to_date()但出现以下异常。
SparkUpgradeException: 由于 Spark 3.0 升级,您可能会得到不同的结果:Fail to parse '12/1/2010 8:26' in the new parser。您可以将 spark.sql.legacy.timeParserPolicy 设置为 LEGACY 以恢复 Spark 3.0 之前的行为,或者设置为 CORRECTED 并将其视为无效的日期时间字符串。
例外情况表明我应该使用旧版时间解析器,首先我不知道如何将其设置为旧版。
这是我的实现
dfWithDate = df.withColumn("date", to_date(col("InvoiceDate"), "MM/dd/yyyy"))
Run Code Online (Sandbox Code Playgroud)
我的日期采用以下格式
+--------------+
| InvoiceDate|
+--------------+
|12/1/2010 8:26|
|12/1/2010 8:26|
|12/1/2010 8:26|
|12/1/2010 8:26|
|12/1/2010 8:26|
Run Code Online (Sandbox Code Playgroud) 我有一个功能可以让我从客户端应用程序查询数据块增量表。这是我用于此目的的代码:
df = spark.sql('SELECT * FROM EmployeeTerritories LIMIT 100')
dataframe = df.toPandas()
dataframe_json = dataframe.to_json(orient='records', force_ascii=False)
Run Code Online (Sandbox Code Playgroud)
但是,第二行抛出了错误
从时间戳[us, tz=Etc/UTC] 转换为时间戳[ns] 将导致时间戳超出范围
我知道这个错误是什么意思,我的日期类型字段超出范围,我尝试寻找解决方案,但它们都不适合我的场景。
我找到的解决方案是关于特定的数据框列,但就我而言,我遇到了一个全局问题,因为我有大量的增量表,并且我不知道特定的日期类型列,所以我可以进行类型操作以避免这种情况。
是否可以找到所有Timestamp类型列并将它们转换为string?这看起来是一个很好的解决方案吗?对于如何实现我想要做的事情,您还有其他想法吗?
我有一个非常简单的Mlab帐户,我正在尝试使用此命令通过命令行连接:
mongo server_name.mlab.com:port/inventory -u <dbuser> -p <dbpassword>
Run Code Online (Sandbox Code Playgroud)
但我一直遇到这个问题:
2016-05-26T15:41:18.195 + 0200错误:18 {ok:0.0,errmsg:"auth failed",代码:18}在src/mongo/shell/db.js:1210异常:登录失败
如何摆脱这种情况以及如何轻松连接到mlab?
我有一个数据框(df)。为了显示其架构,我使用:
from pyspark.sql.functions import *
df1.printSchema()
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
#root
# |-- name: string (nullable = true)
# |-- age: long (nullable = true)
Run Code Online (Sandbox Code Playgroud)
有时架构会发生变化(列类型或名称):
df2.printSchema()
#root
# |-- name: array (nullable = true)
# |-- gender: integer (nullable = true)
# |-- age: long (nullable = true)
Run Code Online (Sandbox Code Playgroud)
我想比较两个模式(df1和df2)并仅获取类型和列名称的差异(有时列可以移动到另一个位置)。结果应该是一个类似这样的表格(或数据框):
column df1 df2 diff
name: string array type
gender: N/A integer new column
Run Code Online (Sandbox Code Playgroud)
(age列是相同的并且没有改变。如果省略列,将会有指示'omitted')如果每个列中有很多列,我该如何有效地做到这一点?
是否有推荐的方法在 pyspark 中实现分类数据的自定义排序?我理想地寻找 pandas 分类数据类型提供的功能。
因此,给定一个带有Speed列的数据集,可能的选项是["Super Fast", "Fast", "Medium", "Slow"]。我想实现适合上下文的自定义排序。
如果我使用默认排序,类别将按字母顺序排序。Pandas 允许将列数据类型更改为分类,并且定义的一部分给出了自定义排序顺序:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Categorical.html
我正在研究spark-core,发现了一个未记录的配置,即spark.executor.allowSparkContext自3.0.1 起可用。我无法在 Spark 官方文档中找到详细信息。在代码中,有对此配置的简短描述
如果设置为 true,则可以在执行器中创建 SparkContext。
但我想知道,如何SparkContext在执行器中创建?据我所知,SparkContext是在驱动程序上创建的,执行程序是由资源管理器分配的。所以SparkContext总是在执行者之前创建。此配置的用例是什么?
我有一个如下所示的数据集:
| 纪元秒数 | 等值时间 |
|---|---|
| 1636663343887 | 2021-11-12 02:12:23 |
现在,我试图将 转换eq_time为epoch秒,它应该与第一列的值匹配,但我无法这样做。下面是我的代码:
df = spark.sql("select '1636663343887' as epoch_seconds")
df1 = df.withColumn("eq_time", from_unixtime(col("epoch_seconds") / 1000))
df2 = df1.withColumn("epoch_sec", unix_timestamp(df1.eq_time))
df2.show(truncate=False)
Run Code Online (Sandbox Code Playgroud)
我得到如下输出:
| 纪元秒数 | 等值时间 | 纪元秒 |
|---|---|---|
| 1636663343887 | 2021-11-12 02:12:23 | 1636663343 |
我也尝试过这个链接,但没有帮助。我的expected输出是第一列和第三列应该相互匹配。
PS:我Spark 3.1.1在本地使用该版本,而它是Spark 2.4.3在生产中,我的最终目标是在生产中运行它。
我正在尝试texture features使用带有C++的ITK库计算分段式3D脑MRI .所以我按照这个例子.该示例采用a 3D image,并为所有13个可能的空间方向提取3个不同的特征.在我的程序中,我只想获得给定的3D图像:
这是我到目前为止:
//definitions of used types
typedef itk::Image<float, 3> InternalImageType;
typedef itk::Image<unsigned char, 3> VisualizingImageType;
typedef itk::Neighborhood<float, 3> NeighborhoodType;
typedef itk::Statistics::ScalarImageToCooccurrenceMatrixFilter<InternalImageType>
Image2CoOccuranceType;
typedef Image2CoOccuranceType::HistogramType HistogramType;
typedef itk::Statistics::HistogramToTextureFeaturesFilter<HistogramType> Hist2FeaturesType;
typedef InternalImageType::OffsetType OffsetType;
typedef itk::AddImageFilter <InternalImageType> AddImageFilterType;
typedef itk::MultiplyImageFilter<InternalImageType> MultiplyImageFilterType;
void calcTextureFeatureImage (OffsetType offset, InternalImageType::Pointer inputImage)
{
// principal variables
//Gray Level Co-occurance Matrix Generator
Image2CoOccuranceType::Pointer glcmGenerator=Image2CoOccuranceType::New();
glcmGenerator->SetOffset(offset);
glcmGenerator->SetNumberOfBinsPerAxis(16); //reasonable number of bins
glcmGenerator->SetPixelValueMinMax(0, …Run Code Online (Sandbox Code Playgroud) apache-spark ×7
pyspark ×6
python ×3
pandas ×2
c++ ×1
databricks ×1
itk ×1
json ×1
mlab ×1
mongodb ×1
mysql ×1
scala ×1
shell ×1
spark3 ×1
sql-order-by ×1