- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个我正在尝试读入的数值数据文件,数据如下所示:
1 6 4 12 5 5 3 4 1 67 3 2 1 2 1 0 0 1 0 0 1 0 0 1 1
2 48 2 60 1 3 2 2 1 22 3 1 1 1 1 0 0 1 0 0 1 0 0 1 2
由 3 个空格分隔。我想把它放在 Spark DataFrame 中。
我正在努力解析它,它似乎将每一行都读作一个大字符串。
我厌倦了以下;
Dataset<Row> df = spark.read().format("com.databricks.spark.csv")
.option("header", "false")
.option("delimter", "\t")
.load(csvFile);
df.show(5);
还有:
.option("delimter", " ") // leads to java error that Delimter cant take more than one character
也厌倦了 .option("sep", "\t")
而不是 "delimter"
:
这是我的完整代码:
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
public class CreditRiskML {
static SparkSession spark = SparkSession.builder()
.appName("Credit Risk ML")
.master("local[*]")
.config("spark.sql.warehouse.dir", "E:/Exp/")
.getOrCreate();
public static double parseDouble(String str){
return Double.parseDouble(str);
}
public static void main(String[] args){
String csvFile = "input\\credit.data";
Dataset<Row> df = spark.read().format("com.databricks.spark.csv")
.option("header", "false")
.option("delimter", "\t")
.option("sep", "\t")
.load(csvFile);
df.show(5);
//create RDD of type Credit
JavaRDD<Credit> creditRdd = df.toJavaRDD().map(new Function<Row, Credit>() {
@Override
public Credit call(Row r) throws Exception {
return new Credit(parseDouble(r.getString(0)), parseDouble(r.getString(1)) - 1,
parseDouble(r.getString(2)), parseDouble(r.getString(3)), parseDouble(r.getString(4)),
parseDouble(r.getString(5)), parseDouble(r.getString(6)) - 1, parseDouble(r.getString(7)) - 1,
parseDouble(r.getString(8)), parseDouble(r.getString(9)) - 1, parseDouble(r.getString(10)) - 1,
parseDouble(r.getString(11)) - 1, parseDouble(r.getString(12)) - 1,
parseDouble(r.getString(13)), parseDouble(r.getString(14)) - 1,
parseDouble(r.getString(15)) - 1, parseDouble(r.getString(16)) - 1,
parseDouble(r.getString(17)) - 1, parseDouble(r.getString(18)) - 1,
parseDouble(r.getString(19)) - 1, parseDouble(r.getString(20)) - 1);
}
});
//Create a dataset of type Row from the RDD of type Credit
Dataset<Row> creditData = spark.sqlContext().createDataFrame(creditRdd, Credit.class);
creditData.show(5);
}
}
错误信息:
java.lang.NumberFormatException: For input string: "1 6 4 12 5 5 3 4 1 67 3 2 1 2 1 0 0 1 0 0 1 0 0 1 1"
解决这个问题的最佳方法是什么?非常感谢任何帮助。
P.s 这里是信用等级:
public class Credit {
private double creditability;
private double balance;
private double duration;
private double history;
private double purpose;
private double amount;
private double savings;
private double employment;
private double instPercent;
private double sexMarried;
private double guarantors;
private double residenceDuration;
private double assets;
private double age;
private double concCredit;
private double apartment;
private double credits;
private double occupation;
private double dependents;
private double hasPhone;
private double foreign;
public Credit(double creditability, double balance, double duration, double history, double purpose, double amount,
double savings, double employment, double instPercent, double sexMarried, double guarantors,
double residenceDuration, double assets, double age, double concCredit, double apartment, double credits,
double occupation, double dependents, double hasPhone, double foreign) {
super();
this.creditability = creditability;
this.balance = balance;
this.duration = duration;
this.history = history;
this.purpose = purpose;
this.amount = amount;
this.savings = savings;
this.employment = employment;
this.instPercent = instPercent;
this.sexMarried = sexMarried;
this.guarantors = guarantors;
this.residenceDuration = residenceDuration;
this.assets = assets;
this.age = age;
this.concCredit = concCredit;
this.apartment = apartment;
this.credits = credits;
this.occupation = occupation;
this.dependents = dependents;
this.hasPhone = hasPhone;
this.foreign = foreign;
}
public double getCreditability() {
return creditability;
}
public void setCreditability(double creditability) {
this.creditability = creditability;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getDuration() {
return duration;
}
public void setDuration(double duration) {
this.duration = duration;
}
public double getHistory() {
return history;
}
public void setHistory(double history) {
this.history = history;
}
public double getPurpose() {
return purpose;
}
public void setPurpose(double purpose) {
this.purpose = purpose;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public double getSavings() {
return savings;
}
public void setSavings(double savings) {
this.savings = savings;
}
public double getEmployment() {
return employment;
}
public void setEmployment(double employment) {
this.employment = employment;
}
public double getInstPercent() {
return instPercent;
}
public void setInstPercent(double instPercent) {
this.instPercent = instPercent;
}
public double getSexMarried() {
return sexMarried;
}
public void setSexMarried(double sexMarried) {
this.sexMarried = sexMarried;
}
public double getGuarantors() {
return guarantors;
}
public void setGuarantors(double guarantors) {
this.guarantors = guarantors;
}
public double getResidenceDuration() {
return residenceDuration;
}
public void setResidenceDuration(double residenceDuration) {
this.residenceDuration = residenceDuration;
}
public double getAssets() {
return assets;
}
public void setAssets(double assets) {
this.assets = assets;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
public double getConcCredit() {
return concCredit;
}
public void setConcCredit(double concCredit) {
this.concCredit = concCredit;
}
public double getApartment() {
return apartment;
}
public void setApartment(double apartment) {
this.apartment = apartment;
}
public double getCredits() {
return credits;
}
public void setCredits(double credits) {
this.credits = credits;
}
public double getOccupation() {
return occupation;
}
public void setOccupation(double occupation) {
this.occupation = occupation;
}
public double getDependents() {
return dependents;
}
public void setDependents(double dependents) {
this.dependents = dependents;
}
public double getHasPhone() {
return hasPhone;
}
public void setHasPhone(double hasPhone) {
this.hasPhone = hasPhone;
}
public double getForeign() {
return foreign;
}
public void setForeign(double foreign) {
this.foreign = foreign;
}
}
最佳答案
解决这个问题的一种方法是使用 java.util.Scanner
。因为您使用的是空格,所以不需要指定分隔符。
String s = "1 0 2 0";
Scanner scanner = new Scanner(s);
while(scanner.hasNext()){
System.out.println(scanner.next());
}
输出将是:
1
0
2
0
无论给定字符串中的空格数量如何,这都有效。
关于java - 使用 Java 中的 Sparks csv 阅读器加载以 3 个空格作为分隔符的数据文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43678852/
使用 ListView.separated 我们可以在列表项之间添加 Divider(),但是,一旦我转换到 SliverList,我就看不到我的分隔线了。 delegate: SliverChild
使用 ListView.separated 我们可以在列表项之间添加 Divider(),但是,一旦我转换到 SliverList,我就看不到我的分隔线了。 delegate: SliverChild
我对 Angular 还很陌生。我有一个由一些数据填充的列表项: {{content.Company}} {{content.Town}}, {{content.P
我正在尝试从 SwiftUI 中的 List 中删除“行”分隔符(在 SwiftUI 中称为分隔符)。 我浏览了 List 文档,但我没能找到它的修饰符。 如有任何帮助,我们将不胜感激。 最佳答案 i
我有一个带有 4 个按钮的网格...1 行 4 列。我正在寻找一种方法将左侧的两个按钮与右侧的两个按钮进行视觉分组。我一直在寻找一种使用分隔符执行此操作的方法,但它似乎与 Grid 一起玩得不好,更喜
我对 R 语言相当陌生。所以我有这个包含以下内容的向量: > head(sampleVector) [1] "| txt01 | 100 | 200 | 123.456
我正在尝试连接两列中的值,当我使用 =CONCAT(A2,",",B2) 时,它将连接两列并获得正确的结果 (P0810,P1)。但我正在寻找的是这样的东西(“P0810”,“P1”)。我尝试了 =C
我在这里创建了一个简单的演示。在 amount 字段编辑时,我想显示 , 分隔符?目前,它仅在不处于编辑模式时显示 ,。知道如何实现这一目标吗? DEMO IN DOJO var data = [{
这里是java菜鸟... 这让我抓狂,因为我知道这很简单,但我已经为此工作了 30 分钟...... 这是来自代码战斗: 对于参数 = ["Code", "Fight", "On", "!"] 且分隔
基于这个pywin32基础script如何向托盘菜单 menu_options 添加分隔符? 我还可以让菜单在左键单击时弹出,而不仅仅是右键单击吗? 最佳答案 将 notify 函数(从 URL 中的
我正在使用这段代码: StringTokenizer tokenizer=new StringTokenizer(line, "::"); 拆分以下字符串: hi my name is visghal
- Dropbox login fix - Updated iris viewer * other aspects are to be improved + fix crash on viewing
我试图在每个菜单组之间显示一个分隔线。我已经尝试过为每个组提供一个唯一的 ID,但这没有用。我找到了一些其他解决方案,但它们看起来有点奇怪,比如创建高度为 1dp 的 LinearLayout。 这是
我想为 CONCAT_WS() 选择一个与字段值不冲突的分隔符例如,如果我选择“,”,则字段值可能包含带有“,”的字符串我想选择一个与字段值不冲突的分隔符:( 最佳答案 来自here : CONCAT
我想知道 Sphinx 引擎是否可以使用任何定界符(如普通 MySQL 中的逗号和句点)。我的问题来自于一种冲动,根本不使用它们,而是逃避它们,或者至少在使用 FULLTEXT 搜索执行 MATCH
我正在尝试使用 svg 或纯 css3 制作 header 分隔符,如下所示: preview from design 在 header 中我有标准的 bootstrap 4 轮播
我在使用 CSS 分隔符时遇到了一些难题。看看:http://jsfiddle.net/fVxC6/1/ .div-line { border-bottom: 1px solid #f0f0f
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 7 年前。 编辑问题以包含 desired behavior, a specific probl
嘿,我正在尝试使用 getline 读取以下行 (15,0,1,#) (2,11,2,.) (3,20,0,S) 我希望能够将整数提取为 int,将字符提取为 char,但我不知道如何只提取它们。 最
我有 2 列,每边 float 一列,我想使用 1px 宽度的线分隔符,从最长列的顶部到底部。 我宁愿远离 TABLE 布局,而且我不知道哪一个将是最长的列,或者它会有多长。 我怎么能只用 css 做
我是一名优秀的程序员,十分优秀!