【Python】 Python ~ 基本編 / スライス操作 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2019/12/17/225829

の Python の資格勉強で、
スライスに関する理解がいまいちだったので
まとめてみる。

■ スライス

* シーケンス型オブジェクト(※)を指定した箇所で切り取って
 コピーを返してくれる機能
 => あくまで「コピー」で実体ではない
  (その証拠として「value[10] = "a"」はエラー)

※ シーケンス型オブジェクト

1) 文字列
2) リスト
3) タプル
4) レンジ

構文

[start:end:step] のように値が指定できる

使用上の注意

* 指定するインデックスは、値と値の間を指す点

value = "HELLO"

# HEL <= 「HELL」ではない
print(value[0:3])

# EL <= 「ELL」ではない
print(value[1:3])

イメージ

+-+-+-+-+-+
|H|E|L|L|O|
+-+-+-+-+-+
0 1 2 3 4 5

■ サンプル

例1:文字列

value = "Hello world!"

# w
print(value[6])

# r
print(value[-4])

# ell <= 注意:「ello」ではない
print(value[1:4])

# Hlowrd
print(value[0:20:2])

# <> <= エラーにはならない
print("<" + value[100:1000] + ">")

# TypeError: 'str' object does not support item assignment
# value[10] = "a"

参考文献
https://qiita.com/tanuk1647/items/276d2be36f5abb8ea52e

関連記事

Python ~ 基本編 / 文字列 ~
https://dk521123.hatenablog.com/entry/2019/10/12/075251
Python ~ 基本編 / リスト ~
https://dk521123.hatenablog.com/entry/2019/10/12/084943
Python ~ 基本編 / リスト・あれこれ ~
https://dk521123.hatenablog.com/entry/2020/11/01/000000
Python ~ 基本編 / 辞書 ~
https://dk521123.hatenablog.com/entry/2019/10/27/100014
Python ~ 基本編 / 辞書・あれこれ ~
https://dk521123.hatenablog.com/entry/2020/10/11/000000
Python ~ 基本編 / タプル・集合 ~
https://dk521123.hatenablog.com/entry/2019/10/26/000000
Python ~ 基本編 / コマンドライン引数 ~
https://dk521123.hatenablog.com/entry/2019/10/11/223651
Python に関する資格
https://dk521123.hatenablog.com/entry/2019/12/17/225829

【AWS】Amazon EMR ~ EMRFS ~

■ はじめに

https://dk521123.hatenablog.com/entry/2020/02/20/230519
https://dk521123.hatenablog.com/entry/2020/05/27/175610
https://dk521123.hatenablog.com/entry/2020/06/24/173334
https://dk521123.hatenablog.com/entry/2020/11/12/113312

の続き。

 今回は、emrfs syncコマンド を学んだので、
その周辺の知識を少しづつだが纏めていく。

目次

【1】EMRFS
【2】emrfs コマンド (EMRFS CLI)
 1)emrfs sync 
 2)emrfs describe-metadata
 3)emrfs delete-metadata
 4)emrfs create-metadata
 5)emrfs list-metadata-stores
 6)emrfs diff
 7)emrfs import

【1】EMRFS

* EMRFS (EMR File System; EMRファイルシステム)

* AWS S3 を HDFS(分散されたデータをメタ情報で管理)
 のように扱える
 + Hadoop -> HDFS (Hadoop Distributed File System)
 + EMR -> EMRFS (EMR File System)

* HDFS(Hadoop Distributed File System)については、
 以下の関連記事を参照のこと

Apache Hadoop ~ 基本編 / HDFS
https://dk521123.hatenablog.com/entry/2022/05/01/000000

【2】emrfs コマンド (EMRFS CLI)

リファレンス
https://docs.aws.amazon.com/ja_jp/emr/latest/ManagementGuide/emrfs-cli-reference.html
その他
https://docs.aws.amazon.com/ja_jp/emr/latest/ManagementGuide/emrfs-files-tracked.html

1)emrfs sync

* メタデータをリセットし、現在の S3 に存在するものを反映し同期する

コマンド例

emrfs sync s3://your-s3-backet/samples/xxxxx

補足:emrfs syncコマンドの用途

エラー「ConsistencyException(一貫性例外)」が発生した時に、
emrfs syncコマンドを実行して対応した。

なお、エラー内容などの詳細は、以下の関連記事を参照のこと。

https://dk521123.hatenablog.com/entry/2020/05/28/175428

2)emrfs describe-metadata

メタデータテーブルの詳細を表示

コマンド例

# デフォルトのメタデータテーブルの詳細を表示
emrfs describe-metadata

3)emrfs delete-metadata

* メタデータテーブルを削除

コマンド例

emrfs delete-metadata -m sample_table

4)emrfs create-metadata

* メタデータテーブルを作成

コマンド例

emrfs create-metadata -m sample_table

5)emrfs list-metadata-stores

* メタデータテーブルを表示

コマンド例

emrfs list-metadata-stores

6)emrfs diff

* メタデータの差分を比較する

コマンド例

# デフォルトのメタデータテーブルとAmazon S3 バケット上にあるメタデータを比較
emrfs diff s3://your-s3-bucket/sample_db/table_name

BOTH | MANIFEST ONLY | S3 ONLY <= 実際は「青|赤|緑」で色分けされる
DIR elasticmapreduce/samples/cloudfront
DIR elasticmapreduce/samples/cloudfront/code/
・・・

7)emrfs import

* メタデータをインポートする

コマンド例

# 指定したAmazon S3 バケット内のすべてのオブジェクトを
# 整合性のあるビューの追跡メタデータとともにインポートする
emrfs import s3://your-s3-bucket/sample_db/table_name

参考文献

https://dev.classmethod.jp/articles/cmstudy-amazon-emr/
https://agency-star.co.jp/column/hadoop

関連記事

Amazon EMR ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2020/02/20/230519
Amazon EMR ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2020/05/27/175610
Amazon EMR ~ boto3 編 ~
https://dk521123.hatenablog.com/entry/2020/06/24/173334
Amazon EMR ~ AWS Glueとの連携 ~
https://dk521123.hatenablog.com/entry/2020/11/12/113312
起動中の EMR に Session Manager を使って入る
https://dk521123.hatenablog.com/entry/2022/05/11/135632
Amazon EMR に関するトラブルシューティング
https://dk521123.hatenablog.com/entry/2020/08/05/144724
Hive に関するトラブルシューティング
https://dk521123.hatenablog.com/entry/2020/05/28/175428
Apache Hadoop ~ 基本編 / HDFS
https://dk521123.hatenablog.com/entry/2022/05/01/000000

【AWS】Amazon EMR ~ AWS Glueとの連携 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2020/02/20/230519
https://dk521123.hatenablog.com/entry/2020/05/27/175610
https://dk521123.hatenablog.com/entry/2020/06/24/173334

の続き。

今回は、EMRからAWS Glueを利用する際に必要なことを纏める

■ EMRからAWS Glueカタログを参照するには

EMRに以下の設定を行う必要がある

設定例抜粋 ("ReleaseLabel"="emr-5.29.0")

"Configurations" [
  {
    "Classification": "hive-site",
    "Properties": {
      "hive.metastore.client.factory.class": "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory",
      "hive.metastore.schema.verification": "false"
    }
  }
],

公式サイト

Specifying AWS Glue Data Catalog as the Metastore
(AWS Glueデータカタログをメタストアとして指定するには)

https://docs.aws.amazon.com/ja_jp/emr/latest/ReleaseGuide/emr-hive-metastore-glue.html

関連記事

Amazon EMR ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2020/02/20/230519
Amazon EMR ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2020/05/27/175610
Amazon EMR ~ boto3 編 ~
https://dk521123.hatenablog.com/entry/2020/06/24/173334
Amazon EMR ~ EMRFS ~
https://dk521123.hatenablog.com/entry/2020/11/13/145545
Amazon EMR に関するトラブルシューティング
https://dk521123.hatenablog.com/entry/2020/08/05/144724
AWS Glue ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/10/01/221926