Categories: Technology

Universal API Access from Postgres and SQLite – O’Reilly

[ad_1]

In “SQL: The Common Solvent for REST APIs” we noticed how Steampipe’s suite of open supply plug-ins that translate REST API calls immediately into SQL tables. These plug-ins have been, till not too long ago, tightly sure to the open supply engine and to the occasion of Postgres that it launches and controls. That led members of the Steampipe group to ask: “Can we use the plug-ins in our personal Postgres databases?” Now the reply is sure—and extra—however let’s concentrate on Postgres first.

NOTE: Every Steampipe plugin ecosystem is now additionally a standalone foreign-data-wrapper extension for Postgres, a virtual-table extension for SQLite, and an export device.

Study sooner. Dig deeper. See farther.

Utilizing a Steampipe Plugin as a Standalone Postgres International Knowledge Wrapper (FDW)

Go to Steampipe downloads to seek out the installer to your OS, and run it to accumulate the Postgres FDW distribution of a plugin—on this case, the GitHub plugin. It’s one in every of (at present) 140 plug-ins accessible on the Steampipe hub. Every plugin offers a set of tables that map API calls to database tables—within the case of the GitHub plugin, 55 such tables. Every desk can seem in a FROM or JOIN clause; right here’s a question to pick out columns from the GitHub difficulty, filtering on a repository and creator.

choose
  state,
  updated_at,
  title,
  url
from
  github_issue
the place
  repository_full_name="turbot/steampipe"
  and author_login = 'judell'
order by
  updated_at desc

In case you’re utilizing Steampipe, you possibly can set up the GitHub plugin like this:

steampipe plugin set up github

then run the question within the Steampipe CLI or in any Postgres consumer that may hook up with Steampipe’s occasion of Postgres.

However if you wish to do the identical factor in your personal occasion of Postgres, you possibly can set up the plugin another way.

$ sudo /bin/sh -c "$(
   curl -fsSL https://steampipe.io/set up/postgres.sh)"
Enter the plugin title: github
Enter the model (newest): 

Found:
- PostgreSQL model:   14
- PostgreSQL location:  /usr/lib/postgresql/14
- Working system:     Linux
- System structure:  x86_64

Primarily based on the above, steampipe_postgres_github.pg14.linux_amd64.tar.gz
will probably be downloaded, extracted and put in at: /usr/lib/postgresql/14

Proceed with putting in Steampipe PostgreSQL FDW for model 14 at
 /usr/lib/postgresql/14?
- Press 'y' to proceed with the present model.
- Press 'n' to customise your PostgreSQL set up listing 
and choose a distinct model. (Y/n): 


Downloading steampipe_postgres_github.pg14.linux_amd64.tar.gz...
###############################################################
############################ 100.0%
steampipe_postgres_github.pg14.linux_amd64/
steampipe_postgres_github.pg14.linux_amd64/steampipe_postgres_
github.so
steampipe_postgres_github.pg14.linux_amd64/steampipe_postgres_
github.management
steampipe_postgres_github.pg14.linux_amd64/steampipe_postgres_
github--1.0.sql
steampipe_postgres_github.pg14.linux_amd64/set up.sh
steampipe_postgres_github.pg14.linux_amd64/README.md

Obtain and extraction accomplished.

Putting in steampipe_postgres_github in /usr/lib/postgresql/14...

Efficiently put in steampipe_postgres_github extension!

Recordsdata have been copied to:
- Library listing: /usr/lib/postgresql/14/lib
- Extension listing: /usr/share/postgresql/14/extension/

Now hook up with your server as ordinary, utilizing psql or one other consumer, most usually because the postgres person. Then run these instructions, that are typical for any Postgres overseas knowledge wrapper. As with all Postgres extensions, you begin like this:

CREATE EXTENSION steampipe_postgres_fdw_github;

To make use of a overseas knowledge wrapper, you first create a server:

CREATE SERVER steampipe_github FOREIGN DATA WRAPPER
steampipe_postgres_github OPTIONS (config 'token="ghp_..."');

Use OPTIONS to configure the extension to make use of your GitHub entry token. (Alternatively, the usual atmosphere variables used to configure a Steampipe plugin—it’s simply GITHUB_TOKEN on this case—will work should you set them earlier than beginning your occasion of Postgres.)

The tables offered by the extension will reside in a schema, so outline one:

CREATE SCHEMA github;

Now import the schema outlined by the overseas server into the native schema you simply created:

IMPORT FOREIGN SCHEMA github FROM SERVER steampipe_github INTO github;

Now run a question!

The overseas tables offered by the extension reside within the github schema, so by default you’ll discuss with tables like github.github_my_repository. In case you set search_path="github", although, the schema turns into elective and you’ll write queries utilizing unqualified desk names. Right here’s a question we confirmed final time. It makes use of the GitHub_search_repository which encapsulates the GitHub API for looking repositories.

Suppose you’re searching for repos associated to PySpark. Right here’s a question to seek out repos whose names match “pyspark” and report a couple of metrics that will help you gauge exercise and recognition.

choose
  name_with_owner,
  updated_at,     -- how not too long ago up to date?
  stargazer_count -- how many individuals starred the repo?
from 
  github_search_repository 
the place 
  question = 'pyspark in:title' 
order by
  stargazer_count desc
restrict 10;
+---------------------------------------+------------+---------------+
|name_with_owner                        |updated_at  |stargazer_count|
+---------------------------------------+------------+---------------+
| AlexIoannides/pyspark-example-project | 2024-02-09 | 1324          |
| mahmoudparsian/pyspark-tutorial       | 2024-02-11 | 1077          |
| spark-examples/pyspark-examples       | 2024-02-11 | 1007          |
| palantir/pyspark-style-guide          | 2024-02-12 | 924           |
| pyspark-ai/pyspark-ai                 | 2024-02-12 | 791           |
| lyhue1991/eat_pyspark_in_10_days      | 2024-02-01 | 719           |
| UrbanInstitute/pyspark-tutorials      | 2024-01-21 | 400           |
| krishnaik06/Pyspark-With-Python       | 2024-02-11 | 400           |
| ekampf/PySpark-Boilerplate            | 2024-02-11 | 388           |
| commoncrawl/cc-pyspark                | 2024-02-12 | 361           |
+---------------------------------------+------------+---------------+

When you’ve got plenty of repos, the primary run of that question will take a couple of seconds. The second run will return outcomes immediately, although, as a result of the extension features a highly effective and complex cache.

And that’s all there’s to it! Each Steampipe plugin is now additionally a overseas knowledge wrapper that works precisely like this one. You may load a number of extensions with the intention to be a part of throughout APIs. After all, you possibly can be a part of any of those API-sourced overseas tables with your personal Postgres tables. And to avoid wasting the outcomes of any question, you possibly can prepend “create desk NAME as” or “create materialized view NAME as” to a question to persist outcomes as a desk or view.

Utilizing a Steampipe Plugin as a SQLite Extension That Supplies Digital Tables

Go to Steampipe downloads to seek out the installer to your OS and run it to accumulate the SQLite distribution of the identical plugin.

$ sudo /bin/sh -c "$(curl -fsSL https://steampipe.io/set up/sqlite.sh)"
Enter the plugin title: github
Enter model (newest): 
Enter location (present listing): 

Downloading steampipe_sqlite_github.linux_amd64.tar.gz...
############################################################
################ 100.0%
steampipe_sqlite_github.so

steampipe_sqlite_github.linux_amd64.tar.gz downloaded and 
extracted efficiently at /dwelling/jon/steampipe-sqlite.

Right here’s the setup, and you’ll place this code in ~/.sqliterc if you wish to run it each time you begin sqlite.

.load /dwelling/jon/steampipe-sqlite/steampipe_sqlite_github.so

choose steampipe_configure_github('
  token="ghp_..."
');

Now you possibly can run the identical question as above. Right here, too, the outcomes are cached, so a second run of the question will probably be instantaneous.

What in regards to the variations between Postgres-flavored and SQLite-flavored SQL? The Steampipe hub is your good friend! For instance, listed below are Postgres and SQLite variants of a question that accesses a discipline inside a JSON column with the intention to tabulate the languages related along with your gists.

Postgres

SQLite

The github_my_gist desk studies particulars about gists that belong to the GitHub person who’s authenticated to Steampipe. The language related to every gist lives in a JSONB column referred to as information, which comprises a listing of objects like this.

{
   "measurement": 24541,
   "kind": "textual content/markdown",
   "raw_url": "https://gist.githubusercontent.com/judell/49d66ca2a5d2a3b
   "filename": "steampipe-readme-update.md",
   "language": "Markdown"
}

The features wanted to undertaking that record as rows differ: in Postgres you utilize jsonb_array_elements and in SQLite it’s json_each.

As with Postgres extensions, you possibly can load a number of SQLite extensions with the intention to be a part of throughout APIs. You may be a part of any of those API-sourced overseas tables with your personal SQLite tables. And you may prepend create desk NAME as to a question to persist outcomes as a desk.

Utilizing a Steampipe Plugin as a Standalone Export Device

Go to Steampipe downloads to seek out the installer to your OS, and run it to accumulate the export distribution of a plugin—once more, we’ll illustrate utilizing the GitHub plugin.

$ sudo /bin/sh -c "$(curl -fsSL https://steampipe.io/set up/export.sh)"
Enter the plugin title: github
Enter the model (newest): 
Enter location (/usr/native/bin): 
Created non permanent listing at /tmp/tmp.48QsUo6CLF.

Downloading steampipe_export_github.linux_amd64.tar.gz...
##########################################################
#################### 100.0%
Deflating downloaded archive
steampipe_export_github
Putting in
Making use of crucial permissions
Eradicating downloaded archive
steampipe_export_github was put in efficiently to
/usr/native/bin
$ steampipe_export_github -h
Export knowledge utilizing the github plugin.

Discover detailed utilization data together with desk names, 
column names, and examples on the Steampipe Hub:
https://hub.steampipe.io/plugins/turbot/github

Utilization:
  steampipe_export_github TABLE_NAME [flags]

Flags:
      --config string       Config file knowledge
  -h, --help                assist for steampipe_export_github
      --limit int           Restrict knowledge
      --output string       Output format: csv, json or jsonl 
(default "csv")
      --select strings      Column knowledge to show
      --where stringArray   the place clause knowledge

There’s no SQL engine within the image right here; this device is solely an exporter. To export all of your gists to a JSON file:

steampipe_export_github github_my_gist --output json > gists.json

To pick out just some columns and export to a CSV file:

steampipe_export_github github_my_gist --output csv --select
 "description,created_at,html_url" > gists.csv

You should use --limit to restrict the rows returned and --where to filter them, however principally you’ll use this device to rapidly and simply seize knowledge that you simply’ll therapeutic massage elsewhere, for instance, in a spreadsheet.

Faucet into the Steampipe Plugin Ecosystem

Steampipe plug-ins aren’t simply uncooked interfaces to underlying APIs. They use tables to mannequin these APIs in helpful methods. For instance, the github_my_repository desk exemplifies a design sample that applies persistently throughout the suite of plug-ins. From the GitHub plugin’s documentation:

You may personal repositories individually, or you possibly can share possession of repositories with different folks in a corporation. The github_my_repository desk will record repos that you simply personal, that you simply collaborate on, or that belong to your organizations. To question ANY repository, together with public repos, use the github_repository desk.

Different plug-ins comply with the identical sample. For instance, the Microsoft 365 plugin offers each microsoft_my_mail_message and microsoft_mail_message, and the plugin offers googleworkspace_my_gmail_message and googleworkspace_gmail. The place attainable, plug-ins consolidate views of assets from the attitude of an authenticated person.

Whereas plug-ins usually present tables with fastened schemas, that’s not all the time the case. Dynamic schemas, applied by the Airtable, CSV, Kubernetes, and Salesforce plug-ins (amongst others) are one other key sample. Right here’s a CSV instance utilizing a standalone Postgres FDW.

IMPORT FOREIGN SCHEMA csv FROM SERVER steampipe_csv INTO csv 
 OPTIONS(config 'paths=["/home/jon/csv"]');

Now all of the .csv information in /dwelling/jon/csv will automagically be Postgres overseas tables. Suppose you retain observe of legitimate house owners of EC2 situations in a file referred to as ec2_owner_tags. Right here’s a question towards the corresponding desk.

choose * from csv.ec2_owner_tags;
     proprietor      |            _ctx
----------------+----------------------------
 Pam Beesly     | {"connection_name": "csv"}
 Dwight Schrute | {"connection_name": "csv"}

You may be a part of that desk with the AWS plugin’s aws_ec2_instance desk to report proprietor tags on EC2 situations which can be (or will not be) listed within the CSV file.

choose 
    ec2.proprietor,
    case 
        when csv.proprietor is null then 'false'
        else 'true'
    finish as is_listed
from 
    (choose distinct tags ->> 'proprietor' as proprietor 
     from aws.aws_ec2_instance) ec2
left be a part of 
    csv.ec2_owner_tags csv on ec2.proprietor = csv.proprietor;
     proprietor      | is_listed
----------------+-----------
 Dwight Schrute | true
 Michael Scott  | false

Throughout the suite of plug-ins there are greater than 2,300 predefined fixed-schema tables that you should utilize in these methods, plus an infinite variety of dynamic tables. And new plug-ins are continually being added by Turbot and by Steampipe’s open supply group. You may faucet into this ecosystem utilizing Steampipe or Turbot Pipes, from your personal Postgres or SQLite database, or immediately from the command line.


[ad_2]
Amirul

CEO OF THTBITS.com, sharing my insights with people who have the same thoughts gave me the opportunity to express what I believe in and make changes in the world.

Recent Posts

Tori Spelling Reveals She Put On Diaper, Peed Her Pants While In Traffic

[ad_1] Play video content material misSPELLING Tori Spelling is again at it together with her…

1 year ago

The Ultimate Guide to Sustainable Living: Tips for a Greener Future

Lately, the significance of sustainable residing has turn out to be more and more obvious…

1 year ago

Giorgio Armani on his succession: ‘I don’t feel I can rule anything out’

[ad_1] For many years, Giorgio Armani has been eager to maintain a good grip on…

1 year ago

Potential TikTok ban bill is back and more likely to pass. Here’s why.

[ad_1] Federal lawmakers are once more taking on laws to drive video-sharing app TikTok to…

1 year ago

Taylor Swift & Travis Kelce Not Going to Met Gala, Despite Invitations

[ad_1] Taylor Swift and Travis Kelce will not make their massive debut on the Met…

1 year ago

Best Internet Providers in Franklin, Tennessee

[ad_1] What's the greatest web supplier in Franklin?AT&T Fiber is Franklin’s greatest web service supplier…

1 year ago