PostgresでUUIDを生成する

こんにちは。ビーグルソフトの真鍋です。

前回のUUID生成と同じくPostgresでUUIDを生成したくなったので調べてみました。実際にはRDS上のPostgresを利用しているので参考にしてみてください。

blog.beaglesoft.net

バージョン情報

=> SELECT version();
                                                 version
----------------------------------------------------------------------------------------------------------
 PostgreSQL 9.6.10 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit
(1 row)

生成方法

UUIDをPostgresで生成するにはpgcryptoが必要になります。

F.25.5. ランダムデータ関数

そのためあらかじめExtensionがインストールされていることを確認します。

=> SELECT * FROM pg_available_extensions WHERE name = 'pgcrypto';
   name   | default_version | installed_version |         comment
----------+-----------------+-------------------+-------------------------
 pgcrypto | 1.3             | 1.3               | cryptographic functions
(1 row)

今回はすでにインストールされていますが、インストールするには以下のSQLを実行します。

=> CREATE EXTENSION if not exists pgcrypto;
NOTICE:  extension "pgcrypto" already exists, skipping
CREATE EXTENSION

以下の通りgen_random_uuidを実行するとuuidのバージョン4を生成してくれます。

=>  select gen_random_uuid();
           gen_random_uuid
--------------------------------------
 e61de8d7-2822-4544-9c92-4ce89f667396
(1 row)

[改訂新版]内部構造から学ぶPostgreSQL 設計・運用計画の鉄則 (Software Design plus)

[改訂新版]内部構造から学ぶPostgreSQL 設計・運用計画の鉄則 (Software Design plus)

LinuxやMacでUUIDを生成するコマンド

こんにちは。beaglesoftの真鍋です。

今回はLinuxやMacのシェルでUUIDを生成するときに便利なuuidgenを紹介します。

何するものか?

uuidgenはUUIDを生成するためのコマンドです。

使ってみる

以下の通り利用できます。

$ uuidgen
8D4F960F-8E4D-484E-B429-5CCEA11C7880

manを確認すると以下の通りとなっています。

NAME
     uuidgen -- generates new UUID strings

SYNOPSIS
     uuidgen [-hdr]

DESCRIPTION
     The uuidgen command generates a Universally Unique IDentifier (UUID), a 128-bit value guaranteed to be unique over both space and time.

     The following options are available:

     -hdr      Emit CoreFoundation CFUUID-based source code for using the uuid in a header.

RETURN VALUE
     The UUID is printed to standard output as a hyphen-punctuated ASCII string of the form: EEF45689-BBE5-4FB6-9E80-41B78F6578E2 (in printf(3) format
     "%08X-%04X-%04X-%04X-%012X"), unless the -hdr option is given, in which case a fragment of source code is output.

-hdrオプションを付けると、生成したときのマクロが表示されます。これの使いみちがよくわかりません…。

$  uuidgen -hdr
// 198AE91D-3E5B-4790-9465-ABC49E025442
#warning Change the macro name MYUUID below to something useful!
#define MYUUID CFUUIDGetConstantUUIDWithBytes(kCFAllocatorSystemDefault, 0x20, 0xAA,...)

感想

タイムスタンプを利用しようかと思ったのですが、uuidがshellで生成できることを知りませんでした。今後利用する機会が増えそうです。

C#でKuromojiを利用して形態素解析を試してみる

こんにちは。beaglesoftの真鍋です。

ちょっとC#で形態素解析をしたいと思いましてLucene.Net.Analysis.Kuromojiを利用してみました。

www.nuget.org

むかしC#で形態素解析をしたときにはNMeCabを利用しました。

www.nuget.org

とても便利に使わせていただきましたが、残念ながらdotnet coreに対応していません。そのため、以前Javaで利用したことのあるKuromojiのdotnet版がないかなぁと沙汰していたところLucene.Net.Analysis.Kuromojiにたどり着きました。

動作させてみる

テストコードを参考にとりあえず動作するものを作ってみました。いろいろと試してみたのですが、サンプルにあった辞書ファイルを読み込んで辞書の内容で取得できることを試しています。

$ dotnet run

対象の文字列:関西国際空港
---
ICharTermAttribute=>関西
ITermToBytesRefAttribute#BytesRef=>[]
IOffsetAttribute#StartOffset=>0
IOffsetAttribute#EndOffset=>2
IPositionIncrementAttribute=>1
IPositionLengthAttribute=>1
IBaseFormAttribute#GetBaseForm=>
IPartOfSpeechAttribute#GetPartOfSpeech=>テスト名詞
IReadingAttribute#GetReading=>カンサイ
IReadingAttribute#GetPronunciation=>
IInflectionAttribute#GetInflectionForm=>
IInflectionAttribute#GetInflectionType=>
---
---
ICharTermAttribute=>国際
ITermToBytesRefAttribute#BytesRef=>[]
IOffsetAttribute#StartOffset=>2
IOffsetAttribute#EndOffset=>4
IPositionIncrementAttribute=>1
IPositionLengthAttribute=>1
IBaseFormAttribute#GetBaseForm=>
IPartOfSpeechAttribute#GetPartOfSpeech=>テスト名詞
IReadingAttribute#GetReading=>コクサイ
IReadingAttribute#GetPronunciation=>
IInflectionAttribute#GetInflectionForm=>
IInflectionAttribute#GetInflectionType=>
---
---
ICharTermAttribute=>空港
ITermToBytesRefAttribute#BytesRef=>[]
IOffsetAttribute#StartOffset=>4
IOffsetAttribute#EndOffset=>6
IPositionIncrementAttribute=>1
IPositionLengthAttribute=>1
IBaseFormAttribute#GetBaseForm=>
IPartOfSpeechAttribute#GetPartOfSpeech=>テスト名詞
IReadingAttribute#GetReading=>クウコウ
IReadingAttribute#GetPronunciation=>
IInflectionAttribute#GetInflectionForm=>
IInflectionAttribute#GetInflectionType=>
---

ちょっと見づらいですが、関西国際空港が単語単位で分解されています。

ソースコード

ソースコードは以下のようになります。

github.com

KuromojiSample

辞書ファイル

辞書ファイルはテストコードで使用されていたものをそのまま利用しています。

gistdada679d34a0f668c0517abc1cc3f714

さいごに

dotnetはJavaに比べるとライブラリが少ないという印象があるのですが、探せばいろいろあることが最近わかってきました。もっとアンテナの感度と探索範囲を広げるようにしないととおもいました。

作者の方に感謝です。

スラスラわかるC# 第2版

スラスラわかるC# 第2版