-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: 1.加入spark 1.0结合hive的使用例子
- Loading branch information
Showing
11 changed files
with
149 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/com/wugui/sparkstarter/SparkHiveOldVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.wugui.sparkstarter; | ||
|
||
import org.apache.spark.SparkConf; | ||
import org.apache.spark.api.java.JavaSparkContext; | ||
import org.apache.spark.sql.Dataset; | ||
import org.apache.spark.sql.SaveMode; | ||
import org.apache.spark.sql.hive.HiveContext; | ||
|
||
/** | ||
* spark老版本支持hive的使用方法 | ||
* 2.0spark已经不推荐使用hiveContext,改用SparkSession进行统一 | ||
**/ | ||
public class SparkHiveOldVersion { | ||
|
||
|
||
public static void main(String[] args) { | ||
SparkConf conf = new SparkConf(); | ||
conf.setAppName("hive"); | ||
conf.setMaster("local"); | ||
JavaSparkContext sc = new JavaSparkContext(conf); | ||
|
||
//HiveContext是SQLContext的子类。 | ||
HiveContext hiveContext = new HiveContext(sc); | ||
hiveContext.sql("USE default"); | ||
hiveContext.sql("show tables").show(); | ||
hiveContext.sql("DROP TABLE IF EXISTS student_infos"); | ||
//在hive中创建student_infos表 | ||
hiveContext.sql("CREATE TABLE IF NOT EXISTS student_infos (name STRING,age INT) row format delimited fields terminated by ',' "); | ||
hiveContext.sql("load data local inpath '/Users/huzekang/study/spark-starter/src/main/resources/student_infos.txt' into table student_infos"); | ||
|
||
hiveContext.sql("DROP TABLE IF EXISTS student_scores"); | ||
hiveContext.sql("CREATE TABLE IF NOT EXISTS student_scores (name STRING, score INT) row format delimited fields terminated by ','"); | ||
hiveContext.sql("LOAD DATA " | ||
+ "LOCAL INPATH '/Users/huzekang/study/spark-starter/src/main/resources/student_scores.txt'" | ||
+ "INTO TABLE student_scores"); | ||
|
||
hiveContext.sql("select * from student_infos").show(); | ||
hiveContext.sql("select * from student_scores").show(); | ||
/** | ||
* 查询表生成DataFrame | ||
*/ | ||
Dataset goodStudentsDF = hiveContext.sql("SELECT si.name, si.age, ss.score " | ||
+ "FROM student_infos si " | ||
+ "JOIN student_scores ss " | ||
+ "ON si.name=ss.name " | ||
+ "WHERE ss.score>=80"); | ||
|
||
hiveContext.sql("DROP TABLE IF EXISTS good_student_infos"); | ||
|
||
goodStudentsDF.registerTempTable("goodstudent"); | ||
Dataset result = hiveContext.sql("select * from goodstudent"); | ||
result.show(); | ||
|
||
/** | ||
* 将结果保存到hive表 good_student_infos | ||
*/ | ||
goodStudentsDF.write().mode(SaveMode.Overwrite).saveAsTable("good_student_infos"); | ||
|
||
// Row[] goodStudentRows = hiveContext.table("good_student_infos").collect(); | ||
// for(Row goodStudentRow : goodStudentRows) { | ||
// System.out.println(goodStudentRow); | ||
// } | ||
sc.stop(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
# Set everything to be logged to the console | ||
log4j.rootCategory=WARN, console | ||
log4j.appender.console=org.apache.log4j.ConsoleAppender | ||
log4j.appender.console.target=System.err | ||
log4j.appender.console.layout=org.apache.log4j.PatternLayout | ||
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n | ||
|
||
# Settings to quiet third party logs that are too verbose | ||
log4j.logger.org.spark-project.jetty=WARN | ||
log4j.logger.org.spark-project.jetty.util.component.AbstractLifeCycle=ERROR | ||
log4j.logger.org.apache.spark.repl.SparkIMain$exprTyper=INFO | ||
log4j.logger.org.apache.spark.repl.SparkILoop$SparkILoopInterpreter=INFO | ||
log4j.logger.org.apache.parquet=ERROR | ||
log4j.logger.parquet=ERROR | ||
|
||
# SPARK-9183: Settings to avoid annoying messages when looking up nonexistent UDFs in SparkSQL with Hive support | ||
log4j.logger.org.apache.hadoop.hive.metastore.RetryingHMSHandler=FATAL | ||
log4j.logger.org.apache.hadoop.hive.ql.exec.FunctionRegistry=ERROR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
李小龙,18 | ||
周大侠,19 | ||
肖鸿飞,17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
李小龙,67 | ||
周大侠,88 | ||
肖鸿飞,59 |