Skip to content

Commit

Permalink
Merge pull request #53 from snuyanzin/datafaker140
Browse files Browse the repository at this point in the history
Update to datafaker 1.4.0. Add support for TIME
  • Loading branch information
knaufk authored May 25, 2022
2 parents e3431c5 + 75958a6 commit db09af1
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 115 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Currently, the `faker` source supports the following data types:
* `BOOLEAN`
* `TIMESTAMP`
* `DATE`
* `TIME`
* `ARRAY`
* `MAP`
* `MULTISET`
Expand All @@ -124,14 +125,15 @@ Currently, the `faker` source supports the following data types:

### On Timestamps

For rows of type `TIMESTAMP`, `DATE` the corresponding Java Faker expression needs to return a timestamp formatted as `uuuu-MM-dd hh:mi:ss[.nnnnnnnnn]`.
For rows of type `TIMESTAMP`, `DATE` the corresponding Data Faker expression needs to return a timestamp formatted as `uuuu-MM-dd hh:mi:ss[.nnnnnnnnn]`.
Typically, you would use one of the following expressions:

```sql
CREATE TEMPORARY TABLE timestamp_and_date_example (
CREATE TEMPORARY TABLE timestamp_time_and_date_example (
`timestamp1` TIMESTAMP(3),
`timestamp2` TIMESTAMP(3),
`timestamp3` TIMESTAMP(3),
`time` TIME,
`date1` DATE,
`date2` DATE
)
Expand All @@ -140,18 +142,20 @@ WITH (
'fields.timestamp1.expression' = '#{date.past ''15'',''SECONDS''}',
'fields.timestamp2.expression' = '#{date.past ''15'',''5'',''SECONDS''}',
'fields.timestamp3.expression' = '#{date.future ''15'',''5'',''SECONDS''}',
'fields.time.expression' = '#{time.future ''15'',''5'',''SECONDS''}',
'fields.date1.expression' = '#{date.birthday}',
'fields.date2.expression' = '#{date.birthday ''1'',''100''}'
);

SELECT * FROM timestamp_and_date_example;
SELECT * FROM timestamp_time_and_date_example;
```

For `timestamp1` Java Faker will generate a random timestamp that lies at most 15 seconds in the past.
For `timestamp2` Java Faker will generate a random timestamp, that lies at most 15 seconds in the past, but at least 5 seconds.
For `timestamp3` Java Faker will generate a random timestamp, that lies at most 15 seconds in the future, but at least 5 seconds.
For `date1` Java Faker will generate a random birthday between 18 and 65 years ago.
For `date2` Java Faker will generate a random birthday between 1 and 100 years ago.
For `timestamp1` Data Faker will generate a random timestamp that lies at most 15 seconds in the past.
For `timestamp2` Data Faker will generate a random timestamp, that lies at most 15 seconds in the past, but at least 5 seconds.
For `timestamp3` Data Faker will generate a random timestamp, that lies at most 15 seconds in the future, but at least 5 seconds.
For `time` Data Faker will generate a random time, that lies at most 15 seconds in the future, but at least 5 seconds.
For `date1` Data Faker will generate a random birthday between 18 and 65 years ago.
For `date2` Data Faker will generate a random birthday between 1 and 100 years ago.

### On Collection Data Types

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>net.datafaker</groupId>
<artifactId>datafaker</artifactId>
<version>1.3.0</version>
<version>1.4.0</version>
</dependency>

<!-- test dependencies -->
Expand Down
57 changes: 0 additions & 57 deletions src/main/java/com/github/knaufk/flink/faker/DateTime.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static Object stringValueToType(String[] stringArray, LogicalType logicalType) {
.getTime()
/ (86400 * 1000));
case TIME_WITHOUT_TIME_ZONE:
return (int) (Long.parseLong(value) / 1000_000L);
case TIMESTAMP_WITHOUT_TIME_ZONE:
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
return TimestampData.fromInstant(
Expand Down
45 changes: 0 additions & 45 deletions src/main/java/com/github/knaufk/flink/faker/FlinkFaker.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public FlinkFakerGenerator(
@Override
public void open(final Configuration parameters) throws Exception {
super.open(parameters);
faker = new FlinkFaker();
faker = new Faker();
rand = new Random();

nextReadTime = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import net.datafaker.Faker;
import org.apache.flink.table.data.GenericRowData;
import org.apache.flink.table.data.RowData;
import org.apache.flink.table.functions.FunctionContext;
Expand All @@ -17,7 +18,7 @@ public class FlinkFakerLookupFunction extends TableFunction<RowData> {
private LogicalType[] types;
private int[][] keys;
private List<Integer> keyIndeces;
private FlinkFaker faker;
private Faker faker;
private Random rand;

public FlinkFakerLookupFunction(
Expand All @@ -43,7 +44,7 @@ public FlinkFakerLookupFunction(
@Override
public void open(FunctionContext context) throws Exception {
super.open(context);
faker = new FlinkFaker();
faker = new Faker();
rand = new Random();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class FlinkFakerTableSourceFactory implements DynamicTableSourceFactory {
LogicalTypeRoot.ROW,
LogicalTypeRoot.MULTISET,
LogicalTypeRoot.DATE,
LogicalTypeRoot.TIME_WITHOUT_TIME_ZONE,
LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE,
LogicalTypeRoot.TIMESTAMP_WITH_LOCAL_TIME_ZONE,
LogicalTypeRoot.TIMESTAMP_WITH_TIME_ZONE);
Expand Down Expand Up @@ -181,7 +182,7 @@ private String[] readAndValidateFieldExpression(
}

try {
Faker faker = new FlinkFaker();
Faker faker = new Faker();
for (String expression : fieldExpression) faker.expression(expression);
} catch (RuntimeException e) {
throw new ValidationException("Invalid expression for column \"" + fieldName + "\".", e);
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/github/knaufk/flink/faker/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class TestUtils {
new BooleanType(),
new TimestampType(),
new DateType(),
new TimeType(),
new ArrayType(new IntType()),
new MapType(new IntType(), new VarCharType()),
new RowType(
Expand All @@ -43,6 +44,7 @@ public class TestUtils {
{"#{regexify '(true|false){1}'}"},
{"#{date.past '15','5','SECONDS'}"},
{"#{date.future '2','1','DAYS'}"},
{"#{time.future '2','1','SECONDS'}"},
{"#{number.numberBetween '-128','127'}"},
{"#{number.numberBetween '-128','127'}", "#{Lorem.characters '10'}"},
{"#{number.numberBetween '-128','127'}", "#{Lorem.characters '10'}"},
Expand Down

0 comments on commit db09af1

Please sign in to comment.