ODPS功能介紹之資料匯入

來源:互聯網
上載者:User

標籤:

ODPS功能介紹之資料匯入

  在使用ODPS強大的資料處理能力之前,大家最關心的是自己的資料如何匯入到ODPS中。下面介紹一款向ODPS匯入資料的工具-Fluentd。

  Fluentd是一個開源的軟體,用來收集各種源頭日誌(包括Application Log、Sys Log及Access Log),允許使用者選擇外掛程式對日誌資料進行過濾、並儲存到不同的資料處理端(包括MySQL、Oracle、MongoDB、Hadoop、Treasure Data、AWS Services、Google Services以及ODPS等)。Fluentd以小巧靈活而著稱,允許使用者自訂資料來源、過濾處理及目標端等外掛程式,目前在這款軟體中已經有300+個外掛程式運行Fluentd的架構上,而且這些外掛程式全部是開源的。 ODPS也在這款軟體上開源了資料匯入外掛程式。

  環境準備

  使用這款軟體,向ODPS匯入資料,需要具備如下環境:

  Ruby 2.1.0 或更新

  Gem 2.4.5 或更新

  Fluentd-0.10.49 或從Fluentd 官網尋找最新,Fluentd為不同的OS提供了不同的版本

  Protobuf-3.5.1 或更新(Rubyprotobuf)

  安裝匯入外掛程式

  接下來可以通過以下兩種方式中的任意一種來安裝ODPS Fluentd 匯入外掛程式。

  方式一:通過ruby gem安裝:

  複製代碼

  $ gem install fluent-plugin-neitui-odps

  ODPS已經將這個外掛程式發布到GEM庫中, 名稱為fluent-plugin-neitui-odps,只需要通過gem install 命令來安裝即可(大家在使用gem 時在國內可能會遇到gem庫無法訪問,可以在網上搜一下更改gem 庫源來解決)。

  方式二:通過外掛程式源碼安裝:

  複製代碼

  $ gem install protobuf

  $ gem install fluentd --no-ri --no-rdoc

  $ git clone https://github.com/neitui/neitui-odps-fluentd-plugin.git

  $ cp neitui-odps-fluentd-plugin/lib/fluent/plugin/* {YOUR_FLUENTD_DIRECTORY}/lib/fluent/plugin/ -r

  其中第二條命令是安裝fluentd,如果已經安裝可以省略。ODPS Fluentd外掛程式源碼在github上,clone下來之後直接放到Fluentd的plugin目錄中即可。

  外掛程式的使用

  使用Fluentd匯入資料時,最主要的是配置Fluentd的conf檔案,更多conf檔案 的介紹請參見: http://docs.fluentd.org/articles/config-file

  樣本一:匯入Nginx日誌 。Conf中source的配置如下:

  複製代碼

  <source>

  type tail

  path /opt/log/in/in.log

  pos_file /opt/log/in/in.log.pos

  refresh_interval 5s

  tag in.log

  format /^(?<remote>[^ ]*) - - \[(?<datetime>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*?)(?: +\S*)?)?" (?<code>[^ ]*) (?<size>[^ ]*) "-" "(?<agent>[^\"]*)"$/

  time_format %Y%b%d %H:%M:%S %z

  </source>

  fluentd 以tail方式監控指定的檔案內容是否有變化,更多的tail配置參見:http://docs.fluentd.org/articles/in_tail

  match 配置如下:

  複製代碼

  <match in.**>

  type neitui_odps

  neitui_access_id ************

  neitui_access_key *********

  neitui_odps_endpoint http://service.odps.neitui.com/api

  neitui_odps_hub_endpoint http://dh.odps.neitui.com

  buffer_chunk_limit 2m

  buffer_queue_limit 128

  flush_interval 5s

  project projectforlog

  <table in.log>

  table nginx_log

  fields remote,method,path,code,size,agent

  partition ctime=${datetime.strftime(‘%Y%m%d‘)}

  time_format %d/%b/%Y:%H:%M:%S %z

  </table>

  </match>

  資料會匯入到projectforlog project的nginx_log表中,其中會以源中的datetime欄位作為分區,外掛程式遇到不同的值時會自動建立分區;

  樣本二:匯入MySqL中的資料。匯入MySQL中資料時,需要安裝fluent-plugin-sql外掛程式作為source:

  $ gem installfluent-plugin-sql

  配置conf中的source:

  複製代碼

  <source>

  type sql

  host 127.0.0.1

  database test

  adapter mysql

  username xxxx

  password xxxx

  select_interval 10s

  select_limit 100

  state_file /path/sql_state

  <table>

  table test_table

  tag in.sql

  update_column id

  </table>

  </source>

  這個例子是從test_table中SELECT資料,每間隔10s去讀取100條資料出來,SELECT 時將ID列作為主鍵(id欄位是自增型)。關於fluent-plugin-sql的更多說明參見:https://github.com/fluent/fluent-plugin-sql

  match 配置如下:

  複製代碼

  <match in.**>

  type neitui_odps

  neitui_access_id ************

  neitui_access_key *********

  neitui_odps_endpoint http://service.odps.neitui.com/api

  neitui_odps_hub_endpoint http://dh.odps.neitui.com

  buffer_chunk_limit 2m

  buffer_queue_limit 128

  flush_interval 5s

  project your_projectforlog

  <table in.log>

  table mysql_data

  fields id,field1,field2,fields3

  </table>

  </match>

  資料會匯出到ODPSprojectforlog project的mysql_data表中,匯入的欄位包括id,field1,field2,field3。

  關於匯入表的說明

  通過Fluentd匯入資料是走的ODPS即時資料流入通道-Datahub,這個通道需要一個特殊的ODPS表,這個表在建立時需要指定為Hub Table。建立表時可以使用如下語名:

  CREATE TABLE<table_name) (field_name type,…) PARTITIONED BY (pt_name type) INTO<n1> SHARDS HUBLIFECYCLE <n2>;

  其中:n1 是指shards數量,有效值為1-20。在匯入資料時,每個shard的流入量是10M/秒。N2是指資料在Datahub上的保留期,有效值1-7,主要用於StreamCompute情境中使用曆史資料。 例如:

  create table access_log(f1 string, f2 string,f3 string,f4 string,f5 string,f6 string, f7string) partitioned by(ctime string) into 5 shards hublifecycle 7;

  如果向已經存在的表匯入資料,也需要將表修改為HUB表,其命令為:

  ALTER TABLE table_name ENABLE HUTTABLE with <n1> SHARDSHUBLIFECYCLE <n2>;

  外掛程式參數說明

  向ODPS匯入資料,需要將ODPS外掛程式配置在conf檔案中match項中。外掛程式支援的參數說明如下:

  type(Fixed): 固定值neitui_odps.

  neitui_access_id(Required):雲帳號access_id.

  neitui_access_key(Required):雲帳號accesskey.

  neitui_odps_hub_endpoint(Required):如果你的服務部署在ESC上,請把本值設定為 http://dh-ext.odps.neitui-inc.com,否則設定為http://dh.odps.neitui.com.

  neituiodps_endpoint(Required):如果你的服務部署在ESC上,請把本值設定為 http://odps-ext.aiyun-inc.com/api,否則設定為http://service.odps.neitui.com/api .

  buffer_chunk_limit(Optional):塊大小,支援“k”(KB),“m”(MB),“g”(GB)單位,預設 8MB,建議值2MB.

  buffer_queue_limit(Optional):塊隊列大小,此值與buffer_chunk_limit共同決定整個緩衝區大小。

  flush_interval(Optional):強制發送間隔,達到時間後塊資料未滿則強制發送, 預設 60s.

  project(Required):project名稱.

  table(Required):table名稱.

  fields(Required): 與source對應,欄位名必須存在於source之中.

  partition(Optional):若為分區表,則設定此項.

  分區名支援的設定模式:

  固定值: partitionctime=20150804

  關鍵字: partitionctime=${remote} (其中remote為source中某欄位)

  時間格式關鍵字: partitionctime=${datetime.strftime(‘%Y%m%d‘)} (其中datetime為source中某時間格式欄位,輸出為%Y%m%d格式作為分區名稱)

  time_format(Optional):如果使用時間格式關鍵字為<partition>,請設定本參數. 例如: source[datetime]="29/Aug/2015:11:10:16 +0800",則設定<time_format>為"%d/%b/%Y:%H:%M:%S%z"

ODPS功能介紹之資料匯入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.