让我们定义一个Spark管道,它将几列组合在一起,然后应用特征哈希:
val df = sqlContext.createDataFrame(Seq((0.0, 1.0, 2.0), (3.0, 4.0, 5.0))).toDF("colx", "coly", "colz")
val va = new VectorAssembler().setInputCols(Array("colx", "coly", "colz")).setOutputCol("ft")
val hashIt = new HashingTF().setInputCol("ft").setOutputCol("ft2")
val pipeline = new Pipeline().setStages(Array(va, hashIt))
Run Code Online (Sandbox Code Playgroud)
使用pipeline.fit(df)throws 安装管道:
java.lang.IllegalArgumentException:要求失败:输入列必须是ArrayType,但是得到了org.apache.spark.mllib.linalg.VectorUDT@f71b0bce
是否有允许VectorAssembler并HashingTF能够一起工作的变压器?
我有以下sparkdataframe:
id weekly_sale
1 40000
2 120000
3 135000
4 211000
5 215000
6 331000
7 337000
Run Code Online (Sandbox Code Playgroud)
我需要查看weekly_sale列中以下哪些间隔项属于:
under 100000
between 100000 and 200000
between 200000 and 300000
more than 300000
Run Code Online (Sandbox Code Playgroud)
所以我想要的输出将是:
id weekly_sale label
1 40000 under 100000
2 120000 between 100000 and 200000
3 135000 between 100000 and 200000
4 211000 between 200000 and 300000
5 215000 between 200000 and 300000
6 331000 more than 300000
7 337000 more than 300000
Run Code Online (Sandbox Code Playgroud)
任何pyspark,spark.sql和Hive上下文实现都将对我有所帮助。
如何替换类型为timestamp的列中的所有空值?
我希望这会更容易,但是我似乎无法正确获取类型。我认为一个解决方案是将列转换为String,在字符串中填充今天的日期,然后重新转换为timestamp,但是,还有没有更优雅的解决方案?
val today = java.time.LocalDate.now()
var todayStamp = java.sql.Timestamp.valueOf(today.atStartOfDay());
df = df.na.fill(Map("expiration" -> todayStamp))
Run Code Online (Sandbox Code Playgroud)
结果是
java.lang.IllegalArgumentException: Unsupported value type java.sql.Timestamp
Run Code Online (Sandbox Code Playgroud)
使用今天也不起作用,并且使用unix_timestamp(string).cast("timestamp")
期望列而不是字符串。我想我可以在上面提到的“丑陋”方法中使用它。
稍后编辑:忘了提及,在timestamp列上将Int或String与df.na.fill方法一起使用也会导致错误:
org.apache.spark.sql.AnalysisException: cannot resolve 'coalesce(expiration, 0)' due to data type mismatch: input to function coalesce should all be the same type, but it's [timestamp, int];
Run Code Online (Sandbox Code Playgroud) 我创建了一个类"属性".每个类对象都有一个名称,一个数据类型和一个布尔值(可以为空或可以为空).所有对象都保存到ListBuffer中.
我尝试从列表中创建一个模式,并将每个值传递给StructField().启动工作,但遗憾的是,没有其他项目添加到架构中.
def create_schema_from_attr_list(attr_list: ListBuffer[Attribute]): StructType = {
// Get first list item and initiate schema
var schema = StructType(StructField(attr_list(0).name, attr_list(0).data_type, attr_list(0).nullable) :: Nil)
// Add remaining items
for (i <- 1 until attr_list.length) {
schema.add(attr_list(i).name, attr_list(i).data_type, attr_list(i).nullable)
println("Test " + attr_list(i).name.toString())
}
return schema
}
Run Code Online (Sandbox Code Playgroud) 我有一些表需要掩盖其某些列。每个表要屏蔽的列各不相同,我正在从application.conf文件中读取这些列。
例如,对于雇员表,如下所示
+----+------+-----+---------+
| id | name | age | address |
+----+------+-----+---------+
| 1 | abcd | 21 | India |
+----+------+-----+---------+
| 2 | qazx | 42 | Germany |
+----+------+-----+---------+
Run Code Online (Sandbox Code Playgroud)
如果我们要屏蔽名称和年龄列,那么我将按顺序获取这些列。
val mask = Seq("name", "age")
Run Code Online (Sandbox Code Playgroud)
屏蔽后的期望值为:
+----+----------------+----------------+---------+
| id | name | age | address |
+----+----------------+----------------+---------+
| 1 | *** Masked *** | *** Masked *** | India |
+----+----------------+----------------+---------+
| 2 | *** Masked *** | *** Masked *** | Germany | …Run Code Online (Sandbox Code Playgroud) 我知道可以使用以下内容将数据帧列转换为列表:
dataFrame.select("ColumnName").rdd.map(r => r(0)).collect()
Run Code Online (Sandbox Code Playgroud)
假设我已经知道了数据帧的模式,相应地我创建了一个case类,例如:
case class Synonym(URI: String, similarity: Double, FURI: String)
Run Code Online (Sandbox Code Playgroud)
有没有一种有效的方法从数据帧的数据中获取同义词对象列表?
换句话说,我正在尝试创建一个映射器,它将数据帧的每一行转换为我的case类的一个对象,然后以一种我可以在操作结束时拥有这些对象列表的方式返回该对象.这有可能以一种有效的方式吗?
我正在尝试使用Java API从DataFrame中选择列列表.
示例Java代码:
List<String> colList = Arrays.asList(new String[] { "column1", "column2", "column3" });
df.selectExpr((String[])colList.toArray()).show();
Run Code Online (Sandbox Code Playgroud)
在Java API中,我必须使用selectExpr而不是select.有没有其他方法可以使用Java API选择列列表.
但是在Scala中,我可以做类似下面的事情.
示例Scala代码:
val colList = List("column1", "column2", "column3")
df.select(colList.head, colList.tail: _*).show
Run Code Online (Sandbox Code Playgroud) 我RDD[Long]打电话mod,我想使用Spark 2.2和Scala 2.11.8计算此RDD的标准偏差和平均值.
我该怎么做?
我尝试按如下方式计算平均值,但有没有更简单的方法来获取这些值?
val avg_val = mod.toDF("col").agg(
avg($"col").as("avg")
).first().toString().toDouble
val stddev_val = mod.toDF("col").agg(
stddev($"col").as("avg")
).first().toString().toDouble
Run Code Online (Sandbox Code Playgroud) 我有两个DataFrames推荐和电影.建议中的列rec1-rec3表示电影数据帧中的电影ID.
val recommendations: DataFrame = List(
(0, 1, 2, 3),
(1, 2, 3, 4),
(2, 1, 3, 4)).toDF("id", "rec1", "rec2", "rec3")
val movies = List(
(1, "the Lord of the Rings"),
(2, "Star Wars"),
(3, "Star Trek"),
(4, "Pulp Fiction")).toDF("id", "name")
Run Code Online (Sandbox Code Playgroud)
我想要的是:
+---+------------------------+------------+------------+
| id| rec1| rec2| rec3|
+---+------------------------+------------+------------+
| 0| the Lord of the Rings| Star Wars| Star Trek|
| 1| Star Wars| Star Trek|Pulp Fiction|
| 2| the Lord of the Rings| Star Trek| Star Trek|
+---+------------------------+------------+------------+
Run Code Online (Sandbox Code Playgroud) 我们可以使用以下方法从Java对象列表中创建一个数据框:
DataFrame df = sqlContext.createDataFrame(list, Example.class);
Run Code Online (Sandbox Code Playgroud)
对于Java,Spark可以直接从类(在这种情况下)推断模式Example.class。
如果有Scala,有没有办法做同样的事情?
我有一个包含这么多列的数据集,我想使用Java将所有列都转换为字符串。
我尝试了以下步骤,我想知道是否有更好的方法来实现这一目标?
Dataset<Row> ds = ...;
JavaRDD<String[]> stringArrRDD = ds.javaRDD().map(row->{
int length = row.length();
String[] columns = new String[length];
for(int i=0; i<length;i++){
columns[i] = row.get(i) !=null? row.get(i).toString():"";
}
return columns;});
Run Code Online (Sandbox Code Playgroud) 我有一个数据框,并希望删除所有括号,并替换为两个连字符.
之前:
+------------+
| dob_concat|
+------------+
|[1983][6][3]|
+------------+
Run Code Online (Sandbox Code Playgroud)
后:
+------------+
| dob_concat |
+------------+
| 1983-6-3 |
+------------+
Run Code Online (Sandbox Code Playgroud) apache-spark-sql ×12
apache-spark ×11
scala ×7
dataframe ×4
java ×2
pyspark ×2
databricks ×1
hivecontext ×1
loops ×1
oop ×1