Files
impala/tests/util/s3_util.py
Sailesh Mukil ed7f5ebf53 IMPALA-1878: Support INSERT and LOAD DATA on S3 and between filesystems
Previously Impala disallowed LOAD DATA and INSERT on S3. This patch
functionally enables LOAD DATA and INSERT on S3 without making major
changes for the sake of improving performance over S3. This patch also
enables both INSERT and LOAD DATA between file systems.

S3 does not support the rename operation, so the staged files in S3
are copied instead of renamed, which contributes to the slow
performance on S3.

The FinalizeSuccessfulInsert() function now does not make any
underlying assumptions of the filesystem it is on and works across
all supported filesystems. This is done by adding a full URI field to
the base directory for a partition in the TInsertPartitionStatus.
Also, the HdfsOp class now does not assume a single filesystem and
gets connections to the filesystems based on the URI of the file it
is operating on.

Added a python S3 client called 'boto3' to access S3 from the python
tests. A new class called S3Client is introduced which creates
wrappers around the boto3 functions and have the same function
signatures as PyWebHdfsClient by deriving from a base abstract class
BaseFileSystem so that they can be interchangeably through a
'generic_client'. test_load.py is refactored to use this generic
client. The ImpalaTestSuite setup creates a client according to the
TARGET_FILESYSTEM environment variable and assigns it to the
'generic_client'.

P.S: Currently, the test_load.py runs 4x slower on S3 than on
HDFS. Performance needs to be improved in future patches. INSERT
performance is slower than on HDFS too. This is mainly because of an
extra copy that happens between staging and the final location of a
file. However, larger INSERTs come closer to HDFS permformance than
smaller inserts.

ACLs are not taken care of for S3 in this patch. It is something
that still needs to be discussed before implementing.

Change-Id: I94e15ad67752dce21c9b7c1dced6e114905a942d
Reviewed-on: http://gerrit.cloudera.org:8080/2574
Reviewed-by: Sailesh Mukil <sailesh@cloudera.com>
Tested-by: Internal Jenkins
2016-05-12 14:17:49 -07:00

100 lines
3.8 KiB
Python

# Copyright (c) 2016 Cloudera, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# S3 access utilities
#
# This file uses the boto3 client and provides simple functions to the Impala test suite
# to access Amazon S3.
import boto3
import copy
from tests.util.filesystem_base import BaseFilesystem
class S3Client(BaseFilesystem):
@classmethod
def __init__(self, bucket):
self.bucketname = bucket
self.s3 = boto3.resource('s3')
self.bucket = self.s3.Bucket(self.bucketname)
self.s3client = boto3.client('s3')
def create_file(self, path, file_data, overwrite=True):
if not overwrite and self.exists(filename): return False
self.s3client.put_object(Bucket=self.bucketname, Key=path, Body=file_data)
return True
def make_dir(self, path, permission=None):
# This function is a no-op. S3 is a key-value store and does not have a directory
# structure. We can use a non existant path as though it already exists.
pass
def copy(self, src, dst):
self.s3client.copy_object(Bucket=self.bucketname,
CopySource={'Bucket':self.bucketname, 'Key':src}, Key=dst)
# Since S3 is a key-value store, it does not have a command like 'ls' for a directory
# structured filesystem. It lists everything under a path recursively.
# We have to manipulate its response to get an 'ls' like output.
def ls(self, path):
if not path.endswith('/'):
path += '/'
# Use '/' as a delimiter so that we don't get all keys under a path recursively.
response = self.s3client.list_objects(
Bucket=self.bucketname, Prefix=path, Delimiter='/')
dirs = []
# Non-keys or "directories" will be listed as 'Prefix' under 'CommonPrefixes'.
if 'CommonPrefixes' in response:
dirs = [t['Prefix'] for t in response['CommonPrefixes']]
files = []
# Keys or "files" will be listed as 'Key' under 'Contents'.
if 'Contents' in response:
files = [t['Key'] for t in response['Contents']]
files_and_dirs = []
files_and_dirs.extend([d.split('/')[-2] for d in dirs])
for f in files:
key = f.split("/")[-1]
if not key == '':
files_and_dirs += [key]
return files_and_dirs
def get_all_file_sizes(self, path):
if not path.endswith('/'):
path += '/'
# Use '/' as a delimiter so that we don't get all keys under a path recursively.
response = self.s3client.list_objects(
Bucket=self.bucketname, Prefix=path, Delimiter='/')
if 'Contents' in response:
return [t['Size'] for t in response['Contents']]
return []
def exists(self, path):
response = self.s3client.list_objects(Bucket=self.bucketname,Prefix=path)
return response.get('Contents') is not None
# Helper function which lists keys in a path. Should not be used by the tests directly.
def _list_keys(self, path):
if not self.exists(path):
return False
response = self.s3client.list_objects(Bucket=self.bucketname, Prefix=path)
contents = response.get('Contents')
return [c['Key'] for c in contents]
def delete_file_dir(self, path, recursive=False):
if not self.exists(path):
return True
objects = [{'Key': k} for k in self._list_keys(path)] if recursive else path
self.s3client.delete_objects(Bucket=self.bucketname, Delete={'Objects':objects})
return True