|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.gravitino.abs.fs; |
| 21 | + |
| 22 | +import com.google.common.annotations.VisibleForTesting; |
| 23 | +import com.google.common.collect.ImmutableMap; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Map; |
| 26 | +import javax.annotation.Nonnull; |
| 27 | +import org.apache.gravitino.catalog.hadoop.fs.FileSystemProvider; |
| 28 | +import org.apache.gravitino.catalog.hadoop.fs.FileSystemUtils; |
| 29 | +import org.apache.gravitino.storage.ABSProperties; |
| 30 | +import org.apache.hadoop.conf.Configuration; |
| 31 | +import org.apache.hadoop.fs.FileSystem; |
| 32 | +import org.apache.hadoop.fs.Path; |
| 33 | + |
| 34 | +public class AzureFileSystemProvider implements FileSystemProvider { |
| 35 | + |
| 36 | + @VisibleForTesting public static final String ABS_PROVIDER_SCHEME = "abfss"; |
| 37 | + |
| 38 | + @VisibleForTesting public static final String ABS_PROVIDER_NAME = "abs"; |
| 39 | + |
| 40 | + private static final String ABFS_IMPL = "org.apache.hadoop.fs.azurebfs.SecureAzureBlobFileSystem"; |
| 41 | + |
| 42 | + private static final String ABFS_IMPL_KEY = "fs.abfss.impl"; |
| 43 | + |
| 44 | + @Override |
| 45 | + public FileSystem getFileSystem(@Nonnull Path path, @Nonnull Map<String, String> config) |
| 46 | + throws IOException { |
| 47 | + Configuration configuration = new Configuration(); |
| 48 | + |
| 49 | + Map<String, String> hadoopConfMap = |
| 50 | + FileSystemUtils.toHadoopConfigMap(config, ImmutableMap.of()); |
| 51 | + |
| 52 | + if (config.containsKey(ABSProperties.GRAVITINO_ABS_ACCOUNT_NAME) |
| 53 | + && config.containsKey(ABSProperties.GRAVITINO_ABS_ACCOUNT_KEY)) { |
| 54 | + hadoopConfMap.put( |
| 55 | + String.format( |
| 56 | + "fs.azure.account.key.%s.dfs.core.windows.net", |
| 57 | + config.get(ABSProperties.GRAVITINO_ABS_ACCOUNT_NAME)), |
| 58 | + config.get(ABSProperties.GRAVITINO_ABS_ACCOUNT_KEY)); |
| 59 | + } |
| 60 | + |
| 61 | + if (!config.containsKey(ABFS_IMPL_KEY)) { |
| 62 | + configuration.set(ABFS_IMPL_KEY, ABFS_IMPL); |
| 63 | + } |
| 64 | + |
| 65 | + hadoopConfMap.forEach(configuration::set); |
| 66 | + |
| 67 | + return FileSystem.get(path.toUri(), configuration); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String scheme() { |
| 72 | + return ABS_PROVIDER_SCHEME; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String name() { |
| 77 | + return ABS_PROVIDER_NAME; |
| 78 | + } |
| 79 | +} |
0 commit comments