-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from beehyv/ndjson_format_for_exporters
Ndjson format for S3 and Blob exporters
- Loading branch information
Showing
6 changed files
with
138 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import json | ||
import pytest | ||
from azure.storage.blob import BlobServiceClient | ||
from azure.core.exceptions import ResourceNotFoundError | ||
|
||
class TestAzureBlobNDJSONFormat: | ||
|
||
@pytest.fixture | ||
def blob_service_client(self): | ||
connection_string = "" | ||
client = BlobServiceClient.from_connection_string(connection_string) | ||
return client | ||
|
||
@pytest.fixture | ||
def blob_info(self): | ||
return { | ||
'container_name': 'sachin', | ||
'blob_name': 'xx.ndjson' | ||
} | ||
|
||
def test_blob_ndjson_format(self, blob_service_client, blob_info): | ||
try: | ||
blob_client = blob_service_client.get_blob_client(container=blob_info['container_name'], blob=blob_info['blob_name']) | ||
blob_data = blob_client.download_blob().readall().decode('utf-8') | ||
lines = blob_data.strip().split("\n") | ||
for line in lines: | ||
try: | ||
json_obj = json.loads(line) | ||
assert isinstance(json_obj, dict), f"Line is not a valid JSON object: {line}" | ||
except json.JSONDecodeError: | ||
raise AssertionError(f"Line is not valid JSON: {line}") | ||
|
||
except ResourceNotFoundError: | ||
raise AssertionError(f"Blob {blob_info['blob_name']} not found in container {blob_info['container_name']}") | ||
|
||
if __name__ == '__main__': | ||
pytest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import json | ||
import boto3 | ||
from botocore.exceptions import NoCredentialsError | ||
import pytest | ||
|
||
|
||
class TestS3NDJSONFormat: | ||
|
||
@pytest.fixture | ||
def s3_client(self): | ||
client = boto3.client( | ||
's3', | ||
aws_access_key_id='', | ||
aws_secret_access_key='', | ||
region_name='us-east-1' | ||
) | ||
return client | ||
|
||
@pytest.fixture | ||
def bucket_info(self): | ||
return { | ||
'bucket_name': 'sachin-dev', | ||
's3_file_key': 'xx.ndjson' | ||
} | ||
|
||
def test_s3_ndjson_format(self, s3_client, bucket_info): | ||
try: | ||
response = s3_client.get_object(Bucket=bucket_info['bucket_name'], Key=bucket_info['s3_file_key']) | ||
file_content = response['Body'].read().decode('utf-8') | ||
|
||
lines = file_content.strip().split("\n") | ||
for line in lines: | ||
try: | ||
json_obj = json.loads(line) | ||
assert isinstance(json_obj, dict), f"Line is not a valid JSON object: {line}" | ||
except json.JSONDecodeError: | ||
raise AssertionError(f"Line is not valid JSON: {line}") | ||
|
||
except NoCredentialsError: | ||
raise AssertionError("AWS credentials not available") | ||
except Exception as e: | ||
raise AssertionError(f"Test failed with error: {e}") | ||
|
||
|
||
if __name__ == '__main__': | ||
pytest.main() |