美文网首页
spark远程读写hive数据库

spark远程读写hive数据库

作者: iE简 | 来源:发表于2020-12-04 21:39 被阅读0次

    实现spark远程连接hive数据库,需要将服务端mysql数据库里的hive数据表DBS和SDS里的localhost改为可以访问到的IP地址或域名。


    image.png

    更改方式参考:hive修改localhost
    本机spark访问服务端应采用域名的方式。

    配置hostname

    作者采用dbhive作为统一访问hostname
    服务端修改/etc/hosts:

    nano /etc/hosts
    
    # 10.211.55.2是服务端内网地址
    10.211.55.2     dbhive
    

    本机修改/etc/hosts:

    nano /etc/hosts
    
    # 119.23.5.xx是服务端外网地址
    119.23.5.xx     dbhive
    

    服务端配置

    hadoop下的core-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.defaultFS</name>
            <value>hdfs://0.0.0.0:9000</value>
        </property>
        <property>
            <name>hadoop.tmp.dir</name>
            <value>/Users/zhi/Documents/app/hadoopdata</value>
        </property>
        <property>
            <name>hadoop.proxyuser.root.hosts</name>
            <value>*</value>
        </property>
        <property>
            <name>hadoop.proxyuser.root.groups</name>
            <value>*</value>
        </property>
    </configuration>
    
    • fs.defaultFS: 注意将ip设为0.0.0.0,允许外部访问。

    hadoop下的hdfs-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>dfs.replication</name>
            <value>1</value>
        </property>
        <property>
            <name>dfs.datanode.hostname</name>
            <value>dbhive</value>
        </property>
        <property>
            <name>dfs.client.use.datanode.hostname</name>
            <value>true</value>
        </property>
        <property>
            <name>dfs.datanode.use.datanode.hostname</name>
            <value>true</value>
        </property>
    </configuration>
    

    hive下的hive-site.xml:

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    <configuration>
        <property>
            <name>hive.metastore.local</name>
            <value>false</value>
        </property>
        <property>  
            <name>hive.metastore.uris</name>  
            <value>thrift://0.0.0.0:9083</value>  
            <description>Thrift uri for the remote metastore. Used by metastore client to connect to remote metastore.</description>  
        </property>
        <property>
            <name>hive.server2.thrift.bind.host</name>
            <value>localhost</value>
        </property>
        <property>
            <name>hive.server2.thrift.port</name>
            <value>10000</value>
        </property>
        <property>
            <name>javax.jdo.option.ConnectionURL</name>
            <value>jdbc:mysql://localhost:3306/hivedata?characterEncoding=UTF-8</value>
        </property>
        <property>
            <name>javax.jdo.option.ConnectionDriverName</name>
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property>
            <name>hive.server2.enable.doAs</name>
            <value>false</value> 
        </property>
        <property>
            <name>javax.jdo.option.ConnectionUserName</name>
            <value>hive</value>
        </property>
        <property>
            <name>javax.jdo.option.ConnectionPassword</name>
            <value>228228</value>
        </property>
        <property>
            <name>hive.metastore.schema.verification</name>
            <value>false</value>
        </property>
        <property>
            <name>hive.server2.thrift.client.user</name>
            <value>hive</value>
            <description>Username to use against thrift client</description>
        </property>
        <property>
            <name>hive.server2.thrift.client.password</name>
            <value>hive228228</value>
            <description>Password to use against thrift client</description>
        </property> 
    </configuration>
    

    注意将hive.metastore.uris的ip段设为0.0.0.0。

    本机spark配置文件:

    在spark/conf下有三个文件core-site.xml、hdfs-site.xml、hive-site.xml(从服务端拷贝过来)。
    core-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.defaultFS</name>
            <value>hdfs://dbhive:9000</value>
        </property>
        <property>
            <name>hadoop.tmp.dir</name>
            <value>/Users/zhi/Documents/app/hadoopdata</value>
        </property>
    </configuration>
    
    • fs.defaultFS: 服务端的hdfs地址;
    • hadoop.tmp.dir: 和服务端保持一致。

    hdfs-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>dfs.replication</name>
            <value>1</value>
        </property>
        <property>
            <name>dfs.datanode.hostname</name>
            <value>dbhive</value>
        </property>
        <property>
            <name>dfs.datanode.use.datanode.hostname</name>
            <value>true</value>
        </property>
        <property>
            <name>dfs.client.use.datanode.hostname</name>
            <value>true</value>
        </property>
    </configuration>
    
    • dfs.datanode.hostname: 设置datanode的hostname;
    • dfs.datanode.use.datanode.hostname: datanode通讯采用域名方式;
    • dfs.client.use.datanode.hostname: 客户端访问datanode采用域名方式。

    hive-site.xml:

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    <configuration>
        <property>
            <name>hive.metastore.uris</name>
            <value>thrift://dbhive:9083</value>
        </property>
        <property>
            <name>javax.jdo.option.ConnectionURL</name>
            <value>jdbc:mysql://dbhive:3306/hivedata?characterEncoding=UTF-8</value>
        </property>
        <property>
            <name>javax.jdo.option.ConnectionDriverName</name>
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property>
            <name>javax.jdo.option.ConnectionUserName</name>
            <value>hive</value>
        </property>
        <property>
            <name>javax.jdo.option.ConnectionPassword</name>
            <value>123456</value>
        </property>
    </configuration>
    
    • hive.metastore.uris: 访问metastore采用域名方式;
    • javax.jdo.option.ConnectionURL: 服务器mysql地址;
    • javax.jdo.option.ConnectionUserName: 数据库用户;
    • javax.jdo.option.ConnectionPassword: 数据库密码。

    之后本机spark连接远程的metastore就可以读写了。

    配置远程访问最重要的是要配置hadoop下的hdfs-site.xml,设置dfs.datanode.hostname域名,以及开启dfs.client.use.datanode.hostname和dfs.datanode.use.datanode.hostname开启域名访问。hdfs和metastore地址设为0.0.0.0.

    相关文章

      网友评论

          本文标题:spark远程读写hive数据库

          本文链接:https://www.haomeiwen.com/subject/runiwktx.html