10 進数データ型での保存に問題があり、それがバグなのか何か間違っているのかわかりません
ファイル内のデータは次のようになります
Column1 column2 column3
steve 100 100.23
ronald 500 20.369
maria 600 19.23
csvリーダーを使用してsparkのスキーマを推測すると、column3のデータ型を文字列として取得するため、10進数に変換してテーブルとして保存しています。
テーブルにアクセスすると、次のように出力が表示され、小数点以下が削除されます
Column1 column2 column3
steve 100 100
ronald 500 20
maria 600 19
また、column3 を 10 進数としてローカル テーブルを作成し、データをロードして、Hive で同じことをテストしました。
この点で何か助けていただければ幸いです。
これが上記のコードです
スパーク内 ファイルのスキーマ
root
|-- DEST_AIRPORT_ID: integer (nullable = true)
|-- DEST_AIRPORT_SEQ_ID: integer (nullable = true)
|-- DEST_CITY_MARKET_ID: integer (nullable = true)
|-- DEST string: string (nullable = true)
|-- DEST_CITY_NAME: string (nullable = true)
|-- DEST_STATE_ABR: string (nullable = true)
|-- DEST_STATE_FIPS: integer (nullable = true)
|-- DEST_STATE_NM: string (nullable = true)
|-- DEST_WAC: integer (nullable = true)
|-- DEST_Miles: double (nullable = true)
コード
from pyspark import SparkContext
sc =SparkContext()
from pyspark.sql.types import *
from pyspark.sql import HiveContext
sqlContext = HiveContext(sc)
Data=sqlContext.read.format("com.databricks.spark.csv").options(header="true").options(delimiter=",").options(inferSchema="true").load("s3://testbucket/Data_test.csv")
Data1=Data.withColumnRenamed('DEST string','DEST_string')
Data2 =Data1.withColumn('DEST_Miles',Data1.DEST_Miles.cast('Decimal'))
Data2.saveAsTable('Testing_data', mode='overwrite',path='s3://bucketname/Testing_data')
10進数に変換後のスキーマ
root
|-- DEST_AIRPORT_ID: integer (nullable = true)
|-- DEST_AIRPORT_SEQ_ID: integer (nullable = true)
|-- DEST_CITY_MARKET_ID: integer (nullable = true)
|-- DEST string: string (nullable = true)
|-- DEST_CITY_NAME: string (nullable = true)
|-- DEST_STATE_ABR: string (nullable = true)
|-- DEST_STATE_FIPS: integer (nullable = true)
|-- DEST_STATE_NM: string (nullable = true)
|-- DEST_WAC: integer (nullable = true)
|-- DEST_Miles: decimal (nullable = true)
ハイブのために
create table Destination(
DEST_AIRPORT_ID int,
DEST_AIRPORT_SEQ_ID int,
DEST_CITY_MARKET_ID int,
DEST string,
DEST_CITY_NAME string,
DEST_STATE_ABR string,
DEST_STATE_FIPS string,
DEST_STATE_NM string,
DEST_WAC int,
DEST_Miles Decimal(10,0)
);
INSERT INTO TEST_DATA SELECT * FROM TESTING_data;
さらに詳しい情報が必要な場合はお知らせください。
どうもどうも