正規表現(regex)を使用したテキスト・データの読み込み

regexを使用してテキスト・データを読み込むためのDSE Graph Loaderの使用方法。

正規表現(regex)を使用して解析されたテキスト・データのデータ・マッピング・スクリプトを説明とともに示します。完全なスクリプトは、ページの最後にあります。

手順

  • 必要に応じて、マッピング・スクリプトに構成を追加します。
  • 読み込み対象のデータのサンプルは以下のようになります。
    SAMPLE INPUT
    // このファイルではフィールド間にタブを使用します
    // authorREGEX.dataファイル用: 
    name:Julia Child	gender:F
    // bookREGEX.datファイル用:
    name:Simca's Cuisine:100 Classic French Recipes for Every Occasion	year:1972	ISBN:0-394-40152-2
    // authorBookREGEX.datファイル用: 
    bname:Simca's Cuisine:100 Classic French Recipes for Every Occasion	aname:Simone Beck
  • データ入力ファイルを指定します。変数inputfiledirは、入力ファイルのディレクトリー名を指定します。識別された各ファイルは、読み込みに使用されます。
    // DATA INPUT
    // データ入力ソースを定義します 
    // inputfiledirは入力ファイルのディレクトリーです
    
    inputfiledir = '/tmp/REGEX/'
    authorInput = File.text(inputfiledir + "authorREGEX.dat").regex("name:(.*)\\tgender:([MF])").header('name', 'gender')
    bookInput = File.text(inputfiledir + "bookREGEX.dat").
    	regex("name:(.*)\\tyear:([0-9]{4})\\tISBN:([0-9]{1}[-]{1}[0-9]{3}[-]{1}[0-9]{5}[-]{1}[0-9]{0,1})").
    	header('name', 'year', 'ISBN')
    authorBookInput = File.text(inputfiledir + "authorBookREGEX.dat").regex("bname:(.*)\\taname:(.*)").header('bname', 'aname')

    プロパティ・キーnameは、authorBookファイルの頂点ラベルauthorおよびbookの両方に使用されるため、変数anameおよびbnameは著者名および書名にそれぞれ使用されます。これらの変数は、author頂点とbook頂点間の辺を作成するためのマッピング・ロジックで使用されます。

  • 各行で、ファイルはtextファイルとして指定され、ファイル名が指定され、デリミターが設定されます。読み込まれるフィールドを識別するには、ヘッダーを指定する必要があります。また、regexを使用してテキスト・ファイルの各行を解析するため、regexロジックが含まれます。マップauthorInputが作成され、データの処理に使用されます。このマップは、transformsを使用して読み込む前に操作できます。
    authorInput = File.text(inputfiledir + "authorREGEX.dat").regex("name:(.*)\\tgender:([MF])").header('name', 'gender')
  • マッピング・スクリプトの本文を作成します。マッピング・スクリプトのこの部分は、ファイル形式にかかわらず同じです。
  • ドライ・ランとしてテキスト読み込みにDSE Graph Loaderを実行するには、以下のコマンドを使用します。
    graphloader authorBookMappingREGEX.groovy -graph testREGEX -address localhost -dryrun true

    テスト目的の場合、graphloaderの実行前に、指定されたグラフが存在する必要はありません。ただし、実稼働用途では、graphloaderを使用する前にグラフとスキーマを作成する必要があります。

  • 完全な読み込みスクリプトは以下のとおりです。
    /* SAMPLE INPUT - タブを使用します
    author:
    name:Julia Child	gender:F
    book : 
    name:Simca's Cuisine:100 Classic French Recipes for Every Occasion	year:1972	ISBN:0-394-40152-2
    authorBook: 
    bname:Simca's Cuisine:100 Classic French Recipes for Every Occasion	aname:Simone Beck
     */
    
    // CONFIGURATION
    // スキーマを作成するようにデータ・ローダーを構成します
    config create_schema:true, load_new:true, load_threads: 3
    
    // DATA INPUT
    // データ入力ソース(コマンドライン引数で指定可能なファイル)を定義します
    // inputfiledirはコマンドラインで指定された入力ファイルのディレクトリーです
    // as the "-filename" option
    inputfiledir = '/tmp/REGEX/'
    authorInput = File.text(inputfiledir + "authorREGEX.dat").regex("name:(.*)\\tgender:([MF])").header('name', 'gender')
    bookInput = File.text(inputfiledir + "bookREGEX.dat").
    	regex("name:(.*)\\tyear:([0-9]{4})\\tISBN:([0-9]{1}[-]{1}[0-9]{3}[-]{1}[0-9]{5}[-]{1}[0-9]{0,1})").
    	header('name', 'year', 'ISBN')
    authorBookInput = File.text(inputfiledir + "authorBookREGEX.dat").regex("bname:(.*)\\taname:(.*)").header('bname', 'aname')
    
    //(定義済みインラインとして)いずれかのマッパーを使用して読み込むデータ・ソースを指定します
      
    load(authorInput).asVertices {
    label "author"
    key "name"
    }
    
    load(bookInput).asVertices {
    label "book"
    key "name"
    }
    
    load(authorBookInput).asEdges {
    label "authored"
    outV "aname", {
    label "author"
    key "name"
        }
    inV "bname", {
    label "book"
    key "name"
        }
    }