更新时间:2022-04-24 GMT+08:00
java样例代码
前提条件
在DLI管理控制台上已完成创建跨源连接。具体操作请参考《数据湖探索用户指南》。
CSS非安全集群
- 开发说明
- 代码实现
- 构造依赖信息,创建SparkSession
- 通过SQL API访问
- 创建DLI跨源访问 CSS关联表。
sparkSession.sql("create table css_table(id long, name string) using css options( 'es.nodes' = '192.168.9.213:9200', 'es.nodes.wan.only' = 'true','resource' ='/mytest')");
- 插入数据。
sparkSession.sql("insert into css_table values(18, 'John'),(28, 'Bob')");
- 查询数据。
sparkSession.sql("select * from css_table").show();
- 删除数据表。
sparkSession.sql("drop table css_table");
- 创建DLI跨源访问 CSS关联表。
- 提交Spark作业
- 代码实现
- 完整示例代码
- Maven依赖
<dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.11</artifactId> <version>2.3.2</version> </dependency>
- 通过SQL API 访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import org.apache.spark.sql.*; public class java_css_unsecurity { public static void main(String[] args) { SparkSession sparkSession = SparkSession.builder().appName("datasource-css-unsecurity").getOrCreate(); // Create a DLI data table for DLI-associated CSS sparkSession.sql("create table css_table(id long, name string) using css options( 'es.nodes' = '192.168.15.34:9200', 'es.nodes.wan.only' = 'true', 'resource' = '/mytest')"); //*****************************SQL model*********************************** // Insert data into the DLI data table sparkSession.sql("insert into css_table values(18, 'John'),(28, 'Bob')"); // Read data from DLI data table sparkSession.sql("select * from css_table").show(); // drop table sparkSession.sql("drop table css_table"); sparkSession.close(); } }
- Maven依赖
CSS安全集群
- 准备工作
请参考CSS安全集群配置,准备工作的主要目的是为了生成keystore.jks文件和truststore.jks文件,并将其上传至OBS桶中。
- 开发说明-https off如果没有开启https访问的话,不需要去生成keystore.jks和truststore.jks文件的,只需要设置好ssl访问和账号密码参数即可。
- 构造依赖信息,创建SparkSession
- 通过SQL API 访问
- 创建DLI跨源访问 CSS的关联表。
1
sparkSession.sql("create table css_table(id long, name string) using css options( 'es.nodes' = '192.168.9.213:9200', 'es.nodes.wan.only' = 'true', 'resource' = '/mytest','es.net.ssl'='false','es.net.http.auth.user'='admin','es.net.http.auth.pass'='*******')");
- 创建CSS跨源表的参数详情可参考表1。
- 上述示例中,因为CSS安全集群关闭了https访问,所以“es.net.ssl”参数要设置为“false”。“es.net.http.auth.user”以及“es.net.http.auth.pass”为创建集群时设置的账号和密码。
- 插入数据
1
sparkSession.sql("insert into css_table values(18, 'John'),(28, 'Bob')");
- 查询数据
1
sparkSession.sql("select * from css_table").show();
- 删除数据表
sparkSession.sql("drop table css_table");
- 创建DLI跨源访问 CSS的关联表。
- 提交Spark作业
- 完整示例代码
- Maven依赖
<dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.11</artifactId> <version>2.3.2</version> </dependency>
- Maven依赖
- 开发说明-https on
- 构造依赖信息,创建SparkSession
- 通过SQL API 访问
- 创建DLI跨源访问 CSS的关联表。
1 2 3
sparkSession.sql("create table css_table(id long, name string) using css options( 'es.nodes' = '192.168.13.189:9200', 'es.nodes.wan.only' = 'true', 'resource' = '/mytest','es.net.ssl'='true','es.net.ssl.keystore.location' = 'obs://桶名/地址/transport-keystore.jks','es.net.ssl.keystore.pass' = '**', 'es.net.ssl.truststore.location'='obs://桶名/地址/truststore.jks', 'es.net.ssl.truststore.pass'='***','es.net.http.auth.user'='admin','es.net.http.auth.pass'='**')");
- 插入数据
1
sparkSession.sql("insert into css_table values(18, 'John'),(28, 'Bob')");
- 查询数据
1
sparkSession.sql("select * from css_table").show();
- 删除数据表
sparkSession.sql("drop table css_table");
- 创建DLI跨源访问 CSS的关联表。
- 提交Spark作业
- 将写好的代码文件生成jar包,上传至DLI中。控制台操作请参考《数据湖探索用户指南》。API操作请参考《数据湖探索API参考》>《上传资源包》。
- 如果是开启https访问场景,在创建Spark作业时,需要同时上传依赖文件“hadoop-site.xml”,上传依赖文件的界面位置如图1所示。“hadoop-site.xml”文件具体内容参考如下:
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- Licensed 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. See accompanying LICENSE file. --> <!-- Put site-specific property overrides in this file. --> <configuration> <property> <name>fs.obs.bucket.桶名.access.key</name> <value>AK</value> </property> <property> <name>fs.obs.bucket.桶名.secret.key </name> <value>SK</value> </property> </configuration>
<name>fs.obs.bucket.桶名.access.key</name>是为了更好的定位桶地址,该桶名为存放keystore.jks和truststore.jks文件的桶名。
- 在Spark作业编辑器中选择对应的Module模块并执行Spark作业。控制台操作请参考《数据湖探索用户指南》。API操作请参考《数据湖探索API参考》>《创建批处理作业》。
- 完整示例代码
- Maven依赖
<dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.11</artifactId> <version>2.3.2</version> </dependency>
- 通过SQL API 访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
import org.apache.spark.sql.SparkSession; public class java_css_security_httpson { public static void main(String[] args) { SparkSession sparkSession = SparkSession.builder().appName("datasource-css").getOrCreate(); // Create a DLI data table for DLI-associated CSS sparkSession.sql("create table css_table(id long, name string) using css options( 'es.nodes' = '192.168.13.189:9200', 'es.nodes.wan.only' = 'true', 'resource' = '/mytest','es.net.ssl'='true','es.net.ssl.keystore.location' = 'obs://桶名/地址/transport-keystore.jks','es.net.ssl.keystore.pass' = '**','es.net.ssl.truststore.location'='obs://桶名/地址/truststore.jks','es.net.ssl.truststore.pass'='**','es.net.http.auth.user'='admin','es.net.http.auth.pass'='**')"); //*****************************SQL model*********************************** // Insert data into the DLI data table sparkSession.sql("insert into css_table values(34, 'Yuan'),(28, 'Kids')"); // Read data from DLI data table sparkSession.sql("select * from css_table").show(); // drop table sparkSession.sql("drop table css_table"); sparkSession.close(); } }
- Maven依赖
父主题: 对接CSS
