在闪烁2.0.1( hadoop2.6.0 )中,有许多文件用'!@!\r‘分隔,而不是用通常的新行\n分隔,例如:
=========================================
2001810086 rongq 2001 810!@!
2001810087 hauaa 2001 810!@!
2001820081 hello 2001 820!@!
2001820082 jaccy 2001 820!@!
2002810081 cindy 2002 810!@!
=========================================我尝试根据Setting textinputformat.record.delimiter in spark set textinputformat.record.delimiter='!@!\r';或set textinputformat.record.delimiter='!@!\n‘提取数据,但仍然不能提取数据
在spark中,我执行以下操作:===== ================================
create table ceshi(id int,name string, year string, major string)
row format delimited
fields terminated by '\t';
load data local inpath '/data.txt' overwrite into table ceshi;
select count(*) from ceshi;结果是5,但我尝试set textinputformat.record.delimiter='!@!\r',然后select count(*) from ceshi;的结果是1,分隔符不能很好地工作;
我还检查了hadoop2.6.0的来源,即RecordReader在TextInputFormat.java中的方法,我注意到默认的textinputformat.record.delimiter为null,然后LineReader.java使用readDefaultLine方法读取由CR、LF或CRLF(CR ='\r',LF ='\n')之一结束的一行。
发布于 2017-07-23 02:31:04
您应该使用sparkContext的hadoopConfiguration api将textinputformat.record.delimiter设置为
sc.hadoopConfiguration.set("textinputformat.record.delimiter", "!@!\r")然后,如果使用sparkContext读取文本文件,则为
sc.textFile("the input file path")你应该好好的。
更新的
我注意到,保存时带有分隔符\r的文本文件将更改为\n分隔符。
所以,下面的格式应该适用于您,就像对我一样
sc.hadoopConfiguration.set("textinputformat.record.delimiter", "!@!\n")
val data = sc.textFile("the input file path")
val df = data.map(line => line.split("\t"))
.map(array => ceshi(array(0).toInt, array(1), array(2), array(3)))
.toDF名为case clas的ceshi是需要的
case class ceshi(id: Int, name: String, year: String, major :String)它应该将数据作为
+----------+-----+-----+-----+
|id |name |year |major|
+----------+-----+-----+-----+
|2001810086|rongq| 2001|810 |
|2001810087|hauaa| 2001|810 |
|2001820081|hello| 2001|820 |
|2001820082|jaccy| 2001|820 |
|2002810081|cindy| 2002|810 |
+----------+-----+-----+-----+现在,您可以按count函数作为
import org.apache.spark.sql.functions._
df.select(count("*")).show(false)这将使输出成为
+--------+
|count(1)|
+--------+
|5 |
+--------+https://stackoverflow.com/questions/45250048
复制相似问题