flume自訂sink之mysql

來源:互聯網
上載者:User

標籤:http   sources   extends   images   alt   登陸   port   name   結束   

package me;import static org.mockito.Matchers.booleanThat;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import org.apache.flume.Channel;import org.apache.flume.Context;import org.apache.flume.Event;import org.apache.flume.EventDeliveryException;import org.apache.flume.Transaction;import org.apache.flume.conf.Configurable;import org.apache.flume.sink.AbstractSink;import com.google.common.base.Preconditions;public class MySink extends AbstractSink implements Configurable {private Connection connect;private Statement stmt;private String columnName;private String url;private String user;private String password;private String tableName;// 在整個sink結束時執行一遍@Overridepublic synchronized void stop() {// TODO Auto-generated method stubsuper.stop();}// 在整個sink開始時執行一遍@Overridepublic synchronized void start() {// TODO Auto-generated method stubsuper.start();try {connect = DriverManager.getConnection(url, user, password);// 串連URL為 jdbc:mysql//伺服器位址/資料庫名 ,後面的2個參數分別是登陸使用者名稱和密碼stmt = connect.createStatement(); } catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// 不斷迴圈調用@Overridepublic Status process() throws EventDeliveryException {// TODO Auto-generated method stubChannel ch = getChannel();Transaction txn = ch.getTransaction();Event event = null;txn.begin();while (true) {event = ch.take();if (event != null) {break;}}try {String body = new String(event.getBody());if (body.split(",").length == columnName.split(",").length) {String sql = "insert into " + tableName + "(" + columnName + ") values(" + body + ")";stmt.executeUpdate(sql);txn.commit();return Status.READY;} else {txn.rollback();return null;}} catch (Throwable th) {txn.rollback();if (th instanceof Error) {throw (Error) th;} else {throw new EventDeliveryException(th);}} finally {txn.close();}}@Overridepublic void configure(Context arg0) {columnName = arg0.getString("column_name");Preconditions.checkNotNull(columnName, "column_name must be set!!");        url = arg0.getString("url");        Preconditions.checkNotNull(url, "url must be set!!");        user = arg0.getString("user");        Preconditions.checkNotNull(user, "user must be set!!");        password = arg0.getString("password");        Preconditions.checkNotNull(password, "password must be set!!");        tableName = arg0.getString("tableName");        Preconditions.checkNotNull(tableName, "tableName must be set!!");}}

 

agent.sources = s1    agent.channels = c1  agent.sinks = sk1  agent.sources.s1.type = netcat  agent.sources.s1.bind = localhost  agent.sources.s1.port = 5678  agent.sources.s1.channels = c1  agent.sinks.sk1.type = me.MySinkagent.sinks.sk1.url=jdbc:mysql://192.168.16.33:3306/testagent.sinks.sk1.tableName= test.useragent.sinks.sk1.user=root[email protected]agent.sinks.sk1.column_name=id, username, passwordagent.sinks.sk1.channel = c1  agent.channels.c1.type = memoryagent.channels.c1.capacity = 1000     agent.channels.c1.transactionCapacity = 100

 

lihudeMacBook-Pro:~ SunAndLi$ cd hadoop-2.7.2/flume/lihudeMacBook-Pro:flume SunAndLi$  bin/flume-ng agent -c conf -f conf/sink-mysql --name agent -Dflume.root.logger=INFO,console 

 

flume自訂sink之mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.