ianmcook commented on code in PR #39081:
URL: https://github.com/apache/arrow/pull/39081#discussion_r1433352897


##########
http/examples/get/client.java:
##########
@@ -0,0 +1,53 @@
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.VectorSchemaRoot;
+import org.apache.arrow.vector.VectorUnloader;
+import org.apache.arrow.vector.ipc.ArrowStreamReader;
+import org.apache.arrow.vector.ipc.message.ArrowRecordBatch;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.List;
+import java.util.ArrayList;
+
+public class ArrowHttpClient {
+
+    public static void main(String[] args) {
+        String serverUrl = "http://localhost:8000";;
+
+        try {
+            URL url = new URL(serverUrl);
+            HttpURLConnection connection = (HttpURLConnection) 
url.openConnection();
+            connection.setRequestMethod("GET");
+
+            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
+                InputStream inputStream = connection.getInputStream();
+
+                BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
+                ArrowStreamReader reader = new ArrowStreamReader(inputStream, 
allocator);
+                List<ArrowRecordBatch> batches = new ArrayList<>();
+
+                int num_rows = 0;
+                while (reader.loadNextBatch()) { 
+                    VectorSchemaRoot root = reader.getVectorSchemaRoot();
+                    num_rows += root.getRowCount();
+                    VectorUnloader unloader = new VectorUnloader(root);
+                    ArrowRecordBatch arb = unloader.getRecordBatch();
+                    batches.add(arb);
+                }
+                
+                System.out.println(reader.bytesRead() + " bytes received");
+                System.out.println(num_rows + " records received");
+                System.out.println(batches.size() + " record batches 
received");
+
+                reader.close();
+            } else {
+                System.err.println("Failed with response code: " + 
connection.getResponseCode());
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}

Review Comment:
   To run:
   
   Create `pom.xml` with this contents:
   
   ```xml
   <?xml version="1.0" encoding="UTF-8"?>
   <project xmlns="http://maven.apache.org/POM/4.0.0";
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
       <modelVersion>4.0.0</modelVersion>
   
       <groupId>com.example</groupId>
       <artifactId>ArrowHttpClient</artifactId>
       <version>1.0-SNAPSHOT</version>
   
       <properties>
           <arrow.version>14.0.1</arrow.version>
           <maven.compiler.source>21</maven.compiler.source>
           <maven.compiler.target>21</maven.compiler.target>
       </properties>
   
       <dependencies>
   
           <dependency>
               <groupId>org.apache.arrow</groupId>
               <artifactId>arrow-memory-core</artifactId>
               <version>${arrow.version}</version>
           </dependency>
   
           <dependency>
               <groupId>org.apache.arrow</groupId>
               <artifactId>arrow-memory-netty</artifactId>
               <version>${arrow.version}</version>
           </dependency>
   
           <dependency>
               <groupId>org.apache.arrow</groupId>
               <artifactId>arrow-vector</artifactId>
               <version>${arrow.version}</version>
           </dependency>
   
       </dependencies>
   </project>
   ```
   
   Move `client.java` to `src/main/java/com/example/ArrowHttpClient.java`
   
   Then:
   ```sh
   mvn install
   mvn compile
   _JAVA_OPTIONS="--add-opens=java.base/java.nio=ALL-UNNAMED" mvn exec:java 
-Dexec.mainClass="ArrowHttpClient"
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to