aws glue maches dynamic record - amazon-web-services

Could anyone explain code below for matching record in Aws glue
from manual https://aws.amazon.com/ru/blogs/big-data/work-with-partitioned-data-in-aws-glue/
I printed the full schema using githubEvents.printSchema():
id: string
type: string
actor: struct
repo: struct
payload: struct
public: boolean
created_at: string
year: string
month: string
day: string
org: struct
def filterWeekend(rec: DynamicRecord): Boolean = {
def getAsInt(field: String): Int = {
rec.**getField(field**) match {
case Some(strVal: String) => strVal.toInt
// The filter transformation will catch exceptions and mark the record as an error.
case _ => throw new IllegalArgumentException(s"Unable to extract field $field")
}
}
val (year, month, day) = (getAsInt("year"), getAsInt("month"), getAsInt("day"))
val cal = new GregorianCalendar(year, month - 1, day) // Calendar months start at 0.
val dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)
**dayOfWeek** == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY
}
val filteredEvents = githubEvents.filter(filterWeekend)
filteredEvents.count
I think that day should be ingested in getField(field) how glue knows that day should be compared but not other field so I mean we should compare day of the schema with dayofWeek?

Related

loopback4 Regular Expressions- Match Anything

I use loopback4. How do I make an expression to match datetime with time zone.I am interested by the hour parameter only: "finalhour"
example: 2019-12-20T10:22:50.143Z ==> 2019-12-20Tfinalhour:22:50.143Z
I tried with this: const pattern=await '^'+"T"+finalhour+'^'
but loopback usually read it as ^T10^
I'm resort to you after a long search in the forums.I will be thankful if you help me
From what i understand, you want to build a regex that match 2019-12-20TsomeNumber:22:50.143Z.
You could use this default ISO datetime regex
(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})\.\d{3,4}Z
And modify it to take your finalhour variable in it.
let finalHour = 10;
// when building regex from constructor, we need to espace the backslash ( \ )
// i did not know that.
let regex = new RegExp('(\\d{4})-(\\d{2})-(\\d{2})T' + finalHour + '\\:(\\d{2})\\:(\\d{2})\\.\\d{3,4}\\Z');
let test = '2019-12-20T10:22:50.143Z';
console.log(regex.test(test));
Also, you don't need an await keyword here because your are building a string, not waiting for a promise.
I need to return the number of order per hour. so i try this solution but it return false comparison, for example if i have one order at hour:10 it return 6
#get('/orders/count-perHour/{date}', {
responses: {
'200': {
description: 'Order model count',
content: { 'application/json': { schema: CountSchema } },
},
},
})
async countPerHour(
#param.path.string('date') date: string): Promise<any> {
let dateStart = new Date(date);
dateStart.setSeconds(0);
dateStart.setMinutes(0);
let dateEnd = new Date(date);
dateEnd.setSeconds(59);
dateEnd.setMinutes(59);
return await this.orderRepository.count(
{
createdAt: {
lte: dateEnd
,
gte: dateStart
}
}
);
}

how to convert Date(2019/5/10) to shamsiDate(1398/4/12)

I had called a web service in my code that contains Deadline Date . the problem is the response in swagger shows like this :
{
"id": "289",
"serviceName": "خدمت 1",
"deadLine": "2024/05/06",
...
...
}
and I want to show deadline in Shamsi Date like 1398/4/12.
i have used a lot of convertors but it did not worked for me and still shows the "deadLine": "2024/05/06",
this is my view :
public String setDeadLine(Date deadLine) {
this.deadLine = deadLine;
String dateFa;
//String dateFa= DateConvertor.miladi2date(deadLine);
dateFa= this.ConvertToPersianToShow(deadLine);
return dateFa;
}
public String ConvertToPersianToShow(Date deadLine)
{
CalendarTool cl = new CalendarTool();
String date;
String year = cl.getIranianYearInCentury();
String Month = cl.getIranianMonthStr();
String day = cl.getIranianWeekDayStr();
date = year+"/" + Month + "/" + day;
return date;
}
should I type any code in my controller ?
RESOLVE :
I just need to add some code in ConvertToPersianToShow :
calendarTool should be filled .mine was empty
I added this code :
String[] date = deadLine.split("/");
if(date.length != 3)
return null;
Calendar calendar = GregorianCalendar.getInstance();
calendar.set(Integer.parseInt(date[0]),Integer.parseInt(date[1]),Integer.parseInt(date[2]));
and filled the calendar :
CalendarTool cl = new CalendarTool(calendar);

Golang DynamoDB UnmarshalListOfMaps Returns Array of Empty

I have a DynamoDB table of products (id (int), active (bool), name (string), price (int)) and when I retrieve and attempt to unmarshal the list, it returns empty.
[{},{}]
Struct:
type Product struct {
id int
active bool
name string
price int }
And the code to unmarshal is here:
params := &dynamodb.ScanInput{
TableName: aws.String("Products"),
}
result, err := service.Scan(params)
if err != nil {
fmt.Errorf("failed to make Query API call, %v", err)
}
var products = []Product{}
var error = dynamodbattribute.UnmarshalListOfMaps(result.Items, &products)
What am I doing wrong here?
Only public fields can be unmarshaled.
Make your struct fields public using uppercase letter and use json attribute to map them to data values:
type Product struct {
ID int `json:"id"`
Active bool `json:"active"`
Name string `json:"name"`
Price int `json:"price"`
}
Update Oct 2021:
AWS SDK v1 uses json attribute for DynamoDB serialization.
new version aws-sdk-go-v2 contains breaking change and moved from json to dynamodbav attribute to separate JSON and DynamoDB names.
For V2 struct should look like this:
type Product struct {
ID int `dynamodbav:"id"`
Active bool `dynamodbav:"active"`
Name string `dynamodbav:"name"`
Price int `dynamodbav:"price"`
}
Docs: https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/dynamodbattribute/#Marshal

Swift: Finding an Object Property via regex

Target: The following function shall iterate over an array of objects and check a specific property of all objects. This property is a string and shall be matched with a user input via regex. If there's a match the object shall be added to an array which will further be passed to another function.
Problem: I don't know how to set up regex in Swift 3. I'm rather new in Swift at all, so an easily understandable solution would be very helpful :)
How it currently looks like:
func searchItems() -> [Item] {
var matches: [Item] = []
if let input = readLine() {
for item in Storage.storage.items { //items is a list of objects
if let query = //regex with query and item.name goes here {
matches.append(item)
}
}
return matches
} else {
print("Please type in what you're looking for.")
return searchItems()
}
}
This is what Item looks like (snippet):
class Item: CustomStringConvertible {
var name: String = ""
var amount: Int = 0
var price: Float = 0.00
var tags: [String] = []
var description: String {
if self.amount > 0 {
return "\(self.name) (\(self.amount) pcs. in storage) - \(price) €"
} else {
return "\(self.name) (SOLD OUT!!!) - \(price) €"
}
}
init(name: String, price: Float, amount: Int = 0) {
self.name = name
self.price = price
self.amount = amount
}
}
extension Item: Equatable {
static func ==(lhs: Item, rhs: Item) -> Bool {
return lhs.name == rhs.name
}
}
Solved. I just edited this post to get a badge :D
For the purpose of letting the answer to be generic and clear, I will assume that the Item model is:
struct Item {
var email = ""
}
Consider that the output should be a filtered array of items that contains items with only valid email.
For such a functionality, you should use NSRegularExpression:
The NSRegularExpression class is used to represent and apply regular
expressions to Unicode strings. An instance of this class is an
immutable representation of a compiled regular expression pattern and
various option flags.
According to the following function:
func isMatches(_ regex: String, _ string: String) -> Bool {
do {
let regex = try NSRegularExpression(pattern: regex)
let matches = regex.matches(in: string, range: NSRange(location: 0, length: string.characters.count))
return matches.count != 0
} catch {
print("Something went wrong! Error: \(error.localizedDescription)")
}
return false
}
You can decide if the given string does matches the given regex.
Back to the example, consider that you have the following array of Item Model:
let items = [Item(email: "invalid email"),
Item(email: "email#email.com"),
Item(email: "Hello!"),
Item(email: "example#example.net")]
You can get the filtered array by using filter(_:) method:
Returns an array containing, in order, the elements of the sequence
that satisfy the given predicate.
as follows:
let emailRegex = "[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailItems = items.filter {
isMatches(emailRegex, $0.email)
}
print(emailItems) // [Item(email: "email#email.com"), Item(email: "example#example.net")]
Hope this helped.
You can do the same with filter function
let matches = Storage.storage.items.filter({ $0.yourStringPropertyHere == input })

How to disable a date range in Jquery datepicker

I want to disable a range of dates which I are fetched using Ajax. I'm doing it as follows -
$("#date_frm").datepicker({
dateFormat: 'yy-mm-dd',
constrainInput: true,
beforeShow:function(input, inst) {
$.ajax({
type: "POST",
url: "/admin/get_time_span",
data: "",
success: function(data) {
disabled_day = data;
},
});
},
beforeShowDay: disableRangeOfDays
});
function disableRangeOfDays(d)
{
//var arr = "2012-04-19 to 2012-04-26,";
var arr = disabled_day.split(",");
var arr = arr.split(",");
var cnt = arr.length-1;
for(i=0; i<cnt; i++) {
arr1 = arr[i].split(" to ");
//create date for from_date
frm_dt = arr1[0].split('-');
//create date for to_date
to_dt = arr1[1].split('-');
if(d >= new Date(frm_dt[0],(frm_dt[1]-1),frm_dt[2]) &&
d <= new Date(to_dt[0],(to_dt[1]-1),to_dt[2])) {
return [false];
}
}
return [true,''];
}
This works but not for the first time. When I open the date picker first time, the date range still selectable. But, after I close and reopen it, the date range is disabled. Also, if I change the month and come back to the current month then also it works. How can I disable the date range for the first time I open the date picker ? Also, for each month, I want to fetch the date ranges and disable them. How can I do this ?
After spending much time in checking possibilities, I fetched all the date ranges only once while loading the page and assigned all to a hidden field. I removed the Ajax call and used the value of the hidden field directly in the function disableRangeOfDays(d) and it worked as expected.