Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-40488: [Swift] Add simple get swift example #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions http/get_simple/swift/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/
.netrc
Package.resolved
41 changes: 41 additions & 0 deletions http/get_simple/swift/Client/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import PackageDescription

let package = Package(
name: "GetSimpleClient",
platforms: [
.macOS(.v14)
],
dependencies: [
.package(name: "Arrow", path: "vendor/Arrow")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "GetSimpleClient",
dependencies: [
.product(name: "Arrow", package: "Arrow")
]
),
]
)
45 changes: 45 additions & 0 deletions http/get_simple/swift/Client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!---
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

# HTTP GET Arrow Data: Simple Swift Client Example

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> [!CAUTION]
> This Swift client example is compatible _only_ with the Swift server example. It is incompatible with all the other server examples in the `get_simple` directory of this repository. This is because of a problem in how the Swift Arrow package implements the Arrow IPC stream format (https://github.com/apache/arrow/issues/44910).

This directory contains a minimal example of an HTTP client implemented in Swift. The client:
1. Sends an HTTP GET request to a server.
2. Receives an HTTP 200 response from the server, with the response body containing an Arrow IPC stream record batch.
3. Prints some of the record batches attributes and data to the terminal

To run this example, first start one of the server examples in the parent directory, then:
1. download and copy Apache Arrow Swift's Arrow folder into the vendor/Arrow folder:

```sh
git clone --filter=blob:none --no-checkout --depth 1 --sparse https://github.com/apache/arrow.git
pushd arrow
git sparse-checkout add swift/Arrow
git checkout
popd
mkdir -p vendor/Arrow
mv arrow/swift/Arrow vendor
rm -rf arrow
```

2. run:

```sh
swift run
```
52 changes: 52 additions & 0 deletions http/get_simple/swift/Client/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation
import Arrow

let sem = DispatchSemaphore(value: 0)
let url = URL(string: "http://127.0.0.1:8081")!
print("sending request to server")
let task = URLSession.shared.dataTask(with: url) { data, response, error in
defer {sem.signal()}
if let writeData = data {
let arrowReader = ArrowReader()
switch arrowReader.fromStream(writeData) {
case .success(let result):
let recordBatches = result.batches
print("recordBatch: \(recordBatches.count)")
let rb = recordBatches[0]
print("recordBatch values: \(rb.length)")
print("recordBatch columns: \(rb.columnCount)")
for (idx, column) in rb.columns.enumerated() {
print("col \(idx)")
let array = column.array
for idx in 0..<array.length {
print("data col \(idx): \(String(describing:array.asAny(idx)))")
}
}
case.failure(let error):
print("error: \(error)")
}
} else if let error = error {
print("HTTP Request Failed \(error)")
}
}

task.resume()
_ = sem.wait(timeout: .distantFuture)
print("done running http server")
43 changes: 43 additions & 0 deletions http/get_simple/swift/Server/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import PackageDescription

let package = Package(
name: "GetSimpleClient",
platforms: [
.macOS(.v14)
],
dependencies: [
.package(name: "Arrow", path: "vendor/Arrow"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.3.0"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "GetSimpleClient",
dependencies: [
.product(name: "Arrow", package: "Arrow"),
.product(name: "Hummingbird", package: "hummingbird"),
]
),
]
)
44 changes: 44 additions & 0 deletions http/get_simple/swift/Server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!---
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

# HTTP GET Arrow Data: Simple Swift Server Example

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> [!CAUTION]
> This Swift server example is compatible _only_ with the Swift client example. It is incompatible with all the other client examples in the `get_simple` directory of this repository. This is because of a problem in how the Swift Arrow package implements the Arrow IPC stream format (https://github.com/apache/arrow/issues/44910).

This directory contains a minimal example of an HTTP server implemented in Swift. The server:

1. Creates a record batches and populates it with synthesized data.
2. Listens for HTTP requests from clients.
3. Upon receiving a request, sends an HTTP 200 response with the body containing an Arrow IPC stream of record batches.
To run this example:

```sh
git clone --filter=blob:none --no-checkout --depth 1 --sparse https://github.com/apache/arrow.git
pushd arrow
git sparse-checkout add swift/Arrow
git checkout
popd
mkdir -p vendor/Arrow
mv arrow/swift/Arrow vendor
rm -rf arrow
```

2. run:

```sh
swift run
```
75 changes: 75 additions & 0 deletions http/get_simple/swift/Server/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation
import Arrow
import Hummingbird

func makeRecordBatch(_ numRecords: UInt32) throws -> RecordBatch {
let doubleBuilder: NumberArrayBuilder<Double> = try ArrowArrayBuilders.loadNumberArrayBuilder()
let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder()
let date32Builder = try ArrowArrayBuilders.loadDate32ArrayBuilder()
let date2 = Date(timeIntervalSinceReferenceDate: 86400 * 1)
let date1 = Date(timeIntervalSinceReferenceDate: 86400 * 5000 + 352)
for idx in 0..<numRecords {
doubleBuilder.append(11.11 * Double(idx))
stringBuilder.append("test\(idx)")
if (idx & 1) == 1 {
date32Builder.append(date1)
} else {
date32Builder.append(date2)
}
}

let doubleHolder = ArrowArrayHolderImpl(try doubleBuilder.finish())
let stringHolder = ArrowArrayHolderImpl(try stringBuilder.finish())
let date32Holder = ArrowArrayHolderImpl(try date32Builder.finish())
let result = RecordBatch.Builder()
.addColumn("col1", arrowArray: doubleHolder)
.addColumn("col2", arrowArray: stringHolder)
.addColumn("col3", arrowArray: date32Holder)
.finish()
switch result {
case .success(let recordBatch):
return recordBatch
case .failure(let error):
throw error
}
}

let router = Router()
router.get("/") { request, _ -> ByteBuffer in
Copy link
Member

@ianmcook ianmcook Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other suggested change requires this change also.

Suggested change
router.get("/") { request, _ -> ByteBuffer in
router.get("/") { request, _ -> Response in

print("received request from client")
let recordBatchs = [try makeRecordBatch(4), try makeRecordBatch(3)]
let arrowWriter = ArrowWriter()
let writerInfo = ArrowWriter.Info(.recordbatch, schema: recordBatchs[0].schema, batches: recordBatchs)
switch arrowWriter.toStream(writerInfo) {
case .success(let writeData):
print("sending recordBatchs: \(recordBatchs.count)")
return ByteBuffer(data: writeData)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sets the Content-Type response header to the appropriate value.

Suggested change
return ByteBuffer(data: writeData)
return .init(
status: .ok, headers: [.contentType: "application/vnd.apache.arrow.stream"],
body: .init(byteBuffer: buffer))

case.failure(let error):
throw error
}
}

// create application using router
let app = Application(
router: router,
configuration: .init(address: .hostname("127.0.0.1", port: 8081))
)

try await app.runService()