GVKun编程网logo

没有定义Pyspark StructType(没有定义的特殊继电器在用户程序中使用)

13

在这里,我们将给大家分享关于没有定义PysparkStructType的知识,让您更了解没有定义的特殊继电器在用户程序中使用的本质,同时也会涉及到如何更有效地'StructType'对象没有属性'to

在这里,我们将给大家分享关于没有定义Pyspark StructType的知识,让您更了解没有定义的特殊继电器在用户程序中使用的本质,同时也会涉及到如何更有效地'StructType' 对象没有属性 'toDDL'、-Spark Scala Mongodb- MongoTypeConversionException 无法将 STRING 转换为 StructType(...)、asp.net-mvc-3 – EntityType’x’没有定义键.定义此EntityType的键、C ++中''struct''和''typedef struct''之间的区别?的内容。

本文目录一览:

没有定义Pyspark StructType(没有定义的特殊继电器在用户程序中使用)

没有定义Pyspark StructType(没有定义的特殊继电器在用户程序中使用)

我正在尝试为数据库测试构建模式,而StructType显然由于某种原因而无法正常工作。我正在学习,它不会导入任何额外的模块。

<type ''exceptions.NameError''>, NameError("name ''StructType'' is not defined",), <traceback object at 0x2b555f0>)

我正在使用1.4.0和Ubuntu 12,如果这与问题有关。我该如何解决这个问题?先感谢您。

答案1

小编典典

你进口了StructType吗?如果不

from pyspark.sql.types import StructType

应该解决问题。

'StructType' 对象没有属性 'toDDL'

'StructType' 对象没有属性 'toDDL'

如何解决''StructType'' 对象没有属性 ''toDDL''?

我正在用 spark 2.4.3 & python 3.6.10 运行这个脚本

columns = ["language","users_count"]
data = [("Java","20000"),("Python","100000"),("Scala","3000")]
spark = SparkSession.builder.appName(''SparkByExamples.com'').getorCreate()
rdd = spark.sparkContext.parallelize(data)
df = rdd.toDF()
print(df.schema.toDDL)

AttributeError: ''StructType'' 对象没有属性 ''toDDL''

The java documentation 提到 toDDL 函数可从 spark 2.4.0 中获得,而 the python documentation 中没有。

有没有其他方法可以从 python 中使用这个 java 函数?

解决方法

您可以在 Java 数据帧对象上调用 toDDL 方法:

df._jdf.schema().toDDL()

-Spark Scala Mongodb- MongoTypeConversionException 无法将 STRING 转换为 StructType(...)

-Spark Scala Mongodb- MongoTypeConversionException 无法将 STRING 转换为 StructType(...)

如何解决-Spark Scala Mongodb- MongoTypeConversionException 无法将 STRING 转换为 StructType(...)?

非常感谢任何帮助。

我正在尝试使用来自 mongodb 的数据构建一个数据框。

val spark = SparkSession.builder()
      .master("local")
      .appName("app")
      .config("spark.mongodb.input.uri",uri)
      .config("spark.mongodb.input.collection","collectionName")
      .config("spark.mongodb.input.readPreference.name","secondary")
      .getorCreate()

val df = MongoSpark.load(spark).limit(1)

然后我尝试逐行读取元素,数据框的架构如下所示:

root
 |-- A: struct (nullable = true)
 |    |-- oid: string (nullable = true)
 |-- B: boolean (nullable = true)
 |-- C: string (nullable = true)
 |-- D: string (nullable = true)
 |-- E: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- a: string (nullable = true)
 |    |    |-- b: string (nullable = true)
 |    |    |-- c: string (nullable = true)
 |    |    |-- d: string (nullable = true)

如果数据框不包含 E,dataframe.show() 会打印出来就好了。

但是,如果数据框确实包含 E,那么 dataframe.show() 会给我

Cannot cast STRING into a StructType(StructField(a,StringType,true),StructField(b,StructField(c,StructField(d,true)) (value: BsonString{value=''http://...some url...''})

我尝试了几乎所有与 stackoverflow 上列出的这个问题相关的解决方案,但我仍然没有通过这个错误。

我应该如何解决这个问题?谢谢!

解决方法

E 实际上是一个包含多个字符串的对象数组。

example of mongodb document

asp.net-mvc-3 – EntityType’x’没有定义键.定义此EntityType的键

asp.net-mvc-3 – EntityType’x’没有定义键.定义此EntityType的键

我有这个动作接收参数,我想打印出所有结果

public ActionResult MusicaGenero(string genero) {

            //should return  more than 30 rows 
             var results = con.artista.Where(x=>x.genero==genero);


            return View(results);
        }

MusicaGenero有这个

@model IEnumerable<MvcApplication1.Models.detallesMusica>

@{
    ViewBag.Title = "MusicaGenero";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Musica del genero de: @ViewBag.genero</h2>

<ul>
@foreach(var detallesMusica in Model)
{
    <li>@detallesMusica.artista</li>        
    <li>@detallesMusica.nombre</li> 
    <li>@detallesMusica.publicado</li>  
    <li>@detallesMusica.costo</li>  
}
</ul>

怎么可能,但它抛出异常

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'album' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'genero' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'artista' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'albums' is based on type 'album' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'generos' is based on type 'genero' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'artista' is based on type 'artista' that has no keys defined.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'album' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'genero' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'artista' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'albums' is based on type 'album' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'generos' is based on type 'genero' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'artista' is based on type 'artista' that has no keys defined.

这里有什么问题?我已经添加了一个密钥,但仍然给了我这个错误.

解决方法

如您所见,您收到的是System.Data.Entity错误,至少在这种情况下,这些错误与您发布的代码无关.

实体框架需要某种方式知道它应该定义哪个字段作为主键.
您可以通过两种方式告诉它如何做到这一点.

您可以定义名称为“Id”的属性,或者将“Id”附加到与实体同名的属性.
例如,其中任何一个都可行

public class Album
{
  public string Id {get; set;}
  public string Name {get; set;}
}
public class Album
{
  public string AlbumId {get; set;}
  public string Name {get; set;}
}

EF将根据命名约定理解使字段Id或AlbumId成为主键.

您可以做的另一件事是使用System.ComponentModel.DataAnnotations [Key]属性来定义密钥.
例如,这会使字段Name成为表的主键.

using System.ComponentModel.DataAnnotations;
//...
public class Album
{
  [Key]
  public string Name {get; set;}
}

C ++中''struct''和''typedef struct''之间的区别?

C ++中''struct''和''typedef struct''之间的区别?

在C ++中,之间有什么区别:

struct Foo { ... };

typedef struct { ... } Foo;

#1楼

您不能对typedef结构使用forward声明。

struct本身是一个匿名类型,因此您没有实际名称来转发声明。

typedef struct{
    int one;
    int two;
}myStruct;

像这样的前瞻声明不会起作用:

struct myStruct; //forward declaration fails

void blah(myStruct* pStruct);

//error C2371: ''myStruct'' : redefinition; different basic types

#2楼

C ++中''typedef struct''和''struct''之间的一个重要区别是''typedef structs''中的内联成员初始化将不起作用。

// the ''x'' in this struct will NOT be initialised to zero
typedef struct { int x = 0; } Foo;

// the ''x'' in this struct WILL be initialised to zero
struct Foo { int x = 0; };

#3楼

Struct是创建数据类型。 typedef用于设置数据类型的昵称。


#4楼

C ++没有区别,但是我相信它会允许你在不明确地执行的情况下声明struct Foo的实例:

struct Foo bar;

#5楼

在C ++中,只有一个微妙的区别。 这是C的延续,它有所作为。

C语言标准( C89§3.1.2.3 , C99§6.2.3和C11§6.2.3 )要求为不同类别的标识符分别命名空间,包括标记标识符 (用于struct / union / enum )和普通标识符 (用于typedef和其他标识符)。

如果你刚才说:

struct Foo { ... };
Foo x;

您会收到编译器错误,因为Foo仅在标记名称空间中定义。

您必须将其声明为:

struct Foo x;

每当你想要引用Foo ,你总是要把它称为struct Foo 。 这会很快烦人,所以你可以添加一个typedef

struct Foo { ... };
typedef struct Foo Foo;

现在struct Foo (在标记命名空间中)和普通Foo (在普通标识符命名空间中)都引用相同的东西,并且您可以在没有struct关键字的情况下自由声明Foo类型的对象。


构造:

typedef struct Foo { ... } Foo;

只是声明和typedef的缩写。


最后,

typedef struct { ... } Foo;

声明一个匿名结构并为其创建一个typedef 。 因此,使用此构造,它在标记名称空间中没有名称,只有typedef名称空间中的名称。 这意味着它也无法向前宣布。 如果要进行前向声明,则必须在标记名称空间中为其指定名称


在C ++中,所有struct / union / enum / class声明都像隐式typedef一样,只要该名称不被另一个具有相同名称的声明隐藏。 有关详细信息,请参阅Michael Burr的答案 。

今天关于没有定义Pyspark StructType没有定义的特殊继电器在用户程序中使用的介绍到此结束,谢谢您的阅读,有关'StructType' 对象没有属性 'toDDL'、-Spark Scala Mongodb- MongoTypeConversionException 无法将 STRING 转换为 StructType(...)、asp.net-mvc-3 – EntityType’x’没有定义键.定义此EntityType的键、C ++中''struct''和''typedef struct''之间的区别?等更多相关知识的信息可以在本站进行查询。

本文标签: