Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
flink-service
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
SuperHive
back-end
flink-service
Commits
6aca7e80
Commit
6aca7e80
authored
Aug 14, 2020
by
chenfm
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
连接MQTT增加账号信息
parent
f5d2f496
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
17 deletions
+33
-17
EmqClient.java
src/main/java/com/esv/flink/EmqClient.java
+17
-9
StreamingJob.java
src/main/java/com/esv/flink/StreamingJob.java
+6
-6
application-dev.properties
src/main/resources/application-dev.properties
+5
-1
application-test.properties
src/main/resources/application-test.properties
+5
-1
No files found.
src/main/java/com/esv/flink/EmqClient.java
View file @
6aca7e80
...
...
@@ -6,6 +6,8 @@ import org.fusesource.mqtt.client.MQTT;
import
org.fusesource.mqtt.client.QoS
;
import
org.fusesource.mqtt.client.Topic
;
import
java.util.Properties
;
/**
* @description:
* @project: emqdemo
...
...
@@ -19,11 +21,17 @@ import org.fusesource.mqtt.client.Topic;
public
class
EmqClient
{
private
String
broker
;
private
String
subTopic
=
"$esv/iot/#"
;
private
String
clientId
=
"subscribe_emqx_flink"
;
private
String
subTopic
;
private
String
clientId
;
private
String
username
;
private
String
password
;
public
EmqClient
(
String
broker
)
{
this
.
broker
=
broker
;
public
EmqClient
(
Properties
properties
)
{
this
.
broker
=
properties
.
getProperty
(
"mqtt.host"
);
this
.
subTopic
=
properties
.
getProperty
(
"mqtt.subscribe.topic"
);
this
.
clientId
=
properties
.
getProperty
(
"mqtt.client.id"
);
this
.
username
=
properties
.
getProperty
(
"mqtt.username"
);
this
.
password
=
properties
.
getProperty
(
"mqtt.password"
);
}
public
FutureConnection
run
()
{
...
...
@@ -36,6 +44,8 @@ public class EmqClient {
mqtt
.
setKeepAlive
((
short
)
30
);
mqtt
.
setSendBufferSize
(
64
);
mqtt
.
setClientId
(
clientId
);
mqtt
.
setUserName
(
username
);
mqtt
.
setPassword
(
password
);
Topic
[]
topics
=
{
new
Topic
(
subTopic
,
QoS
.
AT_MOST_ONCE
)
...
...
@@ -44,13 +54,11 @@ public class EmqClient {
FutureConnection
connection
=
mqtt
.
futureConnection
();
connection
.
connect
();
connection
.
subscribe
(
topics
);
log
.
info
(
"mqtt连接成功"
);
log
.
info
(
"mqtt连接成功: {}"
,
broker
);
return
connection
;
}
catch
(
Exception
me
)
{
me
.
printStackTrace
();
}
catch
(
Exception
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
return
null
;
}
...
...
src/main/java/com/esv/flink/StreamingJob.java
View file @
6aca7e80
...
...
@@ -77,7 +77,7 @@ public class StreamingJob {
String
brokerUrl
=
properties
.
getProperty
(
"broker.url"
,
"tcp://192.168.0.122:1883"
);
env
.
setParallelism
(
1
);
EmqSource
emqSource
=
new
EmqSource
(
brokerUrl
);
EmqSource
emqSource
=
new
EmqSource
(
properties
);
DataStream
<
String
>
inStream
=
env
.
addSource
(
emqSource
);
inStream
.
print
();
...
...
@@ -113,20 +113,20 @@ public class StreamingJob {
// res.writeAsText("D://flink_result.txt");
// execute program
env
.
execute
(
"Streaming
WordCount
"
);
env
.
execute
(
"Streaming
DataCenter IOT.
"
);
}
public
static
class
EmqSource
implements
ParallelSourceFunction
<
String
>
{
private
static
final
long
serialVersionUID
=
1L
;
private
volatile
boolean
isRunning
=
true
;
private
String
brokerUrl
;
private
Properties
properties
;
public
EmqSource
(
String
brokerUrl
)
{
this
.
brokerUrl
=
brokerUrl
;
public
EmqSource
(
Properties
properties
)
{
this
.
properties
=
properties
;
}
public
void
run
(
SourceContext
<
String
>
ctx
)
throws
Exception
{
EmqClient
emqClient
=
new
EmqClient
(
brokerUrl
);
EmqClient
emqClient
=
new
EmqClient
(
properties
);
FutureConnection
connection
=
emqClient
.
run
();
int
num
=
0
;
...
...
src/main/resources/application-dev.properties
View file @
6aca7e80
broker.url
=
tcp://192.168.31.248:1883
mqtt.host
=
tcp://192.168.31.248:1883
mqtt.username
=
esv_mqtt_server
mqtt.password
=
123456
mqtt.subscribe.topic
=
$esv/iot/#
mqtt.client.id
=
subscribe_emqx_flink
postgres.url
=
jdbc:postgresql://192.168.31.248:5432/iot
postgres.user
=
iot
postgres.pwd
=
123456
...
...
src/main/resources/application-test.properties
View file @
6aca7e80
broker.url
=
tcp://192.168.0.122:1883
mqtt.host
=
tcp://192.168.0.122:1883
mqtt.username
=
esv_mqtt_server
mqtt.password
=
123456
mqtt.subscribe.topic
=
$esv/iot/#
mqtt.client.id
=
subscribe_emqx_flink
postgres.url
=
jdbc:postgresql://192.168.0.17:54321/iot
postgres.user
=
iot
postgres.pwd
=
123456
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment