カナダのStudy Permits(学生ビザ、学生パーミット)の有効期限について

カナダのStudy Permitの失効について

カナダの学生に関する規則に関しては、司法局のWebサイト(この記事の中程にlinkを記載)にある、Table Contentsの PART12 - Students に記載されている。 特に、Study Permitの失効に関してはその中の No.222 - DIVISION 5 に記載されいている。 以下、No.222より。(誤訳、解釈の誤りなどがある可能性があるのでこの記事に貼ったlinkで原文を確認して欲しい)

  • Study Permitは以下の内、いずれか一番早い日付が失効日となる
    1. 学業が修了した日(卒業した日?)から90日目の日
    2. removal orderが失効可能になった日
    3. Permitの有効期限が切れた日

上記を調べるために得た知識を、以下にまとめておく。

法律、規則の確認

確かな確証を得たいのであれば、日本で言う法務省のサイトを確認するしかない。 カナダでは直訳では司法省となる「Department of Justice」のWebサイトで法律、規則等を確認できる。 https://www.justice.gc.ca/eng/

移民および難民保護規則

1. Table of Contents(目次)

Immigration and Refugee Protection Regulations

https://laws-lois.justice.gc.ca/eng/regulations/SOR-2002-227/index.html

2. Study Permitsの有効性と満期について

以下のNo.222に該当規則が記載されている。(2019.4月現在)

210 PART 12 - Students - No. 222 - DIVISION 5 - Validity and Expiry of Study Permits https://laws-lois.justice.gc.ca/eng/regulations/SOR-2002-227/section-222.html

No.222に記載されている、Study Permitsの文章

Invalidity

222 (1) A study permit becomes invalid upon the first to occur of the following days:

(a) the day that is 90 days after the day on which the permit holder completes their studies,

(b) the day on which a removal order made against the permit holder becomes enforceable, or

(c) the day on which the permit expires.

Marginal note:Exception

(2) Paragraph (1)(a) does not apply to

(a) a person described in any of paragraphs 300(2)(a) to (i); or

(b) a family member of a foreign national who resides in Canada and is described in any of paragraphs 215(2)(a) to (i).

SOR/2014-14, s. 16.

DockerコンテナでPHP練習環境構築

用意した動機

Udemyのphpのコースを受講し始めたが、そのコースではCodeanywhereを前提としていた。Codeanywhere自体はネットワーク環境さえ問題なければ使いやすいのだが、無料期間が一週間のため自前でphpの簡易な練習環境を用意した。

www.udemy.com

PHP実行環境

  • Dockerコンテナで用意した。
  • 今回はシンプルにApacheをappサーバーとしたイメージを利用している。
  • コンテナ起動時にコンテナのルートディレクトリとホストのディレクトリを共有ディレクトリにすることで、ホスト側で普段使用しているテキストエディターでPHPファイルを編集、追加して作業する。
  • ローカルなので、サーバーはファイル一覧を表示するようにしている。

1. git cloneをする

github.com

2. Dockerfileからイメージを作成(Dockerfileに修正がなければ初回のみ)

  • イメージ作成
docker build -t php-pg .
  • イメージ確認
docker images

3. 共有ディレクトリを指定してコンテナ起動

docker run -it -d --name php-pg -v ${PATH}/contents:/var/www/html/ -p 8080:80 php-pg
docker run -it -d -v /home-dir-path/php/php-playgrounds/contents:/var/www/html/ --name php-pg -p 8080:80 php-pg

4. ブラウザで確認

http://localhost:8080/ にアクセスしてHello, World!が表示されることを確認する

5. 実際に練習

  • 3.で指定したホストの共有ディレクトリ内で作業する。元々あるindex.phpを編集してもいいし、新規にファイルやディレクトリを作成するなどしてもいい。

  • http://localhost:8080/をブックマークしておくと作業効率がいい。

その他

Apacheのファイルの一覧表示の設定の該当箇所。(Indexesを追加)

php-playground/apache2.conf at d18b69900bfb5723a4b7dc07e9dc0da10f3eeab9 · mizushou/php-playground · GitHub

18.6501x Fundamentals of Statistics(Unit3) チェックリスト

Unit3 Methods for estimation

What you learned

Lec8: Distance measures between distributions

  • Unit2までは、estimatorをsample aveとして直感的に決めてきた
  • 今回は、最適なestimatorを決める手法を学ぶ
  • 今までは、sample aveの期待値がLLNよりパラメーターに収束する場合だけしか、ほぼ扱ってこなかった。
  • そのため、sample aveはestimatorとして活用できた。
  • しかし、パラメーターの値に収束しない場合は、estimatorは何にすればよいだろうか?
  • 大きく、次の3つの方法が考えられる。
    1. Maximum likelihood estimation(最尤法)
    2. Method of moments
    3. M-estimators
  • Total variation distance(TV)
    • これはいわゆる、距離
  • Kullback-Leibler divergence(KL)
    • 相対エントロピーとしても有名
    • 確率測度間の距離を最小化問題は、KLを使って考える
    • TVは距離であったが、KLは距離の定義を満たさないので距離ではない、divergenceと呼ばれる
    • KLの最小 ⇔ likelihoodの最大値。This is the maximum likelihood principle
  • Likelihood
    • データとパラメーターを引数にとる関数
    • 値は確率or確率密度と考えていい。joint pmf or joint pdf

Lec9: Introduction to Maximum Likelihood Estimation

  • 以下の確率変数のlikelihoodを計算

    • Bernoulli
    • Poisson
    • Gussian
    • Exponenssial
    • Uniform
  • Maximum likelihood estimator(MLE)

    • log-likelihood estimatorは実際に計算するときに便利なのでよく使う
  • 一般的な教科書はだいたいminimizingで書かれているが、この授業ではmaximizingで進める
  • concave(上に凸)/convex(下に凸)の判定
    • gradientの導入
    • Hessian matrixの導入
    • Hessian matrixからconcave/convexを判定
  • 実際にMLEを計算
    • Bernoulli
    • Poisson
    • Gaussian

What you noticed

  • concave/convexの判定は、Hessian matrixから数値化、または固有値を求めて判断
  • MLEを計算する際は、log-likelihoodを使うことが大半。便利だから。
  • パラメーターが1つの場合は、高校数学と同じ。

その他

  • ベクトル解析を少し復習が必要

参考文献

自然科学の統計学 (基礎統計学)

自然科学の統計学 (基礎統計学)

プログラミングのための線形代数

プログラミングのための線形代数

Dockerメモ

やったこと

  • 事前準備

    • DockerをUbuntu上にインストール
      • 以下の記事を参考にインストール
        • しかし、記事の注意事項に書いてあるとおり商用版Docker EEをリリースしたことにより無償版はDocker CEに名称変更。
          • これにより、パッケージ名がdocker-engineからdocker-ceに。インストール手順はほぼ変らないが、気持ち悪いのでアンインストールして再度ceをインストールし直した。
      • non-root userでDockerをwork betterにするための設定
      • 自動起動の設定はしなかった
  • 講座・ハンズオン

    • Dockerの仕組みのかんたんな紹介
    • Dockerを使うために最低限理解しておくこと
    • Step.1 公開されているイメージを使う
      • MySQL5.6
      • MySQL5.7
    • Step.2 Dockerfileからイメージを作成し使う
      • サンプルのDockerfileからphpを実行するAPコンテナを作成
    • Step.3 イメージを組み合わせて使う
      • サンプルのDockerfileからWebサーバ環境(コンテナの組合せ)を作成

インストール

1. アンインストール

  • まず、インストールしてしまったdocker-engineをアンインストール
sudo apt-get purge docker-engine
sudo rm -rf /var/lib/docker

2. SET UP THE REPOSITORY

dockerの公式ドキュメントの通りにインストール

https://docs.docker.com/install/linux/docker-ce/ubuntu/#uninstall-docker-ce

  • Update the apt package index:
sudo apt-get update
  • install packages to allow apt to use a repository over HTTPS:
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  • Verify that you now have the key with the fingerprint
sudo apt-key fingerprint 0EBFCD88

pub   4096R/0EBFCD88 2017-02-22
      Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid                  Docker Release (CE deb) <docker@docker.com>
sub   4096R/F273FCD8 2017-02-22
  • Set up the stable repository
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

3.INSTALL DOCKER CE

  • Update the apt package index.
sudo apt-get update
  • Install the latest version of Docker CE
sudo apt-get install docker-ce
  • Verify that Docker CE is installed correctly by running the hello-world image.
sudo docker container run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

4. non-root userでDockerをwork betterにするための設定

以下の理由より、インストールしただけだとsudoを常にしないいけなにので、めんどう

The Docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The Docker daemon always runs as the root user.

  • Create the docker group
sudo groupadd docker
  • Add your user to the docker group
sudo usermod -aG docker $USER
  • Log out and log back in so that your group membership is re-evaluated

  • Verify that you can run docker commands without sudo

docker run hello-world

基本コマンド

systemctlコマンド

  • docker service起動
❯ systemctl start docker.service
  • docker service停止
❯ systemctl stop docker.service
  • docker service ステータス確認
❯ systemctl status docker.service

dockerコマンド

  • Dockerfileからイメージをビルド
    • ${PATH}はDockerfileがあるパス
docker build -t ${REPOSITORY}[:${TAG}] ${PATH}
  • コンテナ起動、停止、再起動
docker start | stop | restart ${コンテナID}
  • 起動中のコンテナプロセス表示(-aオプションで停止中のプロセスも表示)
docker ps
  • コンテナのログを表示
docker logs ${コンテナID}
  • コンテナ内でコマンド実行
docker exec ${コンテナID} ${コマンド}
  • コンテナへのログイン
    • exec はすでに起動しているコンテナでコマンドを実行する。 run コマンドは新たにコンテナを起動してからそのコンテナでコマンドを実行する。
docker exec -it ${コンテナID} bash
  • コンテナ削除
    • 削除前に対象コンテナの停止が必要
docker rm ${コンテナID}
  • イメージを削除
docker rmi ${イメージID}
  • Run a Docker Container
    • runコマンドはコマンド実行中のみコンテナを起動。コマンド実行終了後にはコンテナはendする。
docker run ${イメージ名 || イメージID} ${command to run into container}
  • e.g. catコマンドを実行
docker run ubuntu cat /etc/issue
  • 初回の動作確認
❯ sudo docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
  • Run an Interactive Session into a Container
    • いわゆるコンテナへのログイン
    • -i : start an interactive session
    • -t : allocates a tty and attaches stdin and stdout
    • ubuntu : the image that we used to create the container
    • bash (or /bin/bash) : the command that we are running inside the Ubuntu container
docker run -it ubuntu bash
  • 起動しているコンテナのセッションからhostのターミナルに戻るには2通りある。
    • 1 exit
      • exit はコンテナの全てのセッションを終了して、そのコンテナ自体も停止させる
    • 2 Ctrl+p or Ctrl+q
      • セッションを維持しつつ、hostのターミナルに戻りたい場合はこのキーを使う
      • 新規セッションを作ってこのキーで出たりを繰り返すと、その分だけコンテナが起動しセッションも生成されてしまうので注意。元のセッションへの戻り方は次の項目に記載
❯ docker run -it ubuntu bash
root@7519130e117b:/# exit
exit

~
❯ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

~
❯ docker run -it ubuntu bash
root@f0dffe57d9ce:/#   
~ 27s
❯ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f0dffe57d9ce        ubuntu              "bash"              31 seconds ago      Up 29 seconds                           friendly_stonebraker
  • 再度、コンテナに戻る方法
    • まず、 docker ps でID or nameを確認する。
    • そして以下のコマンドで元のセッションのコンテナに接続し直す
    • exec でコンテナに入るのと同じこと?
docker attach ${コンテナID or name}
  • 稼働しているコンテナをホストから停止する
docker kill ${コンテナID or name}

docker-composeコマンド

  • 複数イメージを組合せたコンテナ群(docker-compose.yml)を起動
docker-compose up -d
  • docker-composeで起動した各コンテナの状態を確認
docker-compose ps
  • コンテナ群の起動、停止、再起動
docker-compose start | stop | restart
  • コンテナを停止して、削除
docker-compose down

Docker用語

  • Dockerイメージ
  • Dockerコンテナ

ハンズオン

Step.1 公開されているイメージを使うパターン

  • mysqlのイメージを使う
  • 使い方はdocker hubのイメージのページにだいたい書いてある
  • 実際にpullするイメージのDockerfileはGithubで確認できる

https://hub.docker.com/_/mysql

1. mysqlのdockerイメージ取得

  • docker hubのページでsupportされているタグを確認
  • タグ指定無しはバージョンlatast扱いになる
  • debianベースのmysqlサーバー

  • 構文

docker pull ${REPOSITORY}[:${TAG}]
  • バージョン(5.6)を指定してdockerイメージを取得
docker pull mysql:5.6

2. dockerイメージを確認

  • 取得したイメージを確認する
docker images

3. 取得したdockerイメージからmysqlコンテナ起動

  • dockerイメージからコンテナを起動
  • 上記で確認した${REPOSITORY}[:${TAG}]を指定する
  • 今回の起動コマンドの基本構文は以下の通り

  • 基本構文

docker run -d --name ${NAME} -e MYSQL_ROOT_PASSWORD=my-secret-pw ${REPOSITORY}[:${TAG}]
  • ${NAME} : コンテナに割り当てたい任意の名前
  • my-secret-pw : MySQLのrootユーザーのパスワード
  • ${TAG} : MySQLのバージョンを明示をする際に付ける
  • -d : コンテナをバックグラウンド実行

docker run -d --name mysql5.6 -e MYSQL_ROOT_PASSWORD=password mysql:5.6

4. mysqlコンテナ内でコマンド実行

  • login shellとしてbashを実行(コンテナにログイン)
docker exec -it some-mysql bash
  • 他のコマンド
docker exec -it mysql5.6 ls -al

5. mysqlサーバーのログを見る

docker logs some-mysql

Step.2 Dockerfileからイメージを作成するパターン

1. Dockerfileをビルドしイメージを作成する

docker build -t ${REPOSITORY}[:${TAG}] ${PATH}
docker build -t php .
  • tag : イメージのバージョンを明示する際に付ける

2. dockerイメージを確認

  • 取得したイメージを確認する
docker images

3. 上記で作成したイメージからコンテナを起動する

  • 基本構文
docker run -d --name ${NAME} ${REPOSITORY}[:${TAG}] -p ${HOST PORT: CONTAINER PORT}
  • -d : コンテナをバックグラウンド実行

docker run -d --name php -p 80:80 php

Step.3 docker-compose.ymlからイメージを組合せて作成するパターン

  • docker-compose.ymlがあるディレクトリをカレントにする

  • docker-composeコマンドを実行する

sudo docker-compose up –d
  • ホストで使用するポートを専有している場合は、サービスを停止しておく
    • 今回はMySQL(3306)が起動しており、コンテナ起動失敗した

18.6501x Fundamentals of Statistics(Unit1-2) チェックリスト

Unit1 Introduction to Statistics

What you learned

Lec1: What is statistics

Lec2: Probability Redux

  • Sample average
    • estimatorとして使う
  • probabilistic tools
    1. LLN(Laws(weak and strong) of large numbers)
      • a.s. convergence
      • Convergence in probability
    2. CLT(Central limit theorem)
      • Convergence in distribution
    3. Hoeffinding's inequality
      • sample size nが小さくても使える。(n=1でもいい)
      • CLTが使えない時の代り、ただし精度はCLTほどでない
    4. Consistent estimator
    5. Gaussian distribution
      • PDF, CDF
      • Affine transformation
      • Standardization
      • Symmetry
      • Table(CDF of Standard normal distribution)
      • Quantiles
    6. Three types of convergence
      1. Almost surely(a.s.) convergence
      2. Convergence in probability
      3. Convergence in distribution
    7. Addition, multiplication, division
      • Almost surely(a.s.) convergence and Convergence in probability
    8. Addition, multiplication, division (Slutsky's theorem)
      • Convergence in distribution
    9. Continuous mapping theorem

What you noticed

  • sample averageにCLTを適用することで、Gaussian distributionに分布収束する。その際sampleのr.v.はGaussianである必要はない、任意の分布のr.v.でも大丈夫
  • sample sizeが小さくてCLT適用できない時は、Hoeffinding's inequality
  • CLTもHoeffinding's inequalityもestimatorであるsample averageがunknownな母集団の期待値にどれくれい近いかを測るために使う

その他

参考文献


Unit2 Parametric Inference

What you learned

Lec3: Parametric Statistical Models

  • Trinity of statistical inference
    1. Estimation
    2. Confidence intervals
    3. Hypothesis testing
  • The goal of statistics is to learn the distribution of r.v
  • discrete r.v.s

ja.wikipedia.org

ja.wikipedia.org

ja.wikipedia.org

ja.wikipedia.org

ja.wikipedia.org

  • statistical model is a pair of sample space and a family of probilty distributions.
  • well specified
  • parametric
  • non-parametric
  • semi parametric is a hybrid model
    • nuisance parameter (撹乱母数、迷惑母数)
  • Linear regression model (線形回帰モデル)
  • Cox proportional Hazard model (コックス比例ハザードモデル) 生存モデル
  • identifiable

Lec4: Parametric Estimation and Confidence Intervals

  • Definitions
    • Statistic
      • Any measurable function of the sample
      • Rule of thumb : if you can compute it exactly once given data, it is measurable.
    • Estimator of theta
      • Any statistic whose expression does not depend on theta(data)
    • weakly (resp. strongly) consistent estimatorの条件
    • asymptotically normalの条件
      • estimatorはr.v. そのestimatorも正規分布に近似できる。
      • 近似した際の、分散をasymptotic variance
  • Bias of an estimator
  • Risk (or quadratic risk)
    • varianceとbiasを求めて、これを求めるという流れ
    • MSEと同じ意味合いだけと思うけど、言葉は区別した方いいのかな
  • Confidence intervals(C.I.)
    • confidence interval of level 1 - alpha for theta
      • any random interval whose boundaries do not depend on theta
      • true value theta が、interval内である確率が1 - alpha 以上のintervalのこと
    • C.I. of asymptotic level 1 - alpha for theta
      • any random interval whose boundaries do not depend on theta
      • sample size nの極限を取った時に、上記のような条件を満たすintervalのこと
  • A confidence interval for the kiss example
    • sample spaceの分布がBer(p)の場合
    • CLTより、estimator(sample ave)を標準正規分布に近似がスタート
    • 標準正規分布への近似だけでは、完璧なC.I.は求まらない。なぜならパラメーターに依存した形だから。(今回の場合は、true value p)
    • 次の3つの方法で求める
      1. Solution 1. Conservative bound
      2. Solution 2. Solving the (quadratic) equation for p
        • 実際は、解の公式よりコンピューター計算
      3. Solution 3. plug-in
        • Slutskyより、true vale pの代りにestimatorをplug-inして求める

What you noticed

  • どの分布が適切かを選択するのが、statistical modelingの第一歩
  • その際に、離散な確率変数であれば「台」に注目するのもポイント。有限個なのか無限個なのか

Lec5: Delta Method and Confidence Intervals

  • C.I.の復習

    • 95%,98%の区間があるからといって、必ずしも98%区間の方が広いわけではない
    • 同じ50%のC.I.でも区間の広さは異なる。正規分布の形から区間の中点を正規分布の中心に持ってくる時、一番区間を小さくできる
    • n → ∞にした時に成立するものをasymptotic confidence intervalと呼ぶ。(つまりn=1の時などは成立しない)
    • [0.34, 0.57]が95% confidence interval、と言われた時どう捉えるか?
      • この区間にunknownなパラメーターpが入る確率は0,1。0.95ではない。
      • realizationしたC.I.には注意
      • それでも、[0.34, 0.57]を95%のC.I.と呼ぶので注意。
      • これはあくまでも1 - alpha = 0.95でrandom C.I.をdeterministicな区間にrealizationしただけ
  • Red line TのKenall stでの待ち時間のモデル(delta method)

    • 電車の到着間の時間を計測する(つまり次の電車が来るまでの待ち時間)
    • この各待ち時間をモデル化する
    • 以下の様に仮定する
      • Mutually independent
      • パラメーターlambdaの指数分布
    • この時、lambdaをestimateする
    • lack of memory
      • why would I use exponential?
        • It's a very common distribution for inter-arrival times
        • main reason "lack of memory"
    • exponentialのexpectationからわかるように、LLN -> CLTを適用しても、単純にsample aveをestimatorにしてただけではlambdaのestimateできない
    • ここで、delta methodの登場

ja.wikipedia.org

  • delta method
    • this is important
    • 確率変数の列がthetaで正規分布に分布収束するとする
      • この時、この列をasymptotically normal around thetaと言う
    • 次に、thetaでcontinuously differentiableな関数gを考える
    • 上記の確率変数の列をこの関数に関しても、正規分布へ分布収束する
    • delta methodの導出にはtaylor展開を使う
    • 指数分布の場合は、estimatorをsample aveの逆数を取る。このestimateの時にLNN,CLTに加えてdelta methodを使う
  • frequentist interpretation
    • 複数回試行を行ったとき、true value lambdaがC.I.に入る確率は95%
    • 1111011101111..のような結果になる。

What you noticed

  • パラメーターに依存するrandom intervalは実際はC.I.ではない
  • 3つのsolutionを用いて、数値化(realizations)したendpoint間のintervalがC.I.
  • このように、まずC.I.はrandomなのか、realizationしたdeterministicな区間なのかをまず区別する
  • HW2より。正規分布の確率変数の列の和も正規分布になる
  • HW2でガウス分布登場

ja.wikipedia.org

Firebase使い始め [Androidアプリの追加]

  • Firebaseのプロジェクトを作成したら、そのプロジェクトに自分が作成しているアプリを追加する必要がある。
  • 今回は作成中のAndroidのアプリを追加してみる。

What you will learn

  1. Add Firebase to your Android app

Requirements

  • Device/Emulator :
    1. Android Device API level9+(Android 2.3)
      1. Play Service9.0+
        • setting -> app -> GooglePlay servicesでバージョンを確認
  • Android SDK(Android Studio) : 以下の要件を満たすことでFirebase client libraryをビルド可能になる
    1. Google Plya services rev 30+
    2. Google Repository rev 26+
      1. Android Studioを開き、SDK Mangerを開く。
      2. Appearance & Behavoir -> System Settings -> Android SDKを開き、中央のSDK Toolsを開く。
      3. 以下を確認
        1. Google Plya servicesのrevが30+
        2. Google Repositoryのrevが26+

FirebaseにAndroidアプリ追加手順

1. Firebaseコンソールから作成したプロジェクトを選択

2. Androidアプリの追加

f:id:mizushou:20190213174349p:plain

2-1. アプリの情報を入力する

  • コンソール上部のAndroidのマークのボタンをクリック
  • 以下の情報を入力する
    1. Androidパッケージ名
      • build.gradle(app)のapplicationIdに記載されているもの(念の為)
    2. デバッグ用証明書のSHA1フィンガプリント (SHA-1 hash of your debug key)
      • 2-2.のフィンガプリントの取得方法へ

2-2.デバッグ用証明書のSHA1フィンガプリントを取得

  • 2-1.で求められたデバッグ用証明書のフィンガプリントをAndroidStudioの機能を用いて取得する。コマンドでも取得できるがこっちのほうが楽。
  • AndroidStudioの右側におそらくあるはずの[Gradle]を開く
  • {プロジェクト名}/app/Tasks/android/signingReportをダブルクリックで実行
  • そうすると、AndroidStudio上で証明書の情報が表示される。
  • その中にSHA1の値がある。これを2-1のアプリ情報としてコピペする

f:id:mizushou:20190213174716p:plain

2-3. google-services.json(設定ファイル)を配置

  • パッケージ名とフィンガプリントを入力したらregisterする

f:id:mizushou:20190213173920p:plain

  • そうすると、jsonファイルをダウンロードできる

f:id:mizushou:20190213174058p:plain

  • jsonファイルをダウンロードしたら、appフォルダ配下に配置
    • AndroidStudioの場合は、[Project]に切り替えて、appフォルダに配置すればいい

2-4. Add dependencies(Firebase SDKの追加)

  • Firebase client libraryをインポート・ビルドするためにgradle fileにdependenciesを追加
  • dependenciesをすべて追加してから、最後にsyncする
  • 追加するdependenciesのバージョンは以下のページで最新版を確認

firebase.google.com

2-4-1. build.gradle(module)
dependencies {
    ...
    classpath 'com.google.gms:google-services:4.0.0'
    ...
}
2-4-2. build.gradle(app)
  • ライブラリの追加(Firebaseの機能を追加するたびに追加する)
  • パス : /app/build.gradle (app配下にある)
    1. 最終行に追加
    2. dependenciesに使用するpluginを追加。最初はcore(analtics)を追加
//これを最終行に
apply plugin: 'com.google.gms.google-services'

dependencies {
    ....
    // Firebase dependencies
    implementation 'com.google.firebase:firebase-core:16.0.1'
}

2-4. 確認

  • アプリを起動し、logcatで確認
    • FirebaseApp initialization successfulが出ていることを確認してひとまず完了
2018-06-14 13:16:33.947 4655-4655/com.example.shouhei.mlkitdemo I/FirebaseInitProvider: FirebaseApp initialization successful

f:id:mizushou:20190213174425p:plain

Firebase使い始め [プロジェクト作成]

What you will learn

  1. Firebaseのプロジェクト作成
    • プロジェクト名の設定
    • Analyticsのロケーションの選択
    • Cloud Firestoreのロケーションの選択

Terminology

cloud.google.com

  • ロケーション(location)
    • 北米、南米、ヨーロッパ、アジア、オーストラリアなど地理的な範囲を示す
    • 各々のロケーションはリージョンに分割される。さらにリージョンはゾーンに分割される。
  • リージョン(region)
    • ゾーンから構成される地理的エリア。
    • リージョン内は95%がネットワーク遅延 < 1msに抑えられるらしい
  • ゾーン(zone)
    • リージョン内のCloud Platformリソースのデプロイメントエリア
    • リージョン内の単一障害点となる
    • 高可用性を保つには複数のゾーンにデプロイする必要がある

cloud.google.com

1.プロジェクトのロケーションについて

1-1. ロケーションの種類

  • ロケーションはマルチリージョン(multi-region location)と(単一)リージョン(single, regional location)の2種類から選択できる。

    1. マルチリージョンは一般的な国単位の地理的エリアをイメージすればいい。例えばアメリカなど。
      • Cloud Firebase databaseの高可用性と冗長性を最大限にできる。
      • マルチリージョンが使用可能なのは、現在はアメリカのみ。(2019年2月現在)
    2. リージョンは都市など特定の地理的場所をイメージするといい。例えば、サウスカロライナ州など。
      • データはリージョン内の複数のゾーンにレプリケーションされる。
      • リージョンを選択した時は、ネットワーク遅延や他のGCPリソース利用する場合などに注意を払った方がいい。
  • 個人的にFirebaseを試してみる場合などは、特にそこまで気にすることはない。自分の住んでいる一番近い都市を選択すればよいと思う。

  • 以下、雑な絵。以下の3つのサービスはプロジェクトのロケーションを共有するらしい。

    • Cloud Firestore
    • Cloud Storage
    • App Engine

f:id:mizushou:20190206231423p:plain

1-2. 選択するロケーション

  1. Analytics location

Analytics location, which represents the country/region of your organization

--

  1. Cloud Firestore location

Cloud Firestore location, which represents where your data is stored by Google Cloud Platform services that support data location, like Cloud Firestore

--

Once you select a location for your project, you cannot change it.

1-3. ロケーションをどう決めるか

  • Analytics location
    • これは収益レポートを作る際の通貨に関わってくるので、実際に組織がある国を選択すればよさそう
    • このロケーションは、データがどのリージョンのFirebaseに蓄積されるかなどには影響しない。つまり、次で設定するFirebaseのロケーションとは関係しない。
  • Cloud Firestore location
    • 選択したロケーションがCloud Firestoreにも適用される。パフォーマンスや可用性などを考慮して実際にユーザーに近い場所やサービスとして必要とされる場所を選択したほうがよさそう。

2. プロジェクト作成手順

ロケーションについてある程度理解したので、実際にプロジェクトを作成してみる。作成自体は一瞬。Firebase(https://firebase.google.com/)開いて、コンソールへ移動。プロジェクト作成をクリックして、以下を設定し作成。

  1. プロジェクト名(任意)
  2. Analytics location
  3. Cloud Firestore location