2015-07-31 15:09:48 -04:00
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
# The ASF licenses this file to You 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.
|
|
|
|
#
|
|
|
|
|
2019-01-17 20:40:39 -05:00
|
|
|
from pyspark.serializers import NoOpSerializer
|
2015-07-31 15:09:48 -04:00
|
|
|
from pyspark.storagelevel import StorageLevel
|
|
|
|
from pyspark.streaming import DStream
|
2019-03-10 21:15:07 -04:00
|
|
|
from pyspark.util import _print_missing_jar
|
|
|
|
|
2015-07-31 15:09:48 -04:00
|
|
|
|
|
|
|
__all__ = ['KinesisUtils', 'InitialPositionInStream', 'utf8_decoder']
|
|
|
|
|
|
|
|
|
|
|
|
def utf8_decoder(s):
|
|
|
|
""" Decode the unicode as UTF-8 """
|
2015-08-19 21:36:01 -04:00
|
|
|
if s is None:
|
|
|
|
return None
|
|
|
|
return s.decode('utf-8')
|
2015-07-31 15:09:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
class KinesisUtils(object):
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def createStream(ssc, kinesisAppName, streamName, endpointUrl, regionName,
|
|
|
|
initialPositionInStream, checkpointInterval,
|
|
|
|
storageLevel=StorageLevel.MEMORY_AND_DISK_2,
|
2017-02-22 11:32:36 -05:00
|
|
|
awsAccessKeyId=None, awsSecretKey=None, decoder=utf8_decoder,
|
|
|
|
stsAssumeRoleArn=None, stsSessionName=None, stsExternalId=None):
|
2015-07-31 15:09:48 -04:00
|
|
|
"""
|
|
|
|
Create an input stream that pulls messages from a Kinesis stream. This uses the
|
|
|
|
Kinesis Client Library (KCL) to pull messages from Kinesis.
|
|
|
|
|
2020-11-15 20:44:57 -05:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
ssc : :class:`StreamingContext`
|
|
|
|
StreamingContext object
|
|
|
|
kinesisAppName : str
|
|
|
|
Kinesis application name used by the Kinesis Client Library (KCL) to
|
|
|
|
update DynamoDB
|
|
|
|
streamName : str
|
|
|
|
Kinesis stream name
|
|
|
|
endpointUrl : str
|
|
|
|
Url of Kinesis service (e.g., https://kinesis.us-east-1.amazonaws.com)
|
|
|
|
regionName : str
|
|
|
|
Name of region used by the Kinesis Client Library (KCL) to update
|
|
|
|
DynamoDB (lease coordination and checkpointing) and CloudWatch (metrics)
|
|
|
|
initialPositionInStream : int
|
|
|
|
In the absence of Kinesis checkpoint info, this is the
|
|
|
|
worker's initial starting position in the stream. The
|
|
|
|
values are either the beginning of the stream per Kinesis'
|
|
|
|
limit of 24 hours (InitialPositionInStream.TRIM_HORIZON) or
|
|
|
|
the tip of the stream (InitialPositionInStream.LATEST).
|
|
|
|
checkpointInterval : int
|
|
|
|
Checkpoint interval(in seconds) for Kinesis checkpointing. See the Kinesis
|
|
|
|
Spark Streaming documentation for more details on the different
|
|
|
|
types of checkpoints.
|
|
|
|
storageLevel : :class:`pyspark.StorageLevel`, optional
|
|
|
|
Storage level to use for storing the received objects (default is
|
|
|
|
StorageLevel.MEMORY_AND_DISK_2)
|
|
|
|
awsAccessKeyId : str, optional
|
|
|
|
AWS AccessKeyId (default is None. If None, will use
|
|
|
|
DefaultAWSCredentialsProviderChain)
|
|
|
|
awsSecretKey : str, optional
|
|
|
|
AWS SecretKey (default is None. If None, will use
|
|
|
|
DefaultAWSCredentialsProviderChain)
|
|
|
|
decoder : function, optional
|
|
|
|
A function used to decode value (default is utf8_decoder)
|
|
|
|
stsAssumeRoleArn : str, optional
|
|
|
|
ARN of IAM role to assume when using STS sessions to read from
|
|
|
|
the Kinesis stream (default is None).
|
|
|
|
stsSessionName : str, optional
|
|
|
|
Name to uniquely identify STS sessions used to read from Kinesis
|
|
|
|
stream, if STS is being used (default is None).
|
|
|
|
stsExternalId : str, optional
|
|
|
|
External ID that can be used to validate against the assumed IAM
|
|
|
|
role's trust policy, if STS is being used (default is None).
|
2015-07-31 15:09:48 -04:00
|
|
|
|
2020-11-15 20:44:57 -05:00
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
A DStream object
|
|
|
|
|
|
|
|
Notes
|
|
|
|
-----
|
|
|
|
The given AWS credentials will get saved in DStream checkpoints if checkpointing
|
|
|
|
is enabled. Make sure that your checkpoint directory is secure.
|
2015-07-31 15:09:48 -04:00
|
|
|
"""
|
|
|
|
jlevel = ssc._sc._getJavaStorageLevel(storageLevel)
|
|
|
|
jduration = ssc._jduration(checkpointInterval)
|
|
|
|
|
|
|
|
try:
|
2016-03-14 15:22:02 -04:00
|
|
|
helper = ssc._jvm.org.apache.spark.streaming.kinesis.KinesisUtilsPythonHelper()
|
|
|
|
except TypeError as e:
|
|
|
|
if str(e) == "'JavaPackage' object is not callable":
|
2019-03-10 21:15:07 -04:00
|
|
|
_print_missing_jar(
|
|
|
|
"Streaming's Kinesis",
|
|
|
|
"streaming-kinesis-asl",
|
|
|
|
"streaming-kinesis-asl-assembly",
|
|
|
|
ssc.sparkContext.version)
|
2016-03-11 14:18:51 -05:00
|
|
|
raise
|
2016-03-14 15:22:02 -04:00
|
|
|
jstream = helper.createStream(ssc._jssc, kinesisAppName, streamName, endpointUrl,
|
|
|
|
regionName, initialPositionInStream, jduration, jlevel,
|
2017-02-22 11:32:36 -05:00
|
|
|
awsAccessKeyId, awsSecretKey, stsAssumeRoleArn,
|
|
|
|
stsSessionName, stsExternalId)
|
2015-07-31 15:09:48 -04:00
|
|
|
stream = DStream(jstream, ssc, NoOpSerializer())
|
|
|
|
return stream.map(lambda v: decoder(v))
|
|
|
|
|
|
|
|
|
|
|
|
class InitialPositionInStream(object):
|
|
|
|
LATEST, TRIM_HORIZON = (0, 1)
|