Flink异步IO之Mysql



	4.0.0

	org.myorg.quickstart
	quickstart
	0.1
	jar

	Flink Quickstart Job
	http://www.myorganization.org

	
		UTF-8
		1.9.1
		1.8
		2.11
		${java.version}
		${java.version}
	

	
		
			apache.snapshots
			Apache Development Snapshot Repository
			https://repository.apache.org/content/repositories/snapshots/
			
				false
			
			
				true
			
		
	

	
		
		
		
			org.apache.flink
			flink-java
			${flink.version}
			provided
		
		
			org.apache.flink
			flink-streaming-java_${scala.binary.version}
			${flink.version}
			provided
		

		

		

		
		
		
			org.slf4j
			slf4j-log4j12
			1.7.7
			runtime
		
		
			log4j
			log4j
			1.2.17
			runtime
		

		
		
			com.alibaba
			druid
			1.0.18
		
	

	
		

			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.1
				
					${java.version}
					${java.version}
				
			

			
			
			
				org.apache.maven.plugins
				maven-shade-plugin
				3.0.0
				
					
					
						package
						
							shade
						
						
							
								
									org.apache.flink:force-shading
									com.google.code.findbugs:jsr305
									org.slf4j:*
									log4j:*
								
							
							
								
									
									*:*
									
										META-INF/*.SF
										META-INF/*.DSA
										META-INF/*.RSA
									
								
							
							
								
									org.myorg.quickstart.StreamingJob
								
							
						
					
				
			
		

		
			

				
				
					org.eclipse.m2e
					lifecycle-mapping
					1.0.0
					
						
							
								
									
										org.apache.maven.plugins
										maven-shade-plugin
										[3.0.0,)
										
											shade
										
									
									
										
									
								
								
									
										org.apache.maven.plugins
										maven-compiler-plugin
										[3.1,)
										
											testCompile
											compile
										
									
									
										
									
								
							
						
					
				
			
		
	

	
	
	
	
		
			add-dependencies-for-IDEA

			
				
					idea.version
				
			

			
				
					org.apache.flink
					flink-java
					${flink.version}
					compile
				
				
					org.apache.flink
					flink-streaming-java_${scala.binary.version}
					${flink.version}
					compile
				
			
		
	


 

package org.myorg.quickstart.async;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.async.ResultFuture;
import org.apache.flink.streaming.api.functions.async.RichAsyncFunction;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Supplier;

public class AsyncMysqlRequest extends RichAsyncFunction {
    private transient DruidDataSource dataSource;
    private transient ExecutorService executorService;
    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
        executorService = Executors.newFixedThreadPool(20);

        dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setUrl("jdbc:mysql://localhost:3306/bigdata?characterEncoding=UTF-8");
        dataSource.setInitialSize(5);
        dataSource.setMinIdle(10);
        dataSource.setMaxActive(20);
    }

    @Override
    public void close() throws Exception {
        super.close();
        dataSource.close();
        executorService.shutdown();
    }

    @Override
    public void asyncInvoke(String id, ResultFuture resultFuture) throws Exception {
        Future future = executorService.submit(() -> {
            return queryFromMysql(id);
        });
        CompletableFuture.supplyAsync(new Supplier() {
            @Override
            public String get() {
                try {
                    return future.get();
                } catch (Exception e) {
                    return null;
                }
            }
        }).thenAccept( (String dbResult) -> {
            resultFuture.complete(Collections.singleton(dbResult));
        });


    }

    private String queryFromMysql(String param) throws SQLException {
        String sql = "select name from info where id = ?";
        String result = null;
        Connection connection = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            connection = dataSource.getConnection();
            stmt = connection.prepareStatement(sql);
            stmt.setString(1,param);
            rs = stmt.executeQuery();
            while (rs.next()){
                result = rs.getString("name");
            }
        }finally {
            if (rs != null){
                rs.close();
            }
            if (stmt != null){
                stmt.close();
            }
            if (connection != null){
                connection.close();
            }
        }

        if (result != null){
            //可以放入缓存中
        }


        return result;
    }
}

 

你可能感兴趣的:(Flink)