なんとな~くしあわせ?の日記

「そしてそれゆえ、知識そのものが力である」 (Nam et ipsa scientia potestas est.) 〜 フランシス・ベーコン

AWS Glue上でboto3を使ってみる

現象

AWS Glue上でboto3を呼ぼうとしたのですが、あるはずのメソッドを呼ぶとエラーで落ちた。

対策

ここを読みました

Lambda pythonから S3 にアクセスできない?

試しに、Lambda Functionパッケージ内から boto3とbotocoreを
削除する。

削除した状態で Lmabda FunctionをDeployし、S3にファイルをアップロード。すると、SNS経由でLambdaに通知されたS3のKeyからデータを取得することに成功。

boto3をzipに含めたことが原因だったようです。。

分析

どうやらGlue上でboto3のライブラリを保持しているので、ライブラリをアップする必要はないようです。
boto3のバージョンは試してみると以下の通り:

スクリプト

import boto3

print(boto3.__version__)

出力

1.7.28
  • pip上でのboto3の最新版は1.7.28だったので、一応最新バージョンのようです。

pypi.org


ライブラリの管理が若干気持ち悪いかも?、Glue上のPythonは2.7.12のようです。

Clojure小ネタ

Clojure小ネタ

Clojureのテストはrepl.itというサイトでWEB上で試せる
repl.it - Online REPL, Compiler & IDE

ハッシュマップの型

  • {:key "value"} のような形のハッシュマップは、内部ではPersistentHashMapやPersistentArrayMap, PersistentTreeMapのような型を持ちます。
Clojure 1.8.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_91-b14
   (def m {:no "1" :name "山田" :tel "xxx-xxx-xxxx"})
=> #'user/m
   (println m)
{:no 1, :name 山田, :tel xxx-xxx-xxxx}
=> nil
   (println (type m))
clojure.lang.PersistentArrayMap
=> nil

マップの操作

  • キー/バリューの取り出し
    • keys, vals が使える
   (vals m)
=> ("1" "山田" "xxx-xxx-xxxx")
   (keys m)
=> (:no :name :tel)
  • 順序を変えた取り出し
(map (fn [key] (key m)) [:no :name :tel]) 
=> ("1" "山田" "xxx-xxx-xxxx")
   (map (fn [key] (key m)) [:tel :name :no]) 
=> ("xxx-xxx-xxxx" "山田" "1")
  • キーの削除
    • dissoc を使う
   (dissoc m :no)
=> {:name "山田", :tel "xxx-xxx-xxxx"}
  • 複数キーの削除
    • apply を使うとうまくいく apply無しでも動きました、何かと勘違いしていた?
   (dissoc m [:no :name])
=> {:tel "xxx-xxx-xxxx"}

ClojureでJavaの無名クラス実装

nantonaku-shiawase.hatenablog.com

以前JavaApache POIでCellWalkContextの実装を作ったのだが、これをClojureでやる場合はproxyを使うのがよい。

Java実装

		CellWalk cw = new CellWalk(sheet, range);
		cw.traverse(new CellHandler() {
			@Override
			public void onCell(Cell c, CellWalkContext ctx) {
				c.setCellType(CellType.BLANK);
			}
		});

Clojure実装

    (.traverse cw (proxy [CellHandler] []
                    (onCell [^Cell c ^CellWalkContext ctx]
                      (.setCellType c CellType/BLANK)
                      )))

    ))

Clojureにはproxyとreifyというものがあるが、Javaのメソッドに実装を入れたオブジェクトを突っ込む場合はproxyで動いた。

What is the difference between proxy and reify?