添加依赖:
junit junit 4.12 test org.apache.spark spark-core_2.11 ${spark.core.version} org.apache.spark spark-sql_2.11 ${spark.core.version} mysql mysql-connector-java 5.1.38 org.apache.spark spark-hive_2.11 2.3.0 log4j log4j 1.2.16
Test 测试:
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
public class Test {
public static void main(String[] args) {
SparkSession spark = SparkSession
.builder()
.master("local[*]")
.appName("Java Spark Hive Example")
.config("hive.metastore.uris", "thrift://192.168.0.4:9083")
.config("spark.sql.warehouse.dir", "hdfs://192.168.0.2:9000/user/hive/warehouse")
.enableHiveSupport()
.getOrCreate();
Long start = System.currentTimeMillis();
Dataset sql = spark.sql("select * from test.fs142 union all select * from test.fs142");
sql.show();
}
}
日志打印:新建 log4j.properties 文件
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
# log4j configuration used during build and unit tests
log4j.rootLogger=ERROR,stdout
log4j.threshold=ALL
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %c{2} (%F:%M(%L)) - %m%n



