DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Publishing Data to InfluxDB From Swift

Publishing Data to InfluxDB From Swift

See a tutorial on how to publish data to InfluxDB from Swift.

David G. Simmons user avatar by
David G. Simmons
CORE ·
Apr. 02, 19 · Tutorial
Like (2)
Save
Tweet
Share
5.53K Views

Join the DZone community and get the full member experience.

Join For Free

I've been a very busy man. It was only a few days ago that I wrote about a new InfluxDB library for writing data from Arduino devices to InfluxDB v2 and here I am again, writing about a new library for writing data to InfluxDB. This time, it's in Swift. Now your native Apple apps can write data directly to InfluxDB v2.0 with ease.Image title

It's a really simple library to use, and you can download the entire Xcode project for it from my GitHub. You can use it to write single data points to the DB or to do bulk writes of any size. Here's a quick tutorial on how to use it.

let influxdb = InfluxData()

That gets you an instance of the InfluxData class. Once you have that, you'll need to set some configuration parameters for it.

influxdb.setConfig(server: "serverName", port: 9999, org: "myOrganization", bucket: "myBucket", token: "myToken")

You will, of course, need to set all those values according to your InfluxDB v2.0 server's settings. You can also set the time precision with

let myPrecision = DataPrecision.ms // for Milliseconds, 'us' for microseconds, and 's' for seconds influxdb.setPrecision(precision: myPrecision)

At this point, you're ready to start collecting data and sending it to InfluxDB v2.0! For each data point you collect and want to store, you will create a new Influx object to hold the tags and data.

let point: Influx = Influx(measurement: "myMeasurement") point.addTag(name: "location", value: "home") point.addTag(name: "server", value: "home-server") if !point.addValue(name: "value", value: 100.01) {     print("Unknown value type!\n) } if !point.addValue(name: "value", value: 55) {     print("Unknown value type!\n) } if !point.addValue(name: "value", value: true) {     print("Unknown value type!\n) } if !point.addValue(name: "value", value: "String Value" {     print("Unknown value type!\n) }

As you can see, it accepts Integers, floating point values, Booleans, and strings. If it cannot determine the data type, it will return the Boolean false, so it's always a good idea to check the return value.

For best performance, we recommend writing data in batches to InfluxDB, so you'll need to prepare the data to go into a batch. This is easy to do with a call to:

influxdb.prepare(point: point)

And when it's time to write the batch, just call:

if influxdb.writeBatch() {     print("Batch written successfully!\n) }

Again, writeBatch() returns a Boolean on success or failure, so it's a good idea to check those values.

If you want to write each data point as it comes in, just take the data point you created above and call:

influxdb.writeSingle(dataPoint: point)

You can write data to multiple measurements simultaneously as each data point is initialized with its measurement, and you can add as many tags and fields as you'd like.

This is really the first pass at the InfluxDB v2.0 Swift library, as I'll be adding the ability to query, create buckets, and a lot of other features of the Flux language to the library in the future, but since what most people want to do right away is write data to the database, I thought I'd get this out there.

I hope this is helpful! I know it has been for me. You see, I have lately been just using my Mac laptop to grab data off of my Bluetooth CO2 sensor that I built. In order to do that, I built a small BLE application that connects to the sensor, subscribes to the data ID, and constantly writes the data to InfluxDB. Needless to say, I used this library and have been scraping this data and storing it happily.


I'd love to hear what you plan to do with a Swift Library for 2.0 so be sure to comment below and follow me on twitter and let me know what you're doing!

Data (computing) InfluxDB Swift (programming language) Database

Published at DZone with permission of David G. Simmons, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Development Trends 2023
  • Memory Debugging: A Deep Level of Insight
  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • The 12 Biggest Android App Development Trends in 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: