内容简介:spark-sql是spark的一个核心组件,可以实现简单的关系型数据库操作。如果进入spark-sql报以下错误:Caused by: ERROR XSDB6: Another instance of Derby may have already booted the database /home/bigdata/spark/bin/metastore_db.
spark-sql是spark的一个核心组件,可以实现简单的关系型数据库操作。
一,启动spark-sql
cd /bigdata/spark/bin ./spark-sql
如果进入spark-sql报以下错误:
Caused by: ERROR XSDB6: Another instance of Derby may have already booted the database /home/bigdata/spark/bin/metastore_db.
解决办法:
[root@bigserver1 bin]# ps aux |grep java |grep -i sql
root 24797 2.1 16.8 3286436 559340 pts/2 Sl+ 13:13 1:23 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/bin/java -cp /bigdata/spark/conf/:/bigdata/spark/jars/*:/bigdata/hadoop/etc/hadoop/ -Xmx512m org.apache.spark.deploy.SparkSubmit --class org.apache.spark.sql.hive.thriftserver.SparkSQLCLIDriver spark-internal
[root@bigserver1 bin]# kill
24797
在启动就好
二,创建表
1,以文本方式存储
create external table mytest1(id bigint, name string) row format delimited fields terminated by ',' location 'hdfs://bigserver1:9000/test/spark/tank3';
这种方式创建的表,是以文本的形式存储的
2,以parquet存储
CREATE TABLE mytest3 (id bigint, name string) USING HIVE OPTIONS(fileFormat 'PARQUET') location 'hdfs://bigserver1:9000/test/spark/tank4';
这种创建表的方式,指定了文件存储方式,在用scala读取时会方便一些。
在这里要注意一点,如果没有指定location的话,默认会装到
spark-sql 创建表
hive_serde_tab2,hive_serde_tab1表名
三,添加数据
INSERT INTO mytest3 VALUES (1,"zhang"), (2,"tank")
对于数据的编辑和删除,纯spark-sql现在还不支持,后面会尝试着结合,hive和hbase,来进行。希望达到的目的就是,能实现简单的增,删,改,查
四,单表查询数据,根关系型的差不多
五,用scala去读取上面创建的二个表
1,读取文本表
scala> var test = spark.read.format("text").option("header", true).option("delimiter", ",").load("hdfs://bigserver1:9000/test/spark/tank3");
test: org.apache.spark.sql.DataFrame = [value: string]
scala> test.withColumn("_tmp", split($"value", ",")).select(
| $"_tmp".getItem(0).as("id"),
| $"_tmp".getItem(1).as("name")
| ).drop("_tmp").show();
+---+-----+
| id| name|
+---+-----+
| 2|zhang|
| 3| ying|
| 1| tank|
+---+-----+
2,读取parquet表
scala> var test = spark.read.load("hdfs://bigserver1:9000/test/spark/tank4");
test: org.apache.spark.sql.DataFrame = [id: bigint, name: string]
scala> test.show();
+---+-----+
| id| name|
+---+-----+
| 1|zhang|
| 2| tank|
+---+-----+
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
An Introduction to Genetic Algorithms
Melanie Mitchell / MIT Press / 1998-2-6 / USD 45.00
Genetic algorithms have been used in science and engineering as adaptive algorithms for solving practical problems and as computational models of natural evolutionary systems. This brief, accessible i......一起来看看 《An Introduction to Genetic Algorithms》 这本书的介绍吧!