Hibernate を使用することで、オブジェクト指向のドメインモデルを従来のリレーショナルデータベースにマッピングすることができます。以下のチュートリアルでは、CData JDBC Driver for ADLS を使用し、Hibernate でAzure Data Lake Storage リポジトリのORM を生成する方法を説明します。
この記事を通してEclipse がIDE として選択されていますが、CData JDBC Driver for ADLS はJava Runtime Environment をサポートするすべての
製品で機能します。Knowledge Base には、IntelliJ IDEA やNetBeans からAzure Data Lake Storage に接続するためのチュートリアルがあります。
Hibernate をインストール
以下のステップに従って、Eclipse にHibernate プラグインをインストールします。
- Eclipse で、[Help]->[Install New Software]と進みます。
- Work With ボックスに[http://download.jboss.org/jbosstools/neon/stable/updates/] と入力します。
- フィルタボックスに、[Hibernate]と入力します。
- [Hibernate Tools]を選択します。
新しいプロジェクトを開始
以下のステップに従って、新しいプロジェクトにドライバーJARs を追加します。
- 新しいプロジェクトを作成します。プロジェクトタイプとして[Java Project]を選択し、[Next]をクリックします。プロジェクト名を入力し、[Finish]をクリックします。
- [project]を右クリックし、[Properties]を選択します。[Java Build Path]をクリックし、[Libraries]タブを開きます。
- [Add External JARs]をクリックし、インストールディレクトリのlib サブフォルダにあるcdata.jdbc.adls.jar ライブラリを追加します。
Hibernate 構成ファイルを追加
以下のステップに従って、Azure Data Lake Storage に接続プロパティを構成します。
- [new project]で右クリックし、[New]->[Hibernate]->[Hibernate Configuration File (cfg.xml)]と進みます。
- 親フォルダとして[src]を選択し、[Next]をクリックします。
以下の値をインプットします。
- Hibernate version:5.2
- Database dialect:Derby
- Driver class: cdata.jdbc.adls.ADLSDriver
Connection URL:jdbc:adls: で始まり、セミコロンで区切られた接続プロパティのリストが続くJDBC URL
Authenticating to a Gen 1 DataLakeStore Account
Gen 1 uses OAuth 2.0 in Azure AD for authentication.
For this, an Active Directory web application is required. You can create one as follows:
- Sign in to your Azure Account through the
[.
]- Select "Azure Active Directory".
- Select "App registrations".
- Select "New application registration".
- Provide a name and URL for the application. Select Web app for the type of application you want to create.
- Select "Required permissions" and change the required permissions for this app. At a minimum, "Azure Data Lake" and "Windows Azure Service Management API" are required.
- Select "Key" and generate a new key. Add a description, a duration, and take note of the generated key. You won't be able to see it again.
To authenticate against a Gen 1 DataLakeStore account, the following properties are required:
- Schema: Set this to ADLSGen1.
- Account: Set this to the name of the account.
- OAuthClientId: Set this to the application Id of the app you created.
- OAuthClientSecret: Set this to the key generated for the app you created.
- TenantId: Set this to the tenant Id. See the property for more information on how to acquire this.
- Directory: Set this to the path which will be used to store the replicated file. If not specified, the root directory will be used.
Authenticating to a Gen 2 DataLakeStore Account
To authenticate against a Gen 2 DataLakeStore account, the following properties are required:
- Schema: Set this to ADLSGen2.
- Account: Set this to the name of the account.
- FileSystem: Set this to the file system which will be used for this account.
- AccessKey: Set this to the access key which will be used to authenticate the calls to the API. See the property for more information on how to acquire this.
- Directory: Set this to the path which will be used to store the replicated file. If not specified, the root directory will be used.
ビルトイン接続文字列デザイナ
JDBC URL の構成については、Azure Data Lake Storage JDBC Driver に組み込まれている接続文字列デザイナを使用してください。JAR ファイルのダブルクリック、またはコマンドラインからJAR ファイルを実行します。
java -jar cdata.jdbc.adls.jar
接続プロパティを入力し、接続文字列をクリップボードにコピーします。
以下は一般的なJDBC URL です。
jdbc:adls:Schema=ADLSGen2;Account=myAccount;FileSystem=myFileSystem;AccessKey=myAccessKey;InitiateOAuth=GETANDREFRESH
Hibernate のAzure Data Lake Storage データへの接続
以下のステップに従って、前の手順で作成した構成を選択します。
- [Window]->[Open Perspective]->[Hibernate]と進み、Hibernate Configurations パースペクティブに切り替えます。
- [Hibernate Configurations]パネルで右クリックし、[Add Configuration]を選択します。
- Hibernate version を5.2に設定します。
- [Browse]ボタンをクリックし、[project]を選択します。
- [Configuration file]フィールドで、[Setup]->[Use Existing]とクリックし、(このデモのsrc フォルダー内にある)hibernate.cfg.xml ファイルを選択します。
- [Classpath tab]で、[User Entries]の下に何もなかった場合、[Add External JARS]をクリックしてドライバーjar をもう一度追加します。構成が完了したら、[OK]をクリックします。
- 新しく作成されたHibernate 構成ファイルのDatabase node を展開します。
Azure Data Lake Storage データをリバースエンジニアリング
以下のステップに従って、reveng.xml 構成ファイルを生成します。アクセスするテーブルをオブジェクトとして指定します。
- Package Explorer に戻ります。
- プロジェクトを右クリックし、[New]->[Hibernate]->[Hibernate Reverse Engineering File (reveng.xml)]と進みます。[Next]をクリックします。
- 親フォルダとして[src]を選択し、[Next]をクリックします。
- [Console configuration drop-down]メニューで、上で作成した[Hibernate configuration]ファイルを選択し、[Refresh]をクリックします。
- ノードを展開し、リバースエンジニアリングするテーブルを選択します。完了したら、[Finish]をクリックします。
実行するためのHibernate を構成
以下のステップに従って、plain old Java objects (POJO) をAzure Data Lake Storage テーブルに生成します。
- メニューバーから、[Run]->[Hibernate Code Generation]->[Hibernate Code Generation Configurations]と進みます。
- [Console configuration drop-down]メニューで、前のセクションで作成した[Hibernate configuration]ファイルを選択します。Output ディレクトリで[Browse]をクリックし、src を選択します。
- [JDBC Connection]チェックボックスから[Reverse Engineer]を有効にします。[Setup]ボタンをクリックし、[Use Existing]をクリックして(このデモのsrcフォルダ内にある) hibernate.reveng.xml ファイルの場所を選択します。
- [Exporters]タブで[Domain code (.java)]と[Hibernate XML Mappings (hbm.xml)]をチェックします。
- [Run]をクリックします。
POJO は、前のステップのリバースエンジニアリング設定に基づいて作成されます。
マッピングタグを挿入
生成したマッピングごとにhibernate.cfg.xml にマッピングタグを作成し、Hibernate がマッピングリソースを指定するようにする必要があります。hibernate.cfg.xml を開き、次のようにマッピングタグを挿入します。
cdata.adls.ADLSDriver
jdbc:adls:Schema=ADLSGen2;Account=myAccount;FileSystem=myFileSystem;AccessKey=myAccessKey;InitiateOAuth=GETANDREFRESH
org.hibernate.dialect.SQLServerDialect
SQLを実行
前の手順で作成したエンティティを使用して、Azure Data Lake Storage のsearchを行うことができます。
import java.util.*;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
public class App {
public static void main(final String[] args) {
Session session = new
Configuration().configure().buildSessionFactory().openSession();
String SELECT = "FROM Resources R WHERE Type = :Type";
Query q = session.createQuery(SELECT, Resources.class);
q.setParameter("Type","FILE");
List<Resources> resultList = (List<Resources>) q.list();
for(Resources s: resultList){
System.out.println(s.getFullPath());
System.out.println(s.getPermission());
}
}
}