当我运行以下代码时,我正在使用spark 1.6并遇到上述问题:
// Imports
import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SaveMode
import scala.concurrent.ExecutionContext.Implicits.global
import java.util.Properties
import scala.concurrent.Future
// Set up spark on local with 2 threads
val conf = new SparkConf().setMaster("local[2]").setAppName("app")
val sc = new SparkContext(conf)
val sqlCtx = new HiveContext(sc)
// Create fake dataframe
import sqlCtx.implicits._
var df = sc.parallelize(1 to 50000).map { i => (i, i, i, i, i, i, i) }.toDF("a", "b", "c", "d", "e", "f", "g").repartition(2)
// Write it as a parquet file
df.write.parquet("/tmp/parquet1")
df = …Run Code Online (Sandbox Code Playgroud) 我有一个PUT请求,我想更新我的中间件中的一些参数的值.我知道没有办法直接访问PUT参数,所以我通过它访问它们request.body.
一旦更新了这些值,我需要将其传递request给视图.但是,如果我尝试这样做:
request.body = new_content
Run Code Online (Sandbox Code Playgroud)
在我的中间件中,我得到:
AttributeError: can't set attribute
Run Code Online (Sandbox Code Playgroud)
有没有办法更新中间件中的这些参数并传递它们?
我有一个换行符分隔的 json 文件,看起来像
{"id":1,"nested_col": {"key1": "val1", "key2": "val2", "key3": ["arr1", "arr2"]}}
{"id":2,"nested_col": {"key1": "val1_2", "key2": "val2_2", "key3": ["arr1_2", "arr2"]}}
Run Code Online (Sandbox Code Playgroud)
一旦我使用 读取文件df = spark.read.json(path_to_file),我最终会得到一个数据框,其架构如下所示:
DataFrame[id: bigint,nested_col:struct<key1:string,key2:string,key3:array<string>>]
Run Code Online (Sandbox Code Playgroud)
我想要做的是nested_col在不设置primitivesAsString为 true 的情况下将其转换为字符串(因为我实际上有 100 多列并且需要推断所有其他列的类型)。我也不知道nested_col之前的样子。换句话说,我希望自己DataFrame看起来像
DataFrame[id: bigint,nested_col:string]
Run Code Online (Sandbox Code Playgroud)
我试着做
df.select(df['nested_col'].cast('string')).take(1)`
Run Code Online (Sandbox Code Playgroud)
但它不会返回 JSON 的正确字符串表示形式:
{"id":1,"nested_col": {"key1": "val1", "key2": "val2", "key3": ["arr1", "arr2"]}}
{"id":2,"nested_col": {"key1": "val1_2", "key2": "val2_2", "key3": ["arr1_2", "arr2"]}}
Run Code Online (Sandbox Code Playgroud)
而我希望:
DataFrame[id: bigint,nested_col:struct<key1:string,key2:string,key3:array<string>>]
Run Code Online (Sandbox Code Playgroud)
有谁知道我如何获得所需的结果(也就是将嵌套的 JSON 字段/StructType转换为字符串)?
apache-spark ×2
scala ×2
dataframe ×1
django ×1
httprequest ×1
json ×1
middleware ×1
python ×1