1
resposta

Renomear colunas aninhadas (pyspark)

Tenho o seguinte estrutura em um dataframe pyspark:**

root
|-- customer: struct (nullable = true)  
|    |-- name: string (nullable = true)  
|-- customerId: long (nullable = true)  
|-- driver: struct (nullable = true)  
|    |-- cpf: long (nullable = true)  
|    |-- driverId: long (nullable = true)  
|    |-- name: string (nullable = true)  
|    |-- registration: long (nullable = true)  
|    |-- id: string (nullable = true)  
|-- ntwkMedium: string (nullable = true)  
|-- numSats: long (nullable = true)  
|-- positionDate: string (nullable = true)  
|-- shadow: string (nullable = true)  
|-- smartCameras: struct (nullable = true)  
|    |-- cameras: array (nullable = true)  
|    |    |-- element: struct (containsNull = true)  
|    |    |    |-- camera_id: long (nullable = true)  
|    |    |    |-- files: array (nullable = true)  
|    |    |    |    |-- element: struct (containsNull = true)  
|    |    |    |    |    |-- begin_time: string (nullable = true)  
|    |    |    |    |    |-- duration: long (nullable = true)  
|    |    |    |    |    |-- url: string (nullable = true)  
|    |    |    |    |    |-- url_provider: string (nullable = true)      
|    |-- eventId: long (nullable = true)  
|    |-- eventType: long (nullable = true)  
|    |-- hwOriginalEventId: long (nullable = true)  
|    |-- messageId: long (nullable = true)  
|    |-- metadata: struct (nullable = true)    
|    |    |-- uploadVideoInfo: struct (nullable = true)  
|    |    |    |-- downloadRetries: boolean (nullable = true)  
|    |    |    |-- requested: boolean (nullable = true)  
|    |    |    |-- showButton: boolean (nullable = true)  
|    |    |    |-- totalVehicleChannel: long (nullable = true)  
|    |-- provider: struct (nullable = true)  
|    |    |-- customer: struct (nullable = true)  
|    |    |    |-- orgId: long (nullable = true)  
|    |    |-- hwtype: string (nullable = true)  
|    |    |-- vehicle: struct (nullable = true)  
|    |    |    |-- deviceId: long (nullable = true)  
|    |-- providerRequestId: long (nullable = true)  
|    |-- requestId: long (nullable = true)  
|    |-- validGps: long (nullable = true)  
|-- yearMonth: string (nullable = true) 

Preciso renomear no array cameras, os seguintes campos: camera_id -> cameraId / begin_time -> beginTime e url_provider -> urlProvider mantendo toda o restante da estutura completa para nova saida. Tentei inferindo um schema completo do smartCameras mas da erro na saída. Tentei com withColumnRenamed e só deu certo para o cameraId, abaixo dele nao. ** A saída deve ser.**

root
|-- customer: struct (nullable = true)  
|    |-- name: string (nullable = true)  
|-- customerId: long (nullable = true)  
|-- driver: struct (nullable = true)  
|    |-- cpf: long (nullable = true)  
|    |-- driverId: long (nullable = true)  
|    |-- name: string (nullable = true)  
|    |-- registration: long (nullable = true)  
|    |-- id: string (nullable = true)  
|-- ntwkMedium: string (nullable = true)  
|-- numSats: long (nullable = true)  
|-- positionDate: string (nullable = true)  
|-- shadow: string (nullable = true)  
|-- smartCameras: struct (nullable = true)  
|    |-- cameras: array (nullable = true)  
|    |    |-- element: struct (containsNull = true)  
|    |    |    |-- cameraId: long (nullable = true)  
|    |    |    |-- files: array (nullable = true)  
|    |    |    |    |-- element: struct (containsNull = true)  
|    |    |    |    |    |-- beginTime: string (nullable = true)  
|    |    |    |    |    |-- duration: long (nullable = true)  
|    |    |    |    |    |-- url: string (nullable = true)  
|    |    |    |    |    |-- urlProvider: string (nullable = true)      
|    |-- eventId: long (nullable = true)  
|    |-- eventType: long (nullable = true)  
|    |-- hwOriginalEventId: long (nullable = true)  
|    |-- messageId: long (nullable = true)  
|    |-- metadata: struct (nullable = true)    
|    |    |-- uploadVideoInfo: struct (nullable = true)  
|    |    |    |-- downloadRetries: boolean (nullable = true)  
|    |    |    |-- requested: boolean (nullable = true)  
|    |    |    |-- showButton: boolean (nullable = true)  
|    |    |    |-- totalVehicleChannel: long (nullable = true)  
|    |-- provider: struct (nullable = true)  
|    |    |-- customer: struct (nullable = true)  
|    |    |    |-- orgId: long (nullable = true)  
|    |    |-- hwtype: string (nullable = true)  
|    |    |-- vehicle: struct (nullable = true)  
|    |    |    |-- deviceId: long (nullable = true)  
|    |-- providerRequestId: long (nullable = true)  
|    |-- requestId: long (nullable = true)  
|    |-- validGps: long (nullable = true)  
|-- yearMonth: string (nullable = true)     

Conseguem me ajudar?

1 resposta

Oiii Lisnete, este tópico está duplicado. Te respondi neste link. Continuamos por lá, tudo bem?!

Abraços e bons estudos!