从AWS RDS(MySQL)加载表时是否可以强制模式定义

时间:2017-02-27 08:26:19

标签: mysql amazon-web-services apache-spark apache-spark-sql

我正在使用Apache Spark从 AWS RDS 中读取 MySQL 数据库中的数据。

它实际上也在推断数据库中的模式。不幸的是,表中的一列是active类型(列名:活动)。 TINYINT(1)列具有以下值:

  • 非活跃
  • 活性
  • 未决

Spark将active识别为 BooleanType 。因此,他将true中的所有值更改为falseJsonObject json = JsonObject(params); Log.i("json", json.toString()); 。结果,我无法确定价值。

在将表加载到spark时是否可以强制架构定义?

2 个答案:

答案 0 :(得分:1)

不是 spark TINYINT类型转换为boolean类型为TINYINT(1),但引擎盖下使用了j连接器。

因此,实际上您不需要为该问题指定架构。因为实际导致此问题的是将数据类型BIT视为BIT类型的jdbc驱动程序(因为服务器默默转换TINYINT(1) - > val newUrl = s"$oldUrl&tinyInt1isBit=false" val data = spark.read.format("jdbc") .option("url", newUrl) // your other jdbc options .load 时创建表格。)

您可以查看MySQL official Connector/J Configuration Properties guide中jdbc连接器的所有提示和缺陷。

您只需要通过在网址连接中添加以下内容,为jdbc连接器传递正确的参数:

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
    const string TAG = "MyFirebaseMsgService";
    public override void OnMessageReceived(RemoteMessage message)
    {
        Log.Debug(TAG, "From: " + message.From);
        MessagingCenter.Send(App.CurrentApp, "ReceivedNotification");
    }

    void SendNotification(string messageBody)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        var notificationBuilder = new Android.App.Notification.Builder(this)
            .SetSmallIcon(Resource.Drawable.ic_launcher)
            .SetContentTitle("FCM Message")
            .SetContentText(messageBody)
            .SetAutoCancel(true)
            .SetContentIntent(pendingIntent);

        var notificationManager = NotificationManager.FromContext(this);
        notificationManager.Notify(0, notificationBuilder.Build());
    }
}

答案 1 :(得分:0)

您可以定义架构,并在使用

阅读时使用它
spark.read.schema(Schema)

Spark docs

示例如何定义架构:

// The schema is encoded in a string
val schemaString = "name age"

// Generate the schema based on the string of schema
val fields = schemaString.split(" ")
  .map(fieldName => StructField(fieldName, StringType, nullable = true))
val schema = StructType(fields)

现在您可以使用预定义的架构读取数据:

spark.read.schema(schema).function_to_read_data