I have a simple code as follows,when I run it within IDE, nothing is printed on the console, could some one help take a look? Thanks
import org.apache.flink.api.java.tuple.Tuple
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.streaming.api.scala.function.ProcessWindowFunction
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow
import org.apache.flink.util.Collector
object WindowTest {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime)
val ds = env.fromElements(
(1, "a"), (2, "b"), (3, "c"), (4, "e"), (5, "f"), (6, "g"), (7, "h"), (8, "g"), (1, "1a"), (2, "2b"), (3, "3c"), (4, "4e"), (5, "5f"), (6, "g"), (7, "h"), (8, "g")
)
val ds2 = ds.keyBy(0).timeWindow(Time.seconds(10))
.process(new ProcessWindowFunction[(Int, String), String, Tuple, TimeWindow] {
override def process(key: Tuple, context: Context, elements: Iterable[(Int, String)], out: Collector[String]): Unit = {
val k = key.getField[Int](0)
val w = context.window
val start = w.getStart
val end = w.getEnd
val hc = context.window.hashCode()
//NOT CALLED
println(s"k=$k,start=$start, end=$end,hc=$hc")
}
})
ds2.print()
env.execute()
Thread.sleep(30 * 1000)
}
}
Your ProcessWindowFunction is never called because the window is never triggered. It's never triggered because it runs to completion in a couple of milliseconds (roughly speaking), so it's very unlikely to be running at a moment when the system clock time is exactly at a 10 second boundary, which is what would have to happen for this processing time window to be triggered.
Related
I found a code that works well. I want to use it in a project but I can't. I think the problem is at the level of recovery and then their conversion according to the initial project
this is the array in the source code
loadPreviousEvents() {
mySelectedEvents = {
"2022-09-13": [
{"eventDescp": "11", "eventTitle": "111"},
{"eventDescp": "22", "eventTitle": "22"}
],
"2022-09-30": [
{"eventDescp": "22", "eventTitle": "22"}
],
"2022-09-20": [
{"eventTitle": "ss", "eventDescp": "ss"}
]
};
}
here is how I tried to do the same by retrieving the data in my database
void getData() async {
var url = 'http://xxxxxxxxx/getEvents.php';
var res = await http.get(Uri.parse(url));
var response = json.decode(res.body);
var mySelectedEvents =
groupBy(response, (Map obj) => obj['date']).map((k, v) => MapEntry(
k,
v.map((item) {
item.remove('date');
return item;
}).toList()));
print(mySelectedEvents);
}
List _listOfDayEvents(DateTime dateTime) {
if (mySelectedEvents[DateFormat('yyyy-MM-dd').format(dateTime)] != null) {
return mySelectedEvents[DateFormat('yyyy-MM-dd').format(dateTime)]!;
} else {
return [];
}
}
here are my 2 tables
INSERT INTO `event_date` (`id`, `date`, `selectdate`) VALUES
(4, '2022-09-17', '2022-09-17 22:13:04.508644'),
(5, '2022-09-17', '2022-09-17 23:19:05.885897'),
(6, '2022-09-17', '2022-09-17 23:19:05.885897'),
(7, '2022-09-20', '2022-09-20 00:00:00.000Z'),
(8, '2022-09-17', '2022-09-17 23:20:46.357121');
INSERT INTO `event_list` (`id`, `descript`, `title`, `id_event_date`) VALUES
(1, '11', '111', 1),
(2, '22', '22', 1),
(3, '22', '22', 2),
(4, 'ss', 'ss', 3);
this ma PHP code
$table = "event_date";
$table2 = "event_list";
$db_data = array();
$sql = "SELECT *
FROM $table D
,$table2 E
WHERE D.id = E.id_event_date
";
$result = $conn->query($sql);
if($result->num_rows >0){
while($rows = $result->fetch_assoc()){
$db_data[] = $rows;
}//fin while
//Retourn toutes les reponses en json
echo json_encode(utf8ize($db_data));
}else{
echo "erreur";
}
I am new to scalatest and a bit lost in the gigantic inconsistent mess of features
I am trying to write parameterized tests. It seems that the way to do that is via TableDrivenPropertyChecks
I managed to get it working with a test like:
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.prop.TableDrivenPropertyChecks
class MyTest extends AnyFunSpec with TableDrivenPropertyChecks {
describe("the test") {
val cases = Table(
("label", "a", "b"),
("case 2 > 1", 1, 2),
("case 4 > 3", 3, 4),
)
it("should...") {
forAll(cases) { (name: String, a: Int, b: Int) =>
assert(b > a)
}
}
}
}
It's fine, but when I run it under sbt I just see:
[info] MyTest:
[info] the test
[info] - should...
If I inject a bad case into the table I can see the test fail so I know they are all being tested.
What I want is for the test runner to report something for each case in the table.
I have seen examples which look like they would provide this, e.g.: https://blog.knoldus.com/table-driven-testing-in-scala/
import org.scalatest.FreeSpec
import org.scalatest._
import org.scalatest.prop.TableDrivenPropertyChecks
class OrderValidationTableDrivenSpec extends FreeSpec with TableDrivenPropertyChecks with Matchers {
"Order Validation" - {
"should validate and return false if" - {
val orders = Table(
("statement" , "order")
, ("price is negative" , Order(quantity = 10, price = -2))
, ("quantity is negative" , Order(quantity = -10, price = 2))
, ("price and quantity are negative" , Order(quantity = -10, price = -2))
)
forAll(orders) {(statement, invalidOrder) =>
s"$statement" in {
OrderValidation.validateOrder(invalidOrder) shouldBe false
}
}
}
}
}
But if I try to do this in my test:
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.prop.TableDrivenPropertyChecks
class MyTest extends AnyFunSpec with TableDrivenPropertyChecks {
describe("the test") {
val cases = Table(
("label", "a", "b"),
("case 2 > 1", 1, 2),
("case 3 > 4", 3, 4),
)
it("should...") {
forAll(cases) { (label: String, a: Int, b: Int) =>
s"$label" in {
assert(b > a)
}
}
}
}
}
...I get the error: value in is not a member of String from the s"$label" in { part.
How do I get my TableDrivenPropertyChecks to report something for each case in the table?
One of cool features of ScalaTest is that test is not a method, so you can define tests inside a loop:
Table(
("a", "b"),
(1, 2),
(3, 4)
).forEvery({ (a: Int, b: Int) => {
it(s"should pass if $b is greater then $a") {
assert(b > a)
}
}})
Or even without using Table, just with any collection:
Seq(
(1, 2),
(3, 4)
).foreach {
case (a: Int, b: Int) =>
it(s"should pass if $b is greater then $a") {
assert(b > a)
}
}
This way you will get one test per each iteration of a loop, so all the cases will be executed independently, while in case of a loop in a single test it will fail on first failed assertion and won't check remaining cases.
I came across a problem that required iterating over an array in pairs. What's the best way to do this? Or, as an alternative, what's the best way of transforming an Array into an Array of pairs (which could then be iterated normally)?
Here's the best I got. It requires output to be a var, and it's not really pretty. Is there a better way?
let input = [1, 2, 3, 4, 5, 6]
var output = [(Int, Int)]()
for i in stride(from: 0, to: input.count - 1, by: 2) {
output.append((input[i], input[i+1]))
}
print(output) // [(1, 2), (3, 4), (5, 6)]
// let desiredOutput = [(1, 2), (3, 4), (5, 6)]
// print(desiredOutput)
You can map the stride instead of iterating it, that
allows to get the result as a constant:
let input = [1, 2, 3, 4, 5, 6]
let output = stride(from: 0, to: input.count - 1, by: 2).map {
(input[$0], input[$0+1])
}
print(output) // [(1, 2), (3, 4), (5, 6)]
If you only need to iterate over the pairs and the given array is large
then it may be advantageous to avoid the creation of an intermediate
array with a lazy mapping:
for (left, right) in stride(from: 0, to: input.count - 1, by: 2)
.lazy
.map( { (input[$0], input[$0+1]) } ) {
print(left, right)
}
This is now available as
Sequence.chunks(ofCount: 2) of the swift-algorithms package
for chunk in input.chunks(ofCount: 2) {
print(chunk)
}
I don't think this is any better than Martin R's, but seems the OP needs something else...
struct PairIterator<C: IteratorProtocol>: IteratorProtocol {
private var baseIterator: C
init(_ iterator: C) {
baseIterator = iterator
}
mutating func next() -> (C.Element, C.Element)? {
if let left = baseIterator.next(), let right = baseIterator.next() {
return (left, right)
}
return nil
}
}
extension Sequence {
var pairs: AnySequence<(Self.Iterator.Element,Self.Iterator.Element)> {
return AnySequence({PairIterator(self.makeIterator())})
}
}
input.pairs.forEach{ print($0) }
let output = input.pairs.map{$0}
print(output) //->[(1, 2), (3, 4), (5, 6)]
Here's a version of #OOPer's answer that works with an odd number of elements in your list. You can leave off the conformance to CustomStringConvertible if you like, of course. But it gives prettier output for this example. : )
struct Pair<P: CustomStringConvertible>: CustomStringConvertible {
let left: P
let right: P?
var description: String {
if let right = right {
return "(\(left.description), \(right.description)"
}
return "(\(left.description), nil)"
}
}
struct PairIterator<C: IteratorProtocol>: IteratorProtocol where C.Element: CustomStringConvertible {
private var baseIterator: C
init(_ iterator: C) {
baseIterator = iterator
}
mutating func next() -> Pair<C.Element>? {
if let left = baseIterator.next() {
return Pair(left: left, right: baseIterator.next())
}
return nil
}
}
extension Sequence where Element: CustomStringConvertible {
var pairs: AnySequence<Pair<Self.Element>> {
return AnySequence({PairIterator(self.makeIterator())})
}
}
let input: [Int] = [1,2,3,4,5,6,7]
print(input.pairs)
print(Array(input.pairs))
//output:
AnySequence<Pair<Int>>(_box: Swift._SequenceBox<Swift._ClosureBasedSequence<__lldb_expr_27.PairIterator<Swift.IndexingIterator<Swift.Array<Swift.Int>>>>>)
[(1, 2, (3, 4, (5, 6, (7, nil)]
You don't need a custom type, like PairIterator as the above answers prescribe. Getting a paired sequence is a one-liner:
let xs = [1, 2, 3]
for pair in zip(xs, xs.dropFirst()) {
print(pair) // (1, 2) (2, 3)
}
If you intend to reuse that, you can place a pairs method inside an extension:
extension Sequence {
func pairs() -> AnySequence<(Element, Element)> {
AnySequence(zip(self, self.dropFirst()))
}
}
I came across a problem that required iterating over an array in pairs. What's the best way to do this? Or, as an alternative, what's the best way of transforming an Array into an Array of pairs (which could then be iterated normally)?
Here's the best I got. It requires output to be a var, and it's not really pretty. Is there a better way?
let input = [1, 2, 3, 4, 5, 6]
var output = [(Int, Int)]()
for i in stride(from: 0, to: input.count - 1, by: 2) {
output.append((input[i], input[i+1]))
}
print(output) // [(1, 2), (3, 4), (5, 6)]
// let desiredOutput = [(1, 2), (3, 4), (5, 6)]
// print(desiredOutput)
You can map the stride instead of iterating it, that
allows to get the result as a constant:
let input = [1, 2, 3, 4, 5, 6]
let output = stride(from: 0, to: input.count - 1, by: 2).map {
(input[$0], input[$0+1])
}
print(output) // [(1, 2), (3, 4), (5, 6)]
If you only need to iterate over the pairs and the given array is large
then it may be advantageous to avoid the creation of an intermediate
array with a lazy mapping:
for (left, right) in stride(from: 0, to: input.count - 1, by: 2)
.lazy
.map( { (input[$0], input[$0+1]) } ) {
print(left, right)
}
This is now available as
Sequence.chunks(ofCount: 2) of the swift-algorithms package
for chunk in input.chunks(ofCount: 2) {
print(chunk)
}
I don't think this is any better than Martin R's, but seems the OP needs something else...
struct PairIterator<C: IteratorProtocol>: IteratorProtocol {
private var baseIterator: C
init(_ iterator: C) {
baseIterator = iterator
}
mutating func next() -> (C.Element, C.Element)? {
if let left = baseIterator.next(), let right = baseIterator.next() {
return (left, right)
}
return nil
}
}
extension Sequence {
var pairs: AnySequence<(Self.Iterator.Element,Self.Iterator.Element)> {
return AnySequence({PairIterator(self.makeIterator())})
}
}
input.pairs.forEach{ print($0) }
let output = input.pairs.map{$0}
print(output) //->[(1, 2), (3, 4), (5, 6)]
Here's a version of #OOPer's answer that works with an odd number of elements in your list. You can leave off the conformance to CustomStringConvertible if you like, of course. But it gives prettier output for this example. : )
struct Pair<P: CustomStringConvertible>: CustomStringConvertible {
let left: P
let right: P?
var description: String {
if let right = right {
return "(\(left.description), \(right.description)"
}
return "(\(left.description), nil)"
}
}
struct PairIterator<C: IteratorProtocol>: IteratorProtocol where C.Element: CustomStringConvertible {
private var baseIterator: C
init(_ iterator: C) {
baseIterator = iterator
}
mutating func next() -> Pair<C.Element>? {
if let left = baseIterator.next() {
return Pair(left: left, right: baseIterator.next())
}
return nil
}
}
extension Sequence where Element: CustomStringConvertible {
var pairs: AnySequence<Pair<Self.Element>> {
return AnySequence({PairIterator(self.makeIterator())})
}
}
let input: [Int] = [1,2,3,4,5,6,7]
print(input.pairs)
print(Array(input.pairs))
//output:
AnySequence<Pair<Int>>(_box: Swift._SequenceBox<Swift._ClosureBasedSequence<__lldb_expr_27.PairIterator<Swift.IndexingIterator<Swift.Array<Swift.Int>>>>>)
[(1, 2, (3, 4, (5, 6, (7, nil)]
You don't need a custom type, like PairIterator as the above answers prescribe. Getting a paired sequence is a one-liner:
let xs = [1, 2, 3]
for pair in zip(xs, xs.dropFirst()) {
print(pair) // (1, 2) (2, 3)
}
If you intend to reuse that, you can place a pairs method inside an extension:
extension Sequence {
func pairs() -> AnySequence<(Element, Element)> {
AnySequence(zip(self, self.dropFirst()))
}
}
I tried to copy it to array, but I can't accept two (Sets) parameters
def subset(a:Set[Int],b:Set[Int]):Boolean={
var x = new Array[Int](a.size)
var y = new Array[Int](b.size)
}
or can you explain how to recieve two arrays as parameter?
def main(args: Array[String]): Unit = {
val a = Set(2,3,4,5,6)
val b = Set(2,3,4,5)
var x = new Array[Int](a.size)
var y = new Array[Int](b.size)
i wish to put x and y to function subset and do same but not by Set
You can transform Set to Array or Array to Set:
scala> val array = Array(1, 3, 5)
array: Array[Int] = Array(1, 3, 5)
scala> array.toSet
res0: scala.collection.immutable.Set[Int] = Set(1, 3, 5)
scala> val set = Set(7, 3, 8)
set: scala.collection.immutable.Set[Int] = Set(7, 3, 8)
scala> set.toArray
res1: Array[Int] = Array(7, 3, 8)
If you want a program to take a collection of Ints as a command-line argument, you'll have to parse a string:
object Test extends App {
val x: Array[Int] = args(0).split(",").map{ _.toInt }
val y: Array[Int] = args(1).split(",").map{ _.toInt }
println("x: " + x.mkString("Array(", ", ", ")"))
println("y: " + y.mkString("Array(", ", ", ")"))
}
Usage:
>scala Test "1,2,3" "10,0,-5"
x: Array(1, 2, 3)
y: Array(10, 0, -5)