@@ -52,55 +52,57 @@ public static class HttpGetFunction implements DrillSimpleFunc {
5252 OptionManager options ;
5353
5454 @ Inject
55- ResultSetLoader loader ;
55+ ResultSetLoader rsLoader ;
5656
5757 @ Workspace
58- org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl .JsonLoaderBuilder jsonLoaderBuilder ;
58+ org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl jsonLoader ;
59+
60+ @ Workspace
61+ org .apache .drill .exec .store .easy .json .loader .SingleElementIterator <java .io .InputStream > stream ;
5962
6063 @ Override
6164 public void setup () {
62- jsonLoaderBuilder = new org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl .JsonLoaderBuilder ()
63- .resultSetLoader (loader )
64- .standardOptions (options );
65+ stream = new org .apache .drill .exec .store .easy .json .loader .SingleElementIterator <>();
66+ rsLoader .startBatch ();
6567 }
6668
6769 @ Override
6870 public void eval () {
6971 // Get the URL
7072 String url = org .apache .drill .exec .expr .fn .impl .StringFunctionHelpers .toStringFromUTF8 (rawInput .start , rawInput .end , rawInput .buffer );
71-
7273 // Process Positional Arguments
7374 java .util .List args = org .apache .drill .exec .store .http .util .SimpleHttp .buildParameterList (inputReaders );
7475 // If the arg list is null, indicating at least one null arg, return an empty map
7576 // as an approximation of null-if-null handling.
7677 if (args == null ) {
77- // Return empty map
7878 return ;
7979 }
80-
8180 String finalUrl = org .apache .drill .exec .store .http .util .SimpleHttp .mapPositionalParameters (url , args );
82-
8381 // Make the API call
8482 java .io .InputStream results = org .apache .drill .exec .store .http .util .SimpleHttp .getRequestAndStreamResponse (finalUrl );
85-
8683 // If the result string is null or empty, return an empty map
8784 if (results == null ) {
88- // Return empty map
8985 return ;
9086 }
91-
9287 try {
93- jsonLoaderBuilder .fromStream (results );
94- org .apache .drill .exec .store .easy .json .loader .JsonLoader jsonLoader = jsonLoaderBuilder .build ();
95- loader .startBatch ();
96- jsonLoader .readBatch ();
88+ stream .setValue (results );
89+ if (jsonLoader == null ) {
90+ jsonLoader = org .apache .drill .exec .store .http .udfs .HttpUdfUtils .createJsonLoader (rsLoader , options , stream );
91+ }
92+ org .apache .drill .exec .physical .resultSet .RowSetLoader rowWriter = rsLoader .writer ();
93+ rowWriter .start ();
94+ if (jsonLoader .parser ().next ()) {
95+ rowWriter .save ();
96+ }
9797 } catch (Exception e ) {
98- throw new org .apache .drill .common .exceptions .DrillRuntimeException ("Error while converting from JSON. " , e );
98+ throw org .apache .drill .common .exceptions .UserException .dataReadError (e )
99+ .message ("Error while reading JSON. " )
100+ .addContext (e .getMessage ())
101+ .build ();
99102 }
100103 }
101104 }
102105
103-
104106 @ FunctionTemplate (names = {"http_request" , "httpRequest" },
105107 scope = FunctionTemplate .FunctionScope .SIMPLE ,
106108 isVarArg = true )
@@ -122,17 +124,20 @@ public static class HttpGetFromStoragePluginFunction implements DrillSimpleFunc
122124 DrillbitContext drillbitContext ;
123125
124126 @ Inject
125- ResultSetLoader loader ;
127+ ResultSetLoader rsLoader ;
126128
127129 @ Workspace
128- org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl . JsonLoaderBuilder jsonLoaderBuilder ;
130+ org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl jsonLoader ;
129131
130132 @ Workspace
131133 org .apache .drill .exec .store .http .HttpStoragePlugin plugin ;
132134
133135 @ Workspace
134136 org .apache .drill .exec .store .http .HttpApiConfig endpointConfig ;
135137
138+ @ Workspace
139+ org .apache .drill .exec .store .easy .json .loader .SingleElementIterator <java .io .InputStream > stream ;
140+
136141 @ Override
137142 public void setup () {
138143 String schemaPath = org .apache .drill .exec .expr .fn .impl .StringFunctionHelpers .toStringFromUTF8 (rawInput .start , rawInput .end , rawInput .buffer );
@@ -154,10 +159,9 @@ public void setup() {
154159 endpointName ,
155160 plugin .getConfig ()
156161 );
157-
162+ stream = new org . apache . drill . exec . store . easy . json . loader . SingleElementIterator <>();
158163 // Add JSON configuration from Storage plugin, if present.
159- jsonLoaderBuilder = org .apache .drill .exec .store .http .udfs .HttpUdfUtils .setupJsonBuilder (endpointConfig , loader , options );
160-
164+ rsLoader .startBatch ();
161165 }
162166
163167 @ Override
@@ -167,30 +171,30 @@ public void eval() {
167171 // If the arg list is null, indicating at least one null arg, return an empty map
168172 // as an approximation of null-if-null handling.
169173 if (args == null ) {
170- // Return empty map
171174 return ;
172175 }
173-
174- java .io .InputStream results = org .apache .drill .exec .store .http .util .SimpleHttp .apiCall (
175- plugin ,
176- endpointConfig ,
177- drillbitContext ,
178- args
179- ).getInputStream ();
180-
176+ java .io .InputStream results = org .apache .drill .exec .store .http .util .SimpleHttp .apiCall (plugin , endpointConfig , drillbitContext , args )
177+ .getInputStream ();
181178 // If the result string is null or empty, return an empty map
182179 if (results == null ) {
183- // Return empty map
184180 return ;
185181 }
186-
187182 try {
188- jsonLoaderBuilder .fromStream (results );
189- org .apache .drill .exec .store .easy .json .loader .JsonLoader jsonLoader = jsonLoaderBuilder .build ();
190- loader .startBatch ();
191- jsonLoader .readBatch ();
183+ stream .setValue (results );
184+ if (jsonLoader == null ) {
185+ // Add JSON configuration from Storage plugin, if present.
186+ jsonLoader = org .apache .drill .exec .store .http .udfs .HttpUdfUtils .createJsonLoader (endpointConfig , rsLoader , options , stream );
187+ }
188+ org .apache .drill .exec .physical .resultSet .RowSetLoader rowWriter = rsLoader .writer ();
189+ rowWriter .start ();
190+ if (jsonLoader .parser ().next ()) {
191+ rowWriter .save ();
192+ }
192193 } catch (Exception e ) {
193- throw new org .apache .drill .common .exceptions .DrillRuntimeException ("Error while converting from JSON. " , e );
194+ throw org .apache .drill .common .exceptions .UserException .dataReadError (e )
195+ .message ("Error while reading JSON. " )
196+ .addContext (e .getMessage ())
197+ .build ();
194198 }
195199 }
196200 }
0 commit comments