-
Notifications
You must be signed in to change notification settings - Fork 19
GH-40488: [Swift] Add simple get swift example #41
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
Open
abandy
wants to merge
1
commit into
apache:main
Choose a base branch
from
abandy:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
] | ||
), | ||
] | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
] | ||
), | ||
] | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
ianmcook marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other suggested change requires this change also.
Suggested change
|
||||||||||
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) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sets the
Suggested change
|
||||||||||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.