地图功能遇到问题,它在控制台日志中显示数据
#first I read in the data
rdd = spark.sparkContext.textFile(MessyFile)
#the first line is expected to have the correct number of columns (no linebreaks within a column)
cols = len(rdd.first().split("~#~"))
#I store the already "correct" rows in one RDD, and the incorrect ones in a different RDD
correct = rdd.filter(lambda x: len(x.split("~#~"))==cols)
wrong = rdd.filter(lambda x: len(x.split("~#~"))!=cols)
#The incorrect rows are now so small that they will fit in memory, so I can make RDD into an iterable list
fix = iter(wrong.collect())
fixed = []
#I then iterate over all the rows in the incorrect list and add next row until the row has the expected number of columns, and I add ". " to indicate where there was a linebreak
#The new rows are added to a new list called fixed
for item in fix:
row = item
while len(row.split("~#~"))!=cols:
row+='. '+next(fix)
fixed.append(row)
#I then union the already correct rows with the newly fixed rows
new = correct.union(spark.sparkContext.parallelize(fixed)) \
.map(lambda row: row.split("~#~"))
#I then create a dataframe, assing the first row as header and write it out as a parquet file
header = new.first()
df = new.filter(lambda line: line != header).toDF()
oldcols = df.columns
df = reduce(lambda df, idx:df.withColumnRenamed(oldcols[idx],header[idx]),range(len(oldcols)),df)
df.coalesce(10).write.parquet(CleanFile,mode='overwrite')
但是当我使用地图功能console.log("iiiiiiiiii",this.props.rsl['data'])
时,它抛出一个错误提示
它抛出错误,提示TypeError:无法读取属性'map' 未定义
{this.props.rsl['data'].map(row=>{})
数据未呈现,因为其未映射,任何人都可以指导我解决问题。
答案 0 :(得分:1)
初始页面加载时,rsl
不确定,您可以通过以下方式解决此问题
{this.props.rsl && this.props.rsl['data'].map(row => { ........... })
答案 1 :(得分:0)
我正在使用的React应用程序遇到了同样的问题。我不确定我为什么理解为什么,但是当我尝试在其上进行映射之前为该数组赋予空白数组的默认值时,我能够进行映射其内容在我的组件中。
在我的示例中,原始任务如下:
let { img, title, description, games } = this.state.eventData;
然后,我试图像这样映射游戏数组:
<h3>Games:</h3>
<ul>
{games.map(g => <li>{g.name}</li> )}
</ul>
尝试上面的代码时,我从eventData
分配
对象,并且未设置默认值,我收到一条错误消息,提示未定义games
。
但是当我将其更改为此时,它起作用了:
let { img, title, description, games=[] } = this.state.eventData;
<h3>Games:</h3>
<ul>
{games.map(g => <li>{g.name}</li> )}
</ul>
希望这对您有帮助,和/或将来的人们可以使用此答案!