apache spark - java.lang.NumberFormatException: For input string: "" while creating Dataframe -


i using cloudera vm. have imported products table retail_db textfile '|' fields separator (using sqoop).

following table schema:

mysql> describe products; product_id: int(11) product_category_id: int(11) product_name: varchar(45) product_description: varchar(255) product_price: float product_image: varchar(255) 

i want create dataframe data.

i got no issue while using following code:

var products = sc.textfile("/user/cloudera/ex/products").map(r => {var p = r.split('|'); (p(0).toint, p(1).toint, p(2), p(3), p(4).tofloat, p(5))}) case class products(productid: int, productcategory: int, productname: string, productdescription: string, productprice: float, productimage: string) var productsdf = products.map(r => products(r._1, r._2, r._3, r._4, r._5, r._6)).todf()  productsdf.show() 

but got numberformatexception exception following code:

case class products (product_id: int, product_category_id: int, product_name: string, product_description: string, product_price: float, product_image: string) val productsdf = sc.textfile("/user/cloudera/ex/products").map(_.split("|")).map(p => products(p(0).trim.toint, p(1).trim.toint, p(2), p(3), p(4).trim.tofloat, p(5))).todf() productsdf.show() 

java.lang.numberformatexception: input string: ""

why getting exception in 2nd code though same of 1st one?

the error due _.split("|") in second part of code

you need use _.split('|') or _.split("\\|") or _.split("""\|""") or pattern.quote("|")

if use "|" tries split regular expression , | or in regular expression, not matches , returns empty string ""

hope helps!


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -