我想从一行中的数据库中检索多个列。
实施例: -
CustomerId,AccountNumber
1,AW0000001
我写的Scala代码正在将列值吐出到新行。
实施例: -
1
AW0000001
代码: -
object SparkSQLServer {
class DbRow extends java.util.HashMap[java.lang.String,Object] {
}
def main(args: Array[String]): Unit = {
Logger.getLogger("org").setLevel(Level.WARN)
Logger.getLogger("akka").setLevel(Level.WARN)
val conf = new SparkConf().setMaster("local[2]").setAppName("SparkProducerMSSQL")
val sc = new SparkContext(conf)
val driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
val url = "jdbc:sqlserver://localhost;user=admin;password=oracle;database=AdventureWorks2014"
val username = "admin"
val password = "oracle"
var connection:Connection=null
Class.forName(driver)
connection=DriverManager.getConnection(url,username,password)
val statement=connection.createStatement()
val resultSet=statement.executeQuery("select top 10 CustomerID,AccountNumber from AdventureWorks2014.dbo.Customer")
resultSet.setFetchSize(10);
val columnnumber=resultSet.getMetaData().getColumnCount.toInt
val objcommand=("select top 10 CustomerID,AccountNumber from AdventureWorks2014.dbo.Customer",connection)
//var rows: Vector[DbRow]=Vector.empty
while (resultSet.next())
{
//val row=new DbRow
var i=0.toInt;
for (i <- 1 to columnnumber.toInt)
{
val columnvalue=resultSet.getString(i);
val columnname=resultSet.getMetaData().getColumnName(i)
//row.put(columnname,resultSet.getObject(i))
val test4=resultSet.getObject(i)
var list = new java.util.ArrayList[String]()
list.add(resultSet.getObject(i).toString())
println(list)
}
//rows = rows :+ row
//print(row)
// print(rows.toString().replace("Vector","").replace(")","~"))
//print(rows.toString().replace("Vector",""))
// val test=sc.parallelize(List(row.toString().replace("{","").replace("}","~")))
//val lines=test.flatMap(x=>x.split("~"))
//lines.collect().foreach(println)
//print(row)
//
//println(resultSet)
}
connection.close()
}
}
答案 0 :(得分:0)
移动 &#34; var list = new java.util.ArrayListString&#34; 和 &#34;的println(列表)&#34; 外面的循环
var list = new java.util.ArrayList[String]()
for .. {
}
println(list)
最好使用StringBuilder
StringBuilder sb=new StringBuilder(100);
for {
sb.append(resultSet.getObject(i).toString());
sb.append(",");
}
println(sb.toString())
答案 1 :(得分:0)
Java中的完整示例:
final String url = "jdbc:sqlserver://localhost;user=admin;password=oracle;database=AdventureWorks2014";
final String username = "admin";
final String password = "oracle";
final String sql = "select top 10 CustomerID,AccountNumber from AdventureWorks2014.dbo.Customer";
try (Connection connection = DriverManager.getConnection(url,username,password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql)) {
final ResultSetMetaData metaData = resultSet.getMetaData();
final int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
if (i > 1)
System.out.print(',');
System.out.print(metaData.getColumnName(i));
}
System.out.println();
while (resultSet.next()) {
for (int i = 1; i <= columnCount; i++) {
if (i > 1)
System.out.print(',');
System.out.print(resultSet.getString(i));
}
System.out.println();
}
}