/ / स्पार्कसक्ल में textinputformat.record.delimiter सेट करना

Sparksql में textinputformat.record.delimiter सेट करना - हडूप, अपाचे-स्पार्क, अपाचे-स्पार्क-एसक्यूएल

स्पार्क २.०.१, हडूप २.६.० में, मेरे पास कई फाइलें हैं! "

=========================================

2001810086  rongq   2001    810!@!
2001810087  hauaa   2001    810!@!
2001820081  hello   2001    820!@!
2001820082  jaccy   2001    820!@!
2002810081  cindy   2002    810!@!

=========================================

मैं के अनुसार डेटा निकालने की कोशिश करता हूं स्पार्क में textinputformat.record.delimiter सेट करना set textinputformat.record.delimiter="!@!r";या set textinputformat.record.delimiter="!@!n", लेकिन अभी भी डेटा नहीं निकाला जा सकता है

स्पार्क-एसक्यूएल में, मैं यह करता हूं: ========================================

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, TextInputFormat.java में RecordReader की विधि, मैं ध्यान देता हूं कि डिफ़ॉल्ट textinputformat.record.delimiter शून्य है, फिर LineReader.java पद्धति का उपयोग करता है readDaultLine CR, LF, या CRLF (CR =) में से किसी एक पंक्ति को पढ़ने के लिए। "आर", एलएफ = "एन")।

उत्तर:

जवाब के लिए 0 № 1

आपको उपयोग करना चाहिए sparkContext"रों hadoopConfiguration एपीआई सेट करने के लिए 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       |
+--------+