ablog

不器用で落着きのない技術者のメモ

Juputer Notebook を使って pandas でデータを生成する

Dataframe でデータを生成する

  • コード
import pandas as pd
 df = pd.DataFrame({ 'col1' : 1.,
                        'col2' : pd.Timestamp('20200101'),
                        'col3' : pd.Series(1,index=list(range(20)),dtype='float32') })
df
  • 実行結果

1分間隔のタイムスタンプを生成する

  • コード
import pandas as pd
df = pd.DataFrame({ 'A' :  pd.date_range("2020-01-01", periods=10, freq="T")})
  • 実行結果

f:id:yohei-a:20200523181357p:plain

カテゴリのデータをランダムに生成する

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.choice(['A','B','C'], size=(10)))
  • 実行結果

f:id:yohei-a:20200523182931p:plain

AWS Glue で Jupyter Notebook を使う

Project Jupyter

Project Jupyter (/ˈdʒuːpɪtər/ (About this soundlisten)) is a nonprofit organization created to "develop open-source software, open-standards, and services for interactive computing across dozens of programming languages". Spun-off from IPython in 2014 by Fernando Pérez, Project Jupyter supports execution environments in several dozen languages. Project Jupyter's name is a reference to the three core programming languages supported by Jupyter, which are Julia, Python and R, and also a homage to Galileo's notebooks recording the discovery of the moons of Jupiter. Project Jupyter has developed and supported the interactive computing products Jupyter Notebook, JupyterHub, and JupyterLab, the next-generation version of Jupyter Notebook.

Project Jupyter - Wikipedia

Jupyter Notebook

Jupyter Notebook (formerly IPython Notebooks) is a web-based interactive computational environment for creating Jupyter notebook documents. The "notebook" term can colloquially make reference to many different entities, mainly the Jupyter web application, Jupyter Python web server, or Jupyter document format depending on context. A Jupyter Notebook document is a JSON document, following a versioned schema, and containing an ordered list of input/output cells which can contain code, text (using Markdown), mathematics, plots and rich media, usually ending with the ".ipynb" extension.

A Jupyter Notebook can be converted to a number of open standard output formats (HTML, presentation slides, LaTeX, PDF, ReStructuredText, Markdown, Python) through "Download As" in the web interface, via the nbconvert library or "jupyter nbconvert" command line interface in a shell. To simplify visualisation of Jupyter notebook documents on the web, the nbconvert library is provided as a service through NbViewer which can take a URL to any publicly available notebook document, convert it to HTML on the fly and display it to the use

Project Jupyter - Wikipedia

Jupyter Notebookはサーバが提供する対話型開発環境内で維持されるライブドキュメントであるため、コード、出力、説明用テキストを含めることができます(図4)。Jupyterセッション内では、サーバがHTTPを使用して通常のWebブラウザにノートブックファイルをレンダリングし、レンダリング済みドキュメントの静的および動的コンテンツに対してはHTTPとWebsocketを組み合わせて使用します。バックエンドではサーバがオープンソースのメッセージングプロトコルであるZeroMQ(ØMQ)を使用してコード実行カーネルと通信します。
f:id:yohei-a:20200523173230p:plain
図4:Jupyterセッション内では、サーバがコードを実行するバックエンドサーバとやりとりしながら、ノートブックファイルの内容をWebブラウザレンダリングする。(画像提供:Project Jupyter)

PYNQ開発がFPGAベースのシステム設計を迅速化 | DigiKey
開発エンドポイントの作成

ワーカータイプ
開発エンドポイントに割り当てられる事前定義されたワーカーのタイプ。値として Standard、G.1X、または G.2X を使用します。

  • Standard ワーカータイプでは、各ワーカーが 4 vCPU、16 GB のメモリ、50 GB のディスク、ワーカーごとに 2 個のエグゼキュターを提供します。
  • G.1X ワーカータイプでは、各ワーカーが 1 DPU (4 vCPU、16 GB のメモリ、64 GB のディスク) にマッピングされ、ワーカーごとに 1 個のエグゼキュターを提供します。メモリを大量に消費するジョブには、このワーカータイプをお勧めします。
  • G.2X ワーカータイプでは、各ワーカーが 2 DPU (8 vCPU、32 GB のメモリ、128 GB のディスク) にマッピングされ、ワーカーごとに 1 個のエグゼキュターを提供します。メモリを大量に消費するジョブには、このワーカータイプをお勧めします。

既知の問題: G.2X WorkerType で開発エンドポイントを作成すると、開発エンドポイントの Spark ドライバーは 4 vCPU、16 GB のメモリ、および 64 GB のディスクで実行されます。


ワーカー数
開発エンドポイントに割り当てられた、 定義された workerType のワーカーの数。このフィールドは、ワーカータイプとして G.1X または G.2X を選択した場合にのみ使用できます。
定義可能なワーカーの最大数は、299 (G.1X) および 149 (G.2X) です。


データ処理単位 (DPU)
AWS Glue が開発エンドポイントに使用する DPU の数。数値は 1 より大きくする必要があります。

開発エンドポイントの追加 - AWS Glue

保存

  • [File] - [Download as] - [Notebook(ipynd)] でファイルをダウンロードする

f:id:yohei-a:20200523172020p:plain

  • Upload でアップロードする。

f:id:yohei-a:20200523172055p:plain

データを生成する

Amazon Chime のチャットで Markdown 記法を使う

Sending Markdown Messages
To send an Amazon Chime chat message using markdown syntax, type /md followed by a space at the beginning of your message. Compose your message using markdown syntax. Press Enter to send.
The following example demonstrates how to format an Amazon Chime chat message using markdown syntax.

/md **Hello world!**

The following example demonstrates how the sent message appears in Amazon Chime.

Hello world!


Sending Code Blocks in Messages
To send a code block in an Amazon Chime chat message, type /code followed by a space at the beginning of your message. Copy and paste your code block into the message. Press Enter to send.
The following example demonstrates how to send a code block in an Amazon Chime chat message.

/code CreateBotRequest createBotRequest = new CreateBotRequest()
    .withAccountId("chimeAccountId")
    .withDisplayName("exampleBot")
    .withDomain("example.com");

chime.createBot(createBotRequest);
The following example demonstrates how the sent message appears in Amazon Chime.

CreateBotRequest createBotRequest = new CreateBotRequest()
    .withAccountId("chimeAccountId")
    .withDisplayName("exampleBot")
    .withDomain("example.com");

chime.createBot(createBotRequest);

Using Chat Features - Amazon Chime

Python shell ジョブで six と python_dateutil をインストールする

閉域構成(ジョブにダミー接続をアタッチしてVPC内のみアクセス可能

  • Python ライブラリパス
s3://az2-pypi-org/six-1.14.0-py2.py3-none-any.whl,s3://az2-pypi-org/python_dateutil-2.8.1-py2.py3-none-any.whl
  • ログ
Processing ./glue-python-libs-y3k8nvty/six-1.14.0-py2.py3-none-any.whl
Installing collected packages: six
Successfully installed six-1.14.0
Processing ./glue-python-libs-y3k8nvty/python_dateutil-2.8.1-py2.py3-none-any.whl
  • エラーログ
ERROR: Could not find a version that satisfies the requirement six>=1.5 (from python-dateutil==2.8.1) (from versions: none)
ERROR: No matching distribution found for six>=1.5 (from python-dateutil==2.8.1)
Traceback (most recent call last):
  File "/tmp/runscript.py", line 112, in <module>
    download_and_install(args.extra_py_files)
  File "/tmp/runscript.py", line 62, in download_and_install
    subprocess.check_call([sys.executable, "-m", "pip", "install", "--target=
{}
".format(install_path), local_file_path])
  File "/usr/local/lib/python3.6/subprocess.py", line 311, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/usr/local/bin/python', '-m', 'pip', 'install', '--target=/glue/lib/installation', '/tmp/glue-python-libs-y3k8nvty/python_dateutil-2.8.1-py2.py3-none-any.whl']' returned non-zero exit status 1.

インターネットに接続可能な構成(ダミー接続をアタッチしない)

  • Python ライブラリパス
s3://az2-pypi-org/six-1.14.0-py2.py3-none-any.whl,s3://az2-pypi-org/python_dateutil-2.8.1-py2.py3-none-any.whl
  • ログ
2020-05-06 06:11:44
Processing ./glue-python-libs-s9pm1m37/six-1.14.0-py2.py3-none-any.whl
Installing collected packages: six
Successfully installed six-1.14.0
Processing ./glue-python-libs-s9pm1m37/python_dateutil-2.8.1-py2.py3-none-any.whl
Collecting six>=1.5
  Downloading https://files.pythonhosted.org/packages/65/eb/1f97cb97bfc2390a276969c6fae16075da282f5058082d4cb10c6c5c1dba/six-1.14.0-py2.py3-none-any.whl
Installing collected packages: six, python-dateutil
Successfully installed python-dateutil-2.8.1 six-1.14.0
1.13.0

閉域構成で six のみインストールしてバージョンを表示させてみる

import six as s
print(s.__version__)
  • Python ライブラリパス
s3://az2-pypi-org/six-1.14.0-py2.py3-none-any.whl
  • ログ
2020-05-16 13:39:03
Processing ./glue-python-libs-pfjk11d6/six-1.14.0-py2.py3-none-any.whl
Installing collected packages: six
Successfully installed six-1.14.0
1.13.01.14 をインストールしているのに、1.13 と表示されている

閉域構成で six をインストールせずにバージョンを表示する

import six as s
print(s.__version__)
print(s.__file__)
print(s.__loader__)
print(s.__package__ )
  • ログ
1.13.0
/usr/local/lib/python3.6/site-packages/six.py ★/glue/lib/installation ではなくデフォルトのライブラリパスを参照している
<_frozen_importlib_external.SourceFileLoader object at 0x7f102c3dad68>
six

EC2 で確認

$ pyenv versions
  system
* 3.6.9 (set by /home/ec2-user/.pyenv/version)
$ pip install six
Collecting six
  Using cached https://files.pythonhosted.org/packages/65/eb/1f97cb97bfc2390a276969c6fae16075da282f5058082d4cb10c6c5c1dba/six-1.14.0-py2.py3-none-any.whl
Installing collected packages: six
Successfully installed six-1.14.0
$ pip install python_dateutil
Collecting python_dateutil
  Using cached https://files.pythonhosted.org/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl
Requirement already satisfied: six>=1.5 in ./.pyenv/versions/3.6.9/lib/python3.6/site-packages (from python_dateutil) (1.14.0) ★インストール済と認識されている
Installing collected packages: python-dateutil
Successfully installed python-dateutil-2.8.1

設定

import six as s
print(s.__package__ )
print(s.__version__)
print(s.__file__)
print(s.__loader__)
  • Python ライブラリパス
s3://az2-pypi-org/six-1.14.0-py2.py3-none-any.whl,s3://az2-pypi-org/python_dateutil-2.8.1-py2.py3-none-any.whl
  • 接続
    • タイプ: JDBC
    • JDBC URL: jdbc:mysql://example.com:3306/example
    • VPC ID: vpc-******
    • サブネット: subnet-******
    • セキュリティグループ: sg-******
    • ユーザー名: dummy

MySQL で接続先サーバのホスト名を確認する

MySQL [(none)]> show variables like 'hostname';
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| hostname      | ip-10-7-1-22 |
+---------------+--------------+
1 row in set (0.01 sec)

Route 53 ヘルスチェッカーの IP アドレス範囲を調べる

Route 53 の現在の IP アドレス範囲を表示するには、ip-ranges.json をダウンロードし、ファイルで次の値を検索します。

(中略)

"service": "ROUTE53_HEALTHCHECKS"

これらの IP アドレス範囲は、Route 53 ヘルスチェッカーによって使用されます。Route 53 ヘルスチェックを使用してネットワークのリソースの正常性をチェックする場合は、これらの範囲をホワイトリストに追加します。

ヘルスチェックの IP アドレスの詳細については、「Amazon Route 53 のヘルスチェックができるようにルーターとファイアウォールのルールを設定する」を参照してください。

Amazon Route 53 サーバーの IP アドレス範囲 - Amazon Route 53

東京、シンガポールシドニーの Route53 ヘルスチェッカーの IP アドレスをリストしてみる。

$ curl -s https://ip-ranges.amazonaws.com/ip-ranges.json|jq -r '.prefixes[]|select(.service=="ROUTE53_HEALTHCHECKS" and ( .region=="ap-northeast-1" or .region=="ap-southeast-1" or .region=="ap-southeast-2"))|@text "\(.region)\t\(.ip_prefix)"'|sort
ap-northeast-1	54.248.220.0/26
ap-northeast-1	54.250.253.192/26
ap-southeast-1	54.251.31.128/26
ap-southeast-1	54.255.254.192/26
ap-southeast-2	54.252.254.192/26
ap-southeast-2	54.252.79.128/26