【问题标题】:SqlServer Datatype to Hive Datatype using Spark Scala使用 Spark Scala 将 Sql Server 数据类型转换为 Hive 数据类型
【发布时间】:2019-07-19 11:25:47
【问题描述】:

Spark 用于从 SQL Server 数据库中获取表的架构。由于数据类型不匹配,我在使用此架构创建 Hive 表时遇到问题。我们如何在 Spark Scala 中将 SQL Server 数据类型转换为 Hive 数据类型。

val df = sqlContext.read.format("jdbc")
  .option("url", "jdbc:sqlserver://host:port;databaseName=DB")
  .option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
  .option("dbtable", "schema.tableName")
  .option("user", "Userid").option("password", "pswd")
  .load().schema

【问题讨论】:

标签: scala apache-spark mssql-jdbc


【解决方案1】:

谢谢,得到了解决方案。创建了一种检查数据类型的方法,如下所示。

def sqlToHiveDatatypeMapping(inputDatatype: String): String = inputDatatype match {
  case "numeric" => "int"
  case "bit" => "smallint"
  case "long" => "bigint"
  case "dec_float" => "double"
  case "money" => "double" 
  case "smallmoney" => "double"  
  case "real" => "double"
  case "char" => "string" 
  case "nchar" => "string"  
  case "varchar" => "string"
  case "nvarchar" => "string"
  case "text" => "string"
  case "ntext" => "string"
  case "binary" => "binary"
  case "varbinary" => "binary"
  case "image" => "binary"
  case "date" => "date"
  case "datetime" => "timestamp"
  case "datetime2" => "timestamp"
  case "smalldatetime" => "timestamp"
  case "datetimeoffset" => "timestamp"
  case "timestamp" => "timestamp"
  case "time" => "timestamp"
  case "clob" => "string"
  case "blob" => "binary"
  case _ => "string"
}
val columns = df.fields.map({field => field.name.toLowerCase+" "+sqlToHiveDatatypeMapping(field.dataType.typeName.toLowerCase)}).mkString(",")

【讨论】:

  • 没错,这在我看来是合理的
猜你喜欢
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-27
  • 1970-01-01
相关资源
最近更新 更多