HDFS与关系型数据库数据交换利器—sqoop初探
Sqoop是一种用于 hadoop 与 RDBMS 进行数据传输的工具。 配置比较简单。 去apache官网下载最新的 sqoop 包。 下载地址:http://www.apache.org/dist/ sqoop /1.99.1/ 解压缩到服务器上。服务器要求本身有jdk, hadoop , hive 。 配置: conf/sqoop-env.sh #
Sqoop是一种用于hadoop与RDBMS进行数据传输的工具。
配置比较简单。
去apache官网下载最新的sqoop包。
下载地址:http://www.apache.org/dist/sqoop/1.99.1/
解压缩到服务器上。服务器要求本身有jdk,hadoop,hive。
配置:
conf/sqoop-env.sh
#Set path to where bin/hadoop is available
export HADOOP_HOME=/home/hadoop/hadoop-0.20.205.0
#Set the path to where bin/hive is available
export HIVE_HOME=/home/hadoop/hive-0.8.1
这时候就可以进行试验了。我们主要是利用其与hive进行交互,实际就是将关系型的数据库中的数据提交到hive,保存到HDFS中,以便于大数据的计算。
sqoop主要包含了以下命令,或者说功能。
codegen Import a table definition into Hive eval Evaluate a SQL statement and display the results export Export an HDFS directory to a database table help List available commands import Import a table from a database to HDFS import-all-tables Import tables from a database to HDFS job Work with saved jobs list-databases List available databases on a server list-tables List available tables in a database merge Merge results of incremental imports metastore Run a standalone Sqoop metastore version Display version information <code> 这里主要是使用其中的import功能。export功能的命令语法类似。</code>
示例
./sqoop import --connect jdbc:mysql://lcoalhost:3306/dbname--username dbuser --password dbpassword --table tablename --hive-import --hive-table hivedb.hivetable --hive-drop-import-delims --hive-overwrite --num-mappers 6
以上命令的意思就是要将本地数据库dbname中的tablename表的数据导入到hivedb的hivetable表中。
其中一些常用的参数就不进行解释了。
–hive-import 标识本次导入的地址为hive
–hive-table 标识hive中的表信息
–hive-drop-import-delims 这个比较重要,因为数据从数据库中导入到HDFS中,如果包含了特殊的字符,对MR解析是存在问题的,比如数据库中
有text类型的字段,有可能包含\t,\n等参数,加入这个参数后,会自动将特殊字符进行处理。
–hive-overwrite 如果原有的hive表已经存在,则会进行覆盖操作。
–num-mappers 会指定执行本次导入的mapper任务数量。
还有一个比较重要的参数 –direct 这个参数可以通过数据库的dump功能进行数据导入,这样的性能比上例更好,但是其不能与–hive-drop-import-delims参数功能使用。所以还是要根据自己数据库的情况来进行判断使用何种命令。
如下是sqoop的import命令
Argument | Description |
---|---|
--connect <jdbc-uri></jdbc-uri>
|
Specify JDBC connect string |
--connection-manager <class-name></class-name>
|
Specify connection manager class to use |
--driver <class-name></class-name>
|
Manually specify JDBC driver class to use |
--hadoop-home <dir></dir>
|
Override $HADOOP_HOME |
--help
|
Print usage instructions |
-P
|
Read password from console |
--password <password></password>
|
Set authentication password |
--username <username></username>
|
Set authentication username |
--verbose
|
Print more information while working |
--connection-param-file <filename></filename>
|
Optional properties file that provides connection parameters |
Argument | Description |
---|---|
--hive-home <dir></dir>
|
Override $HIVE_HOME
|
--hive-import
|
Import tables into Hive (Uses Hive’s default delimiters if none are set.) |
--hive-overwrite
|
Overwrite existing data in the Hive table. |
--create-hive-table
|
If set, then the job will fail if the target hive |
table exits. By default this property is false. | |
--hive-table <table-name></table-name>
|
Sets the table name to use when importing to Hive. |
--hive-drop-import-delims
|
Drops \n, \r, and \01 from string fields when importing to Hive. |
--hive-delims-replacement
|
Replace \n, \r, and \01 from string fields with user defined string when importing to Hive. |
--hive-partition-key
|
Name of a hive field to partition are sharded on |
--hive-partition-value <v></v>
|
String-value that serves as partition key for this imported into hive in this job. |
--map-column-hive <map></map>
|
Override default mapping from SQL type to Hive type for configured columns. |
以下为一些参考示例
写入条件
sqoop import –table test –columns “id,name” –where “id>400″
使用dump功能
sqoop import –connect jdbc:mysql://server.foo.com/db –table bar –direct — –default-character-set=latin1
列类型重新定义
sqoop import … –map-column-java id=String,value=Integer
定义分割符
sqoop import –connect jdbc:mysql://db.foo.com/corp –table EMPLOYEES –fields-terminated-by ‘\t’ –lines-terminated-by ‘\n’ –optionally-enclosed-by ‘\”‘
原文地址:HDFS与关系型数据库数据交换利器—sqoop初探, 感谢原作者分享。

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL asynchronous master-slave replication enables data synchronization through binlog, improving read performance and high availability. 1) The master server record changes to binlog; 2) The slave server reads binlog through I/O threads; 3) The server SQL thread applies binlog to synchronize data.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The installation and basic operations of MySQL include: 1. Download and install MySQL, set the root user password; 2. Use SQL commands to create databases and tables, such as CREATEDATABASE and CREATETABLE; 3. Execute CRUD operations, use INSERT, SELECT, UPDATE, DELETE commands; 4. Create indexes and stored procedures to optimize performance and implement complex logic. With these steps, you can build and manage MySQL databases from scratch.

InnoDBBufferPool improves the performance of MySQL databases by loading data and index pages into memory. 1) The data page is loaded into the BufferPool to reduce disk I/O. 2) Dirty pages are marked and refreshed to disk regularly. 3) LRU algorithm management data page elimination. 4) The read-out mechanism loads the possible data pages in advance.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version
SublimeText3 Linux latest version

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.