hadoop-cos-{hadoop.version}-{version}.jar
とcos_api-bundle-{version}.jar
を$HADOOP_HOME/share/hadoop/tools/lib
にコピーします。$HADOOP_HOME/etc/hadoop
ディレクトリに移動し、hadoop-env.shファイルを編集して、以下の内容を追加し、cosn関連のjarパッケージをHadoop環境変数に追加します。for f in $HADOOP_HOME/share/hadoop/tools/lib/*.jar; doif [ "$HADOOP_CLASSPATH" ]; thenexport HADOOP_CLASSPATH=$HADOOP_CLASSPATH:$felseexport HADOOP_CLASSPATH=$ffidone
プロパティキー | 説明 | デフォルト値 | 入力必須項目 |
fs.cosn.userinfo.
secretId/secretKey | なし | はい | |
fs.cosn.
credentials.provider | SecretIdとSecretKeyの取得方法を設定します。現在、5つの取得方法がサポートされています。 1. org.apache.hadoop.fs.auth.SessionCredential
Provider:リクエストURIからsecret idとsecret keyを取得します。その形式は、次のとおりです。cosn://{secretId}:{secretKey}@examplebucket-1250000000/。 2. org.apache.hadoop.fs.auth.SimpleCredentialProvider:core-site.xml設定ファイルからfs.cosn.userinfo.secretIdとfs.cosn.userinfo.secretKeyを読み込み、SecretIdとSecretKeyを取得します。 3. org.apache.hadoop.fs.auth.EnvironmentVariableCredential
Provider:システム環境変数のCOS_SECRET_IDとCOS_SECRET_KEYから取得します。 4. org.apache.hadoop.fs.auth.SessionTokenCredentialProvider:一時キー形式を使用してアクセスします。 5. org.apache.hadoop.fs.auth.CVMInstanceCredentialsProvider:Tencent CloudのCloud Virtual Machine(CVM)にバインドされたロールを使用して、COSにアクセスするための一時キーを取得します 6. org.apache.hadoop.fs.auth.CPMInstanceCredentialsProvider:Tencent CloudのCloud Physical Machine(CPM)にバインドされたロールを使用して、COSにアクセスするための一時キーを取得します。 7. org.apache.hadoop.fs.auth.EMRInstanceCredentialsProvider:Tencent Cloud EMRインスタンスにバインドされたロールを使用して、COSにアクセスするための一時キーを取得します。 8. org.apache.hadoop.fs.auth.RangerCredentialsProviderは、rangerを使用してキーを取得します。 | この設定項目が指定されていない場合、デフォルトで
以下の順序で読み込みます。 1. org.apache.hadoop.fs.auth.SessionCredentialProvider 2. org.apache.hadoop.fs.auth.SimpleCredentialProvider 3. org.apache.hadoop.fs.auth.EnvironmentVariableCredentialProvider 4. org.apache.hadoop.fs.auth.SessionTokenCredentialProvider 5. org.apache.hadoop.fs.auth.CVMInstanceCredentialsProvider 6. org.apache.hadoop.fs.auth.CPMInstanceCredentialsProvider 7. org.apache.hadoop.fs.auth.EMRInstanceCredentialsProvider | いいえ |
fs.cosn.useHttps | COSバックエンドでのトランスポートプロトコルとしてHTTPSを使用するかどうかを設定します。 | true | いいえ |
fs.cosn.impl | FileSystem用のcosn実装クラス、org.apache.hadoop.fs.CosFileSystemに固定されます。 | なし | はい |
fs.AbstractFileSystem.
cosn.impl | AbstractFileSystem用のcosn実装クラス、org.apache.hadoop.fs.CosNに固定されます。 | なし | はい |
fs.cosn.bucket.region | アクセスするバケットのリージョン情報を入力してください。列挙値については、リージョンとアクセスドメイン名のリージョンの略称をご参照ください。
例えば、ap-beijing、ap-guangzhouなどです。元の設定:fs.cosn.userinfo.regionと互換性があります。 | なし | はい |
fs.cosn.bucket.
endpoint_suffix | 接続するCOS endpointを指定します。この項目は入力必須項目ではありません。パブリッククラウドCOSユーザーの場合、
上記のregionの設定を正しく入力するだけです。元の設定:fs.cosn.userinfo.endpoint_suffixと互換性があります。この項目を有効にするために、設定時にfs.cosn.bucket.regionの設定項目のendpointを削除してください。 | なし | いいえ |
fs.cosn.tmp.dir | 実際に存在するローカルディレクトリを設定してください。プロセス実行中に生成された一時ファイルは、一時的にここに置かれます。 | /tmp/hadoop_cos | いいえ |
fs.cosn.upload.
part.size | CosNファイルシステムの各blockのサイズです。マルチパートアップロードされた各part sizeのサイズでもあります。COSはマルチパートアップロードで最大10,000ブロックまでしかサポートできないため、使用できる最大1ファイルサイズを推定する必要があります。
例えば、part sizeが8MBの場合、最大で78GBの単一ファイルのアップロードに対応します。part sizeは最大2GBまで対応可能で、1ファイルあたり最大19TBまで対応できます。 | 8388608(8MB) | いいえ |
fs.cosn.
upload.buffer | CosNファイルシステムがアップロード時に依存するバッファのタイプです。現在、非ダイレクトメモリバッファ(non_direct_memory)
ダイレクトメモリバッファ(direct_memory)、ディスクマップバッファ(mapped_disk)という3種類のバッファがサポートされています。非ダイレクトメモリバッファ
エリアではJVMヒープメモリが使用されますが、ダイレクトメモリバッファエリアではオフヒープメモリが使用されます。ディスクマップバッファはメモリファイルマッピングをベースとするバッファエリアです。 | mapped_disk | いいえ |
fs.cosn.
upload.buffer.size | CosNファイルシステムがアップロードに依存するバッファのサイズです。-1に指定されている場合、バッファに制限がないことを意味します。
バッファサイズが制限されていない場合、バッファタイプはmapped_diskである必要があります。指定されたサイズが0より大きい場合、この値は少なくとも1つのblockのサイズ以上である必要があります。 元の設定:fs.cosn.buffer.sizeと互換性があります。 | -1 | いいえ |
fs.cosn.block.size | CosNファイルシステムのblock sizeです。 | 134217728(128MB) | いいえ |
fs.cosn.
upload_thread_pool | ファイルがCOSにストリーミングされるときの同時アップロードスレッドの数です。 | 10 | いいえ |
fs.cosn.
copy_thread_pool | ディレクトリのコピー操作中にファイルを同時にコピーおよび削除するために使えるスレッドの数です。 | 3 | いいえ |
fs.cosn.
read.ahead.block.size | 先読みブロックのサイズです。 | 1048576(1MB) | いいえ |
fs.cosn.
read.ahead.queue.size | 先読みキューの長さです。 | 8 | いいえ |
fs.cosn.maxRetries | COSへのアクセス中にエラーが発生した場合の最大再試行回数です。 | 200 | いいえ |
fs.cosn.retry.
interval.seconds | 各再試行間の時間間隔です。 | 3 | いいえ |
fs.cosn.
server-side-encryption.algorithm | COSサーバー側の暗号化アルゴリズムを設定します。SSE-CとSSE-COSをサポートしており、デフォルトでは空で、暗号化なしです。 | なし | いいえ |
fs.cosn.
server-side-encryption.key | COSのSSE-Cサーバー暗号化アルゴリズムを有効にする場合は、SSE-Cのキーを設定する必要があります。
キーの形式は、base64でエンコードされたAES-256キーです。デフォルトでは空で、暗号化なしです。 | なし | いいえ |
fs.cosn.
crc64.checksum.enabled | CRC64チェックを有効にするかどうかです。デフォルトでは有効になっていません。現時点では、hadoop fs -checksumコマンドを使用してファイルのCRC64チェックサムを取得することはできません。 | false | いいえ |
fs.cosn.
crc32c.checksum.enabled | CRC32Cチェックを有効にするかどうかです。デフォルトでは有効になっていません。現時点では、hadoop fs -checksumコマンドを使用してファイルのCRC32Cチェックサムを取得することはできません。有効にできるチェック方法は、crc32cかcrc64のうち1つだけです。 | false | いいえ |
fs.cosn.traffic.limit | アップロード帯域幅の制御オプション。819200 - 838860800 bits/s、デフォルト値は-1です。これは、デフォルトでは制限がないことを意味します。 | なし | いいえ |
$HADOOP_HOME/etc/hadoop/core-site.xml
を変更して、COS関連のユーザーおよび実装クラスの情報を追加します。次に例を示します。<configuration><property><name>fs.cosn.credentials.provider</name><value>org.apache.hadoop.fs.auth.SimpleCredentialProvider</value><description>This option allows the user to specify how to get the credentials.Comma-separated class names of credential provider classes which implementcom.qcloud.cos.auth.COSCredentialsProvider:1.org.apache.hadoop.fs.auth.SessionCredentialProvider: Obtain the secret id and secret key from the URI: cosn://secretId:secretKey@examplebucket-1250000000/;2.org.apache.hadoop.fs.auth.SimpleCredentialProvider: Obtain the secret id and secret keyfrom fs.cosn.userinfo.secretId and fs.cosn.userinfo.secretKey in core-site.xml;3.org.apache.hadoop.fs.auth.EnvironmentVariableCredentialProvider: Obtain the secret id and secret keyfrom system environment variables named COS_SECRET_ID and COS_SECRET_KEY.If unspecified, the default order of credential providers is:1. org.apache.hadoop.fs.auth.SessionCredentialProvider2. org.apache.hadoop.fs.auth.SimpleCredentialProvider3. org.apache.hadoop.fs.auth.EnvironmentVariableCredentialProvider4. org.apache.hadoop.fs.auth.SessionTokenCredentialProvider5. org.apache.hadoop.fs.auth.CVMInstanceCredentialsProvider6. org.apache.hadoop.fs.auth.CPMInstanceCredentialsProvider7. org.apache.hadoop.fs.auth.EMRInstanceCredentialsProvider</description></property><property><name>fs.cosn.userinfo.secretId</name><value>xxxxxxxxxxxxxxxxxxxxxxxxx</value><description>Tencent Cloud Secret Id</description></property><property><name>fs.cosn.userinfo.secretKey</name><value>xxxxxxxxxxxxxxxxxxxxxxxx</value><description>Tencent Cloud Secret Key</description></property><property><name>fs.cosn.bucket.region</name><value>ap-xxx</value><description>The region where the bucket is located.</description></property><property><name>fs.cosn.bucket.endpoint_suffix</name><value>cos.ap-xxx.myqcloud.com</value><description>COS endpoint to connect to.For public cloud users, it is recommended not to set this option, and only the correct area field is required.</description></property><property><name>fs.cosn.impl</name><value>org.apache.hadoop.fs.CosFileSystem</value><description>The implementation class of the CosN Filesystem.</description></property><property><name>fs.AbstractFileSystem.cosn.impl</name><value>org.apache.hadoop.fs.CosN</value><description>The implementation class of the CosN AbstractFileSystem.</description></property><property><name>fs.cosn.tmp.dir</name><value>/tmp/hadoop_cos</value><description>Temporary files will be placed here.</description></property><property><name>fs.cosn.upload.buffer</name><value>mapped_disk</value><description>The type of upload buffer. Available values: non_direct_memory, direct_memory, mapped_disk</description></property><property><name>fs.cosn.upload.buffer.size</name><value>134217728</value><description>The total size of the upload buffer pool. -1 means unlimited.</description></property><property><name>fs.cosn.upload.part.size</name><value>8388608</value><description>Block size to use cosn filesysten, which is the part size for MultipartUpload.Considering the COS supports up to 10000 blocks, user should estimate the maximum size of a single file.For example, 8MB part size can allow writing a 78GB single file.</description></property><property><name>fs.cosn.maxRetries</name><value>3</value><description>The maximum number of retries for reading or writing files toCOS, before we signal failure to the application.</description></property><property><name>fs.cosn.retry.interval.seconds</name><value>3</value><description>The number of seconds to sleep between each COS retry.</description></property><property><name>fs.cosn.server-side-encryption.algorithm</name><value></value><description>The server side encryption algorithm.</description></property><property><name>fs.cosn.server-side-encryption.key</name><value></value><description>The SSE-C server side encryption key.</description></property></configuration>
<property><name>fs.defaultFS</name><value>cosn://examplebucket-1250000000</value><description>This option is not advice to config, this only used for some special test cases.</description></property>
$HADOOP_HOME/etc/hadoop/core-site.xml
ファイルに以下の設定を追加すると、SSE-COSの暗号化を実装することができます。<property><name>fs.cosn.server-side-encryption.algorithm</name><value>SSE-COS</value><description>The server side encryption algorithm.</description></property>
$HADOOP_HOME/etc/hadoop/core-site.xml
ファイルに以下の設定を追加して、SSE-Cの暗号化を実装することができます。<property><name>fs.cosn.server-side-encryption.algorithm</name><value>SSE-C</value><description>The server side encryption algorithm.</description></property><property><name>fs.cosn.server-side-encryption.key</name><value>MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=</value> #ユーザーはSSE-Cのキーを自分で設定する必要があり、キーの形式はbase64でエンコードされたAES-256キーです。<description>The SSE-C server side encryption key.</description></property>
hadoop fs -ls -R cosn://<BucketName-APPID>/<パス>
、またはhadoop fs -ls -R /<パス>
(fs.defaultFS
オプションはcosn://BucketName-APPID
に設定する必要あり)です。次の例では、examplebucket-1250000000という名のbucketを例として取り上げ、その後に具体的なパスを追加することができます。hadoop fs -ls -R cosn://examplebucket-1250000000/-rw-rw-rw- 1 root root 1087 2018-06-11 07:49 cosn://examplebucket-1250000000/LICENSEdrwxrwxrwx - root root 0 1970-01-01 00:00 cosn://examplebucket-1250000000/hdfsdrwxrwxrwx - root root 0 1970-01-01 00:00 cosn://examplebucket-1250000000/hdfs/2018-rw-rw-rw- 1 root root 1087 2018-06-12 03:26 cosn://examplebucket-1250000000/hdfs/2018/LICENSE-rw-rw-rw- 1 root root 2386 2018-06-12 03:26 cosn://examplebucket-1250000000/hdfs/2018/ReadMedrwxrwxrwx - root root 0 1970-01-01 00:00 cosn://examplebucket-1250000000/hdfs/test-rw-rw-rw- 1 root root 1087 2018-06-11 07:32 cosn://examplebucket-1250000000/hdfs/test/LICENSE-rw-rw-rw- 1 root root 2386 2018-06-11 07:29 cosn://examplebucket-1250000000/hdfs/test/ReadMe
bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.2.jar wordcount cosn://example/mr/input cosn://example/mr/output3
File System CountersCOSN: Number of bytes read=72COSN: Number of bytes written=40COSN: Number of read operations=0COSN: Number of large read operations=0COSN: Number of write operations=0FILE: Number of bytes read=547350FILE: Number of bytes written=1155616FILE: Number of read operations=0FILE: Number of large read operations=0FILE: Number of write operations=0HDFS: Number of bytes read=0HDFS: Number of bytes written=0HDFS: Number of read operations=0HDFS: Number of large read operations=0HDFS: Number of write operations=0Map-Reduce FrameworkMap input records=5Map output records=7Map output bytes=59Map output materialized bytes=70Input split bytes=99Combine input records=7Combine output records=6Reduce input groups=6Reduce shuffle bytes=70Reduce input records=6Reduce output records=6Spilled Records=12Shuffled Maps =1Failed Shuffles=0Merged Map outputs=1GC time elapsed (ms)=0Total committed heap usage (bytes)=653262848Shuffle ErrorsBAD_ID=0CONNECTION=0IO_ERROR=0WRONG_LENGTH=0WRONG_MAP=0WRONG_REDUCE=0File Input Format CountersBytes Read=36File Output Format CountersBytes Written=40
package com.qcloud.chdfs.demo;import org.apache.commons.io.IOUtils;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileChecksum;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import java.io.IOException;import java.net.URI;import java.nio.ByteBuffer;public class Demo {private static FileSystem initFS() throws IOException {Configuration conf = new Configuration();// COSNの設定項目については、https://www.tencentcloud.com/document/product/436/6884?from_cn_redirect=1#hadoop-.E9.85.8D.E7.BD.AEをご参照ください// 以下の設定は、入力必須項目ですconf.set("fs.cosn.impl", "org.apache.hadoop.fs.CosFileSystem");conf.set("fs.AbstractFileSystem.cosn.impl", "org.apache.hadoop.fs.CosN");conf.set("fs.cosn.tmp.dir", "/tmp/hadoop_cos");conf.set("fs.cosn.bucket.region", "ap-guangzhou");conf.set("fs.cosn.userinfo.secretId", "AKXXXXXXXXXXXXXXXXX");conf.set("fs.cosn.userinfo.secretKey", "XXXXXXXXXXXXXXXXXX");conf.set("fs.ofs.user.appid", "XXXXXXXXXXX");// その他の設定については、公式サイトのドキュメントhttps://www.tencentcloud.com/document/product/436/6884?from_cn_redirect=1#hadoop-.E9.85.8D.E7.BD.AEをご参照ください// CRC64チェックを有効にするかどうかです。デフォルトでは有効になっていません。この時点では、hadoop fs -checksumコマンドを使用してファイルのCRC64チェックサムを取得することはできませんconf.set("fs.cosn.crc64.checksum.enabled", "true");String cosnUrl = "cosn://f4mxxxxxxxx-125xxxxxxx";return FileSystem.get(URI.create(cosnUrl), conf);}private static void mkdir(FileSystem fs, Path filePath) throws IOException {fs.mkdirs(filePath);}private static void createFile(FileSystem fs, Path filePath) throws IOException {// ファイルを作成します(存在する場合は上書きします)// if the parent dir does not exist, fs will create it!FSDataOutputStream out = fs.create(filePath, true);try {// ファイルに書き込みますString content = "test write file";out.write(content.getBytes());} finally {IOUtils.closeQuietly(out);}}private static void readFile(FileSystem fs, Path filePath) throws IOException {FSDataInputStream in = fs.open(filePath);try {byte[] buf = new byte[4096];int readLen = -1;do {readLen = in.read(buf);} while (readLen >= 0);} finally {IOUtils.closeQuietly(in);}}private static void queryFileOrDirStatus(FileSystem fs, Path path) throws IOException {FileStatus fileStatus = fs.getFileStatus(path);if (fileStatus.isDirectory()) {System.out.printf("path %s is dir\\n", path);return;}long fileLen = fileStatus.getLen();long accessTime = fileStatus.getAccessTime();long modifyTime = fileStatus.getModificationTime();String owner = fileStatus.getOwner();String group = fileStatus.getGroup();System.out.printf("path %s is file, fileLen: %d, accessTime: %d, modifyTime: %d, owner: %s, group: %s\\n",path, fileLen, accessTime, modifyTime, owner, group);}private static void getFileCheckSum(FileSystem fs, Path path) throws IOException {FileChecksum checksum = fs.getFileChecksum(path);System.out.printf("path %s, checkSumType: %s, checkSumCrcVal: %d\\n",path, checksum.getAlgorithmName(), ByteBuffer.wrap(checksum.getBytes()).getInt());}private static void copyFileFromLocal(FileSystem fs, Path cosnPath, Path localPath) throws IOException {fs.copyFromLocalFile(localPath, cosnPath);}private static void copyFileToLocal(FileSystem fs, Path cosnPath, Path localPath) throws IOException {fs.copyToLocalFile(cosnPath, localPath);}private static void renamePath(FileSystem fs, Path oldPath, Path newPath) throws IOException {fs.rename(oldPath, newPath);}private static void listDirPath(FileSystem fs, Path dirPath) throws IOException {FileStatus[] dirMemberArray = fs.listStatus(dirPath);for (FileStatus dirMember : dirMemberArray) {System.out.printf("dirMember path %s, fileLen: %d\\n", dirMember.getPath(), dirMember.getLen());}}// 再帰的削除フラグは、ディレクトリを削除するために用いられます// 再帰がfalseで、dirが空でない場合、操作は失敗しますprivate static void deleteFileOrDir(FileSystem fs, Path path, boolean recursive) throws IOException {fs.delete(path, recursive);}private static void closeFileSystem(FileSystem fs) throws IOException {fs.close();}public static void main(String[] args) throws IOException {// ファイルの初期化FileSystem fs = initFS();// ファイルの作成Path cosnFilePath = new Path("/folder/exampleobject.txt");createFile(fs, cosnFilePath);// ファイルの読み取りreadFile(fs, cosnFilePath);// ファイルまたはディレクトリの照会queryFileOrDirStatus(fs, cosnFilePath);// ファイルのチェックサムの取得getFileCheckSum(fs, cosnFilePath);// ローカルからファイルをコピーするPath localFilePath = new Path("file:///home/hadoop/ofs_demo/data/exampleobject.txt");copyFileFromLocal(fs, cosnFilePath, localFilePath);// ファイルをローカルで取得するPath localDownFilePath = new Path("file:///home/hadoop/ofs_demo/data/exampleobject.txt");copyFileToLocal(fs, cosnFilePath, localDownFilePath);listDirPath(fs, cosnFilePath);// リネームmkdir(fs, new Path("/doc"));Path newPath = new Path("/doc/example.txt");renamePath(fs, cosnFilePath, newPath);// ファイルの削除deleteFileOrDir(fs, newPath, false);// ディレクトリの作成Path dirPath = new Path("/folder");mkdir(fs, dirPath);// ディレクトリにファイルを作成するPath subFilePath = new Path("/folder/exampleobject.txt");createFile(fs, subFilePath);// ディレクトリのリストアップlistDirPath(fs, dirPath);// ディレクトリの削除deleteFileOrDir(fs, dirPath, true);deleteFileOrDir(fs, new Path("/doc"), true);// ファイルシステムを閉じるcloseFileSystem(fs);}}
この記事はお役に立ちましたか?