1919
2020import com .dtstack .flinkx .phoenix .util .PhoenixUtil ;
2121import com .dtstack .flinkx .rdb .inputformat .JdbcInputFormat ;
22- import com .dtstack .flinkx .rdb .util .DbUtil ;
2322import com .dtstack .flinkx .reader .MetaColumn ;
2423import com .dtstack .flinkx .util .ClassUtil ;
2524import com .dtstack .flinkx .util .DateUtil ;
25+ import com .dtstack .flinkx .util .ReflectionUtils ;
26+ import com .google .common .collect .Lists ;
2627import org .apache .commons .collections .CollectionUtils ;
28+ import org .apache .commons .io .FilenameUtils ;
2729import org .apache .commons .lang3 .StringUtils ;
2830import org .apache .flink .core .io .InputSplit ;
31+ import org .apache .flink .runtime .execution .librarycache .FlinkUserCodeClassLoaders ;
2932import org .apache .flink .types .Row ;
33+ import sun .misc .URLClassPath ;
3034
3135import java .io .IOException ;
32- import java .sql .SQLException ;
36+ import java .lang .reflect .Field ;
37+ import java .net .URL ;
38+ import java .net .URLClassLoader ;
3339import java .util .ArrayList ;
40+ import java .util .List ;
3441
3542import static com .dtstack .flinkx .rdb .util .DbUtil .clobToString ;
3643
4148 */
4249public class PhoenixInputFormat extends JdbcInputFormat {
4350
51+ private static final String PHOENIX_READER_PREFIX = "flinkx-phoenix-reader" ;
52+
4453 @ Override
4554 public void openInternal (InputSplit inputSplit ) throws IOException {
4655 try {
4756 LOG .info (inputSplit .toString ());
4857
49- ClassUtil .forName (driverName , getClass ().getClassLoader ());
58+ Field declaredField = ReflectionUtils .getDeclaredField (getClass ().getClassLoader (), "ucp" );
59+ declaredField .setAccessible (true );
60+ URLClassPath urlClassPath = (URLClassPath ) declaredField .get (getClass ().getClassLoader ());
61+ declaredField .setAccessible (false );
62+
63+ List <URL > needJar = Lists .newArrayList ();
64+ for (URL url : urlClassPath .getURLs ()) {
65+ String urlFileName = FilenameUtils .getName (url .getPath ());
66+ if (urlFileName .startsWith (PHOENIX_READER_PREFIX )) {
67+ needJar .add (url );
68+ }
69+ }
5070
51- if (incrementConfig .isIncrement () && incrementConfig .isUseMaxFunc ()){
71+ ClassLoader parentClassLoader = getClass ().getClassLoader ();
72+ String [] alwaysParentFirstPatterns = new String [2 ];
73+ alwaysParentFirstPatterns [0 ] = "org.apache.flink" ;
74+ alwaysParentFirstPatterns [1 ] = "com.dtstack.flinkx" ;
75+ URLClassLoader childFirstClassLoader = FlinkUserCodeClassLoaders .childFirst (needJar .toArray (new URL [0 ]), parentClassLoader , alwaysParentFirstPatterns );
76+
77+ ClassUtil .forName (driverName , childFirstClassLoader );
78+
79+ if (incrementConfig .isIncrement () && incrementConfig .isUseMaxFunc ()) {
5280 getMaxValue (inputSplit );
5381 }
5482
5583 initMetric (inputSplit );
5684
57- if (!canReadData (inputSplit )){
85+ if (!canReadData (inputSplit )) {
5886 LOG .warn ("Not read data when the start location are equal to end location" );
5987
6088 hasNext = false ;
6189 return ;
6290 }
6391
64- dbConn = PhoenixUtil .getConnectionInternal (dbUrl , username , password );
92+ dbConn = PhoenixUtil .getConnectionInternal (dbUrl , username , password , childFirstClassLoader );
6593
6694 // 部分驱动需要关闭事务自动提交,fetchSize参数才会起作用
6795 dbConn .setAutoCommit (false );
@@ -76,22 +104,22 @@ public void openInternal(InputSplit inputSplit) throws IOException {
76104 columnCount = resultSet .getMetaData ().getColumnCount ();
77105
78106 boolean splitWithRowCol = numPartitions > 1 && StringUtils .isNotEmpty (splitKey ) && splitKey .contains ("(" );
79- if (splitWithRowCol ){
80- columnCount = columnCount - 1 ;
107+ if (splitWithRowCol ) {
108+ columnCount = columnCount - 1 ;
81109 }
82110
83111 hasNext = resultSet .next ();
84112
85- if (StringUtils .isEmpty (customSql )){
86- descColumnTypeList = DbUtil .analyzeTable (dbUrl , username , password , databaseInterface , table , metaColumns );
113+ if (StringUtils .isEmpty (customSql )) {
114+ descColumnTypeList = PhoenixUtil .analyzeTable (resultSet , metaColumns );
87115 } else {
88116 descColumnTypeList = new ArrayList <>();
89117 for (MetaColumn metaColumn : metaColumns ) {
90118 descColumnTypeList .add (metaColumn .getName ());
91119 }
92120 }
93121
94- } catch (SQLException se ) {
122+ } catch (Exception se ) {
95123 throw new IllegalArgumentException ("open() failed. " + se .getMessage (), se );
96124 }
97125
@@ -108,15 +136,15 @@ public Row nextRecordInternal(Row row) throws IOException {
108136 try {
109137 for (int pos = 0 ; pos < row .getArity (); pos ++) {
110138 Object obj = resultSet .getObject (pos + 1 );
111- if (obj != null ) {
112- if (CollectionUtils .isNotEmpty (descColumnTypeList )) {
139+ if (obj != null ) {
140+ if (CollectionUtils .isNotEmpty (descColumnTypeList )) {
113141 String columnType = descColumnTypeList .get (pos );
114- if ("year" .equalsIgnoreCase (columnType )) {
142+ if ("year" .equalsIgnoreCase (columnType )) {
115143 java .util .Date date = (java .util .Date ) obj ;
116144 obj = DateUtil .dateToYearString (date );
117- } else if ("tinyint" .equalsIgnoreCase (columnType )
118- || "bit" .equalsIgnoreCase (columnType )) {
119- if (obj instanceof Boolean ) {
145+ } else if ("tinyint" .equalsIgnoreCase (columnType )
146+ || "bit" .equalsIgnoreCase (columnType )) {
147+ if (obj instanceof Boolean ) {
120148 obj = ((Boolean ) obj ? 1 : 0 );
121149 }
122150 }
@@ -127,9 +155,8 @@ public Row nextRecordInternal(Row row) throws IOException {
127155 row .setField (pos , obj );
128156 }
129157 return super .nextRecordInternal (row );
130- }catch (Exception e ) {
158+ } catch (Exception e ) {
131159 throw new IOException ("Couldn't read data - " + e .getMessage (), e );
132160 }
133161 }
134-
135162}
0 commit comments