天天看點

Data Lake Analytics: 自動推斷 OSS 上的 CSV 檔案

截止目前,Data Lake Analytics(下面簡稱 DLA)已經和 OSS 有很多深度的內建,詳情可以參見 DLA 的官方文檔: DLA 和 OSS 整合

為了能夠提供更好的使用體驗,目前可以通過 DLA 自動探測 OSS 上的多種檔案格式,并自動生成建表語句,大大減少建表的使用成本。之前我們支援探測的檔案格式包括 JSON、AVRO、Parquet, ORC 等,具體使用可以參見

Data Lake Analytics: 基于OSS檔案自動推斷建表

。為了滿足更多客戶的需求,我們新加了一種檔案格式的探測,也就是我們常見的 CSV 格式。本文将介紹如何使用這個功能。

在介紹之前,我們需要先建立一個基于 OSS 的 Database,如下:

CREATE SCHEMA dla_oss_test with DBPROPERTIES(
   catalog='oss',
   location= 'oss://dla-test/'
 );           

我們這裡用到的 OSS Bucket 名稱為 dla-test,大家在具體使用的時候換成自己對于的 OSS Bucket 即可。

通過 DLA 建立基于 OSS 表并探測檔案格式的建表語句基本文法為

CREATE EXTERNAL TABLE table_name LIKE MAPPING ('oss://path/to/target/dir')           

下面我們通過幾個例子來說明這個功能的使用。

例子1

很多時候,我們的 CSV 檔案是沒有檔案頭的,針對這種情況,DLA 自動探測能夠識别這種情況,并自動生成每列的字段名。比如我們準備以下的 CSV 檔案(1.csv):

1,dla,28,beijing
2,spark on dla,30,hangzhou
3,presto,40,shenzhen
4,aliyun,30,shanghai           

我們把 1.csv 檔案上傳到 dla-test Bucket 的 oss://dla-test/dla_csv_oss/dla_csv_noheader 路徑中,然後我們的建表語句如下:

CREATE EXTERNAL TABLE dla_oss_test.create_table_like_csv like mapping('oss://dla-test/dla_csv_oss/dla_csv_noheader/');           

執行完之後, DLA 結合 OSS 的路徑,自動推斷出這個路徑下的 CSV 檔案的格式,以及檔案的類型,我們使用

show create table dla_oss_test.create_table_like_csv

語句就可以看到推斷出來的建表語句:

show create table dla_oss_test.create_table_like_csv

CREATE EXTERNAL TABLE `dla_oss_test`.`create_table_like_csv` (
    `_col1` tinyint,
    `_col2` string,
    `_col3` tinyint,
    `_col4` string
)
STORED AS `TEXTFILE`
LOCATION 'oss://dla-test/dla_csv_oss/dla_csv_noheader'
TBLPROPERTIES (
    'create.table.like.file' = 'oss://dla-test/dla_csv_oss/dla_csv_noheader/',
    'escape.delim' = '\\',
    'field.delim' = ','
)           

可以看出,DLA 已經自動為我們推斷出 CSV 檔案有四列,因為這個 CSV 檔案沒有頭部資訊,是以使用預設的列名當做表的字段名稱,同時結合資料,推斷出每列的資料類型。在 TBLPROPERTIES 裡面,推斷出 CSV 檔案的分隔符以及轉義字元等。

現在我們可以檢視下這張表的資料:

select * from dla_oss_test.create_table_like_csv           

結果如下:

Data Lake Analytics: 自動推斷 OSS 上的 CSV 檔案

可以看到,已經正确讀出我們要的資料。

例子2

如果 CSV 檔案有檔案頭部資訊,DLA 也可以自動推斷出來,比如我們有如下内容的 CSV 檔案(2.csv)

show_id,title,director,cast,country,date_added,release_year,rating,duration,listed_in,description,type
81193313,Chocolate,,"Ha Ji-won, Yoon Kye-sang, Jang Seung-jo, Kang Bu-ja, Lee Jae-ryong, Min Jin-woong, Kim Won-hae, Yoo Teo",South Korea,"November 30, 2019",2019,TV-14,1 Season,"International TV Shows, Korean TV Shows, Romantic TV Shows","Brought together by meaningful meals in the past and present, a doctor and a chef are reacquainted when they begin working at a hospice ward.",TV Show
81197050,Guatemala: Heart of the Mayan World,"Luis Ara, Ignacio Jaunsolo",Christian Morales,,"November 30, 2019",2019,TV-G,67 min,"Documentaries, International Movies","From Sierra de las Minas to Esquipulas, explore Guatemala's cultural and geological wealth, including ancient Mayan cities and other natural wonders.",Movie
81213894,The Zoya Factor,Abhishek Sharma,"Sonam Kapoor, Dulquer Salmaan, Sanjay Kapoor, Sikander Kher, Angad Bedi, Koel Purie, Pooja Bhamrah, Manu Rishi Chadha",India,"November 30, 2019",2019,TV-14,135 min,"Comedies, Dramas, International Movies","A goofy copywriter unwittingly convinces the Indian cricket team that she’s their lucky mascot, to the dismay of their superstition-shunning captain.",Movie
81082007,Atlantics,Mati Diop,"Mama Sane, Amadou Mbow, Ibrahima Traore, Nicole Sougou, Amina Kane, Mariama Gassama, Coumba Dieng, Ibrahima Mbaye, Diankou Sembene","France, Senegal, Belgium","November 29, 2019",2019,TV-14,106 min,"Dramas, Independent Movies, International Movies","Arranged to marry a rich man, young Ada is crushed when her true love goes missing at sea during a migration attempt – until a miracle reunites them.",Movie
80213643,Chip and Potato,,"Abigail Oliver, Andrea Libman, Briana Buckmaster, Brian Dobson, Chance Hurstfield, Dominic Good, Emma Jayne Maas, Evan Byarushengo, Scotia Anderson, Alessandro Juliani","Canada, United Kingdom",,2019,TV-Y,2 Seasons,Kids' TV,"Lovable pug Chip starts kindergarten, makes new friends and tries new things – with a little help from Potato, her secret mouse pal.",TV Show
81172754,Crazy people,Moses Inwang,"Ramsey Nouah, Chigul, Sola Sobowale, Ireti Doyle, Ben Touitou, Francis Onwochei, Ememobong Nkana, Emem Inwang, Patrick Onyeke",Nigeria,"November 29, 2019",2018,TV-14,107 min,"Comedies, International Movies, Thrillers",Nollywood star Ramsey Nouah learns that someone is impersonating him and breaks out of a mental institution to expose the imposter.,Movie
81120982,I Lost My Body,Jérémy Clapin,"Hakim Faris, Victoire Du Bois, Patrick d'Assumçao, Dev Patel, Alia Shawkat, George Wendt",France,"November 29, 2019",2019,TV-MA,81 min,"Dramas, Independent Movies, International Movies","Romance, mystery and adventure intertwine as a young man falls in love and a severed hand scours Paris for its owner in this mesmerizing animated film.",Movie
81227195,Kalushi: The Story of Solomon Mahlangu,Mandla Dube,"Thabo Rametsi, Thabo Malema, Welile Nzuza, Jafta Mamabolo, Louw Venter, Pearl Thusi",South Africa,"November 29, 2019",2016,TV-MA,107 min,"Dramas, International Movies","The life and times of iconic South African liberation fighter Solomon Mahlangu, who battled the forces of apartheid, come into focus.",Movie
70205672,La Reina del Sur,,"Kate del Castillo, Cristina Urgel, Alberto Jiménez, Juan José Arjona, Humberto Zurita, Dagoberto Gama, Christian Tappán, Miguel de Miguel, Salvador Zerboni, Carmen Navarro, Santiago Meléndez, Juan Carlos Solarte","United States, Spain, Colombia, Mexico",,2019,TV-14,2 Seasons,"Crime TV Shows, International TV Shows, Spanish-Language TV Shows","This compelling show tells the story of the legendary Teresa Mendoza, a courageous woman who is perceived as conquering the world of drug trafficking.",TV Show
81172841,Lagos Real Fake Life,Mike Ezuruonye,"Nonso Diobi, Mike Ezuruonye, Mercy Aigbe, Rex Nosa, Annie Macaulay Idibia, Ik Ogbonna, Nedu Wazobia, Uzee Usman, Odunlade Adekola, Mr Jollof, Efe Irele, Josh 2 Funny, Haillie Sumney, Emmanuella, MC Lively",,"November 29, 2019",2018,TV-14,118 min,"Comedies, International Movies","Two mooching friends vie for the attention of wealthy, beautiful women only to discover that their lavish lifestyles are bogus.",Movie
81172899,Payday,Cheta Chukwu,"Baaj Adebule, Ebiye Victor, Meg Otanwa, Bisola Aiyeola, Mawuli Gavor, Zack Orji, Segilola Ogidan",Nigeria,"November 29, 2019",2018,TV-MA,110 min,"Comedies, Independent Movies, International Movies","After an expensive night out, two flatmates get tangled in an overnight misadventure to recover their rent money to pay their dead landlord's daughter.",Movie           

我們把這個檔案上傳到 OSS 的 oss://dla-test/dla_csv_oss/dla_csv_header/ 路徑下,然後建表語句如下:

CREATE EXTERNAL TABLE dla_oss_test.create_table_like_header like mapping('oss://dla-test/dla_csv_oss/dla_csv_header/');           

執行這個語句的時候,DLA 能夠自動推斷出檔案的 header 資訊,以及類型。執行完這個語句之後,DLA 給我們推斷出來的建表語句如下:

CREATE EXTERNAL TABLE `dla_oss_test`.`create_table_like_header` (
    `show_id` string COMMENT 'from deserializer',
    `title` string COMMENT 'from deserializer',
    `director` string COMMENT 'from deserializer',
    `cast` string COMMENT 'from deserializer',
    `country` string COMMENT 'from deserializer',
    `date_added` string COMMENT 'from deserializer',
    `release_year` string COMMENT 'from deserializer',
    `rating` string COMMENT 'from deserializer',
    `duration` string COMMENT 'from deserializer',
    `listed_in` string COMMENT 'from deserializer',
    `description` string COMMENT 'from deserializer',
    `type` string COMMENT 'from deserializer'
)
ROW FORMAT
    SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
STORED AS
    INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'
    OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION 'oss://dla-test/dla_csv_oss/dla_csv_header'
TBLPROPERTIES (
    'create.table.like.file' = 'oss://dla-test/dla_csv_oss/dla_csv_header/',
    'escapeChar' = '"',
    'quoteChar' = '"',
    'separatorChar' = ',',
    'skip.header.line.count' = '1'
)           

可以看出,表的列名使用 CSV 檔案中的 Header 資訊,我們可以檢視一下這張表的資料:

Data Lake Analytics: 自動推斷 OSS 上的 CSV 檔案

小結

DLA 自動推斷 OSS 表上的常見檔案格式可以大大減少我們的使用成本,在建表之前,我們不需要事先去找業務方去了解檔案的格式,字段名等等,DLA 自動為我們做了這些事。

當然,目前自動推斷支援的檔案格式比較少,後期我們會根據具體情況,會增加更多的檔案格式。

Data Lake Analytics簡介

Data Lake Analytics(簡稱DLA)是阿裡雲上一款Serverless化的低成本大資料處理和分析服務。主打高成本效益、打通和聯合雲上資料生态,以及高易用性的BI相容能力。

  • 高成本效益:世界領先的并行SQL計算引擎能力,為使用者提供千億級資料的互動式分析能力和萬億級資料的批量處理能力,并按量付費,零資源維護和保有成本;
  • 雲上資料生态:提供阿裡雲上覆寫最廣泛的資料聯邦、融合分析、處理、回流能力,用統一的資料視角盤活、打通OSS、Table Store、日志服務資料湖投遞、DBS資料庫的資料湖備份、關系型資料庫和NoSQL資料庫的分析、處理生态;
  • 易用的BI相容能力:以MySQL使用者接口的形态相容幾乎所有具備MySQL連接配接能力的BI套件和可視化分析工具,讓使用者輕松點選即可分析、洞見雲資料價值。
  • 産品連結: https://www.aliyun.com/product/datalakeanalytics
  • 新客戶1元試用: https://common-buy.aliyun.com/?commodityCode=openanalytics_post

歡迎關注資料湖技術社群

資料湖開發者社群由 阿裡雲開發者社群 與 阿裡雲Data Lake Analytics團隊 共同發起,緻力于推廣資料湖相關技術,包括hudi、delta、spark、presto、oss、中繼資料、存儲加速、格式發現等,學習如何建構資料湖分析系統,打造适合業務的資料架構。

Data Lake Analytics: 自動推斷 OSS 上的 CSV 檔案