PostgreSQLのページャーとMariaDB Xpandの連携

I’m not an anti-GUI person. In fact, I wrote three books about web GUI development with Java. However, I also like the command-line interface (CLI), especially text-based UIs. After a year of exploring MariaDB and the DevOps world, I got to discover and play with many text-based CLI tools that I didn’t know even existed. These tools are especially useful when connecting to remote servers that don’t have a GUI.

私が頻繁に使用する特別なCLIツールの1つは、mariadb SQL クライアント(またはMySQLの世界ではmysql)です。これは、MariaDB互換データベースに接続するためのCLIプログラムです。それを使用すると、SQLクエリやその他のコマンドをデータベースサーバーに送信できます。

MariaDB CLIベースのSQLクライアント

mariadb SQLクライアントには、設定オプションが複数あり、その1つに端末ページャーを設定する可能性があります。Linuxに精通している場合、morelessページャーを聞いたり使用したりしたことがあるでしょう。環境変数PAGERを介してページャーを設定し、mariadbが自動的に使用することができます。または、mariadbプロンプトを使用して現在のセッションのみにページャーを設定することもできます。たとえば、データベースに接続した後、lessページャーを使用するには、次のコマンドを実行します。

MariaDB SQL

 

pager less

次にSQLクエリを実行すると、キーボードの矢印キーを使用して結果セットを移動できるようになります。

MariaDB SQLクライアントを使用してページャーを設定する

lessページャは便利ですが、テーブルとして表示されるSQL結果セットには最適ではありません。pspgというオープンソースツールがあります(GitHubでドキュメントとソースコードを確認できます)。これは最初はPostgreSQL用に開発されましたが、後にMariaDBを含む他のいくつかのデータベースをサポートするようになりました。mariadb SQLクライアントがMariaDB Xpandデータベースに接続できるため、試してみましたが、完璧に動作しました。試してみる方法を続けて読んでください。

Xpandデータベースを起動する最も簡単な方法は、SkySQLでサービスを作成することです(無料です)。ただし、Dockerを使用してローカルインスタンスを実行することもできます。必要なスニペットは次の通りです:

Shell

 

docker run --name xpand \
  -d \
  -p 3306:3306 \
  --ulimit memlock=-1 \
  mariadb/xpand-single

データベースにデータがあるときにはもっと楽しいです。シンプルで興味深いデモデータベースはこちらのウェブサイトで入手できます。Linuxライクなオペレーティングシステムでは、次のコマンドを実行してください(Xpandデータベースが他の場所で実行されている場合は、最後のコマンドのIPアドレスを変更してください):

Shell

 

sudo apt install curl -y
curl https://www.mariadbtutorial.com/wp-content/uploads/2019/10/nation.zip --output nation.zip
unzip nation.zip
mariadb -h 127.0.0.1 -u xpand < nation.sql
rm nation.zip nation.sql

必ずpspgをインストールしてください:

Shell

 

apt install pspg -y

mariadb SQLクライアントを使用してデータベースに接続し、カスタムでクールなプロンプトを使用して「Xpand」を表示します。

Shell

 

mariadb -h 127.0.0.1 -u xpand --prompt="Xpand [\d]> " nation

I learned this tip from my colleague Patrick Bossman (Product Manager at MariaDB) during a webinar on MariaDB Xpand + Docker. I recommend watching it if you want to learn more.

MariaDB Xpandにカスタムプロンプトを使用して接続する

現在のセッションでpspgページャーを設定する:

MariaDB SQL

 

pager pspg -s 14 -X --force-uniborder --quit-if-one-screen

A nice feature in pspg is that it shows the fancy text-based UI only when it makes sense (--quit-if-one-screen). So if your query returns only a few rows that fit in the screen, it will just show them right there on the screen as usual. For example, try running the following query:

MariaDB SQL

 

select * from continents;

ここに新しいものはありません。

表示される行が少ない場合、pspgページャーはアクティブになりません

しかし、以下を試してみてください:

MariaDB SQL

 

select * from countries;

A navigable text-based interface allows you to explore the data more efficiently.

MariaDB Xpandからのデータをレンダリングするpspgページャー

行を検索したり、並べ替えたり、CSVにエクスポートしたり、列を固定したり、行をマークしたり、その他、ツールと対話するためにマウスを使用することもできます。

pspgのいくつかのメニューオプション

I hope this tool helps you the next time you have to interact with a database via SSH and the command line. You can find more information about how to install pspg on your operating system, configuration options, and documentation on the GitHub repository for the project. If you want to learn more about distributed SQL and the MariaDB Xpand database, watch this short video, take a look at this datasheet, and explore some of the blog posts and documentation.

Source:
https://dzone.com/articles/using-the-postgressql-pager-with-mariadb-xpand