Mapreduce读取hbase汇总到RDBMS
前言 Hbase对Mapreduce API进行了扩展,方便Mapreduce任务读写HTable数据。 HBase作为源的MapReduce读取示例 package hbase;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import jav
前言
Hbase对Mapreduce API进行了扩展,方便Mapreduce任务读写HTable数据。
HBase作为源的MapReduce读取示例
<code>package hbase; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil; import org.apache.hadoop.hbase.mapreduce.TableMapper; import org.apache.hadoop.hbase.mapreduce.TableReducer; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat; public class ExampleHbaseToMysqlMapreduce { public static void main(String[] args) throws Exception { //hbase配置 Configuration config = HBaseConfiguration.create(); String tableName = "flws"; Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes("5768014")); scan.setStopRow(Bytes.toBytes("5768888")); scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("AH")); scan.setCaching(500); scan.setCacheBlocks(false); //JOB定义 Job job = new Job(config, "ExampleHbaseMapreduce"); job.setJarByClass(ExampleHbaseToMysqlMapreduce.class); //设置map读取hbase方法 TableMapReduceUtil.initTableMapperJob(tableName, scan, MyMapper.class, Text.class,Text.class, job); //reduce设置 job.setReducerClass(MyReducer.class); job.setOutputFormatClass(NullOutputFormat.class); job.setNumReduceTasks(5); boolean b = job.waitForCompletion(true); if (!b) { throw new Exception("error with job!"); } } public static class MyMapper extends TableMapper<text text> { public void map(ImmutableBytesWritable row, Result value, Context context) throws IOException, InterruptedException { context.write( new Text(row.get()), new Text(value.getValue(Bytes.toBytes("cf"), Bytes.toBytes("AH")))); } } public static class MyReducer extends TableReducer<text text immutablebyteswritable> { private Connection conn = null; @Override protected void cleanup(Context context) throws IOException, InterruptedException { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } @Override protected void setup(Context context) throws IOException, InterruptedException { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://172.16.35.242/judgment?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull"; try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection(url, "root", "root"); } catch (SQLException e) { e.printStackTrace(); } super.setup(context); } public void reduce(Text key, Iterable<text> values, Context context) throws IOException, InterruptedException { StringBuffer sb = new StringBuffer(); for (Text text : values) { sb.append(text.toString()); } try { Statement st = conn.createStatement(); st.executeUpdate("insert into test_mapreduce (id,ah) values (" + Integer.valueOf(key.toString()) + ",'" + sb.toString() + "')"); } catch (SQLException e) { e.printStackTrace(); } } } } </text></text></text></code>
原文地址:Mapreduce读取hbase汇总到RDBMS, 感谢原作者分享。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use pandas to read txt files correctly requires specific code examples. Pandas is a widely used Python data analysis library. It can be used to process a variety of data types, including CSV files, Excel files, SQL databases, etc. At the same time, it can also be used to read text files, such as txt files. However, when reading txt files, we sometimes encounter some problems, such as encoding problems, delimiter problems, etc. This article will introduce how to read txt correctly using pandas

Practical tips for reading txt files using pandas, specific code examples are required. In data analysis and data processing, txt files are a common data format. Using pandas to read txt files allows for fast and convenient data processing. This article will introduce several practical techniques to help you better use pandas to read txt files, along with specific code examples. Reading txt files with delimiters When using pandas to read txt files with delimiters, you can use read_c

Example of using OpenCSV to read and write CSV files in Java. CSV (Comma-SeparatedValues) refers to comma-separated values and is a common data storage format. In Java, OpenCSV is a commonly used tool library for reading and writing CSV files. This article will introduce how to use OpenCSV to implement examples of reading and writing CSV files. Introducing the OpenCSV library First, you need to introduce the OpenCSV library to

The practical method of reading web page data in Pandas requires specific code examples. During data analysis and processing, we often need to obtain data from web pages. As a powerful data processing tool, Pandas provides convenient methods to read and process web page data. This article will introduce several commonly used practical methods for reading web page data in Pandas, and attach specific code examples. Method 1: Use the read_html() function. Pandas’ read_html() function can read directly from the web page.

How to read Excel files with PHP and FAQs Excel is a very common spreadsheet file format, and many businesses and data are stored in Excel files. During the development process, if you need to import the data in the Excel file into the system, you need to use PHP to read the Excel file. This article will introduce how to read Excel files with PHP and answer common questions. 1. How to read Excel files with PHP 1. Use the PHPExcel class library PHPExcel is a P

Quick Start: Pandas method of reading JSON files, specific code examples are required Introduction: In the field of data analysis and data science, Pandas is one of the important Python libraries. It provides rich functions and flexible data structures, and can easily process and analyze various data. In practical applications, we often encounter situations where we need to read JSON files. This article will introduce how to use Pandas to read JSON files, and attach specific code examples. 1. Installation of Pandas

How to read binary files in Golang? Binary files are files stored in binary form that contain data that a computer can recognize and process. In Golang, we can use some methods to read binary files and parse them into the data format we want. The following will introduce how to read binary files in Golang and give specific code examples. First, we need to open a binary file using the Open function from the os package, which will return a file object. Then we can make

Getting started with PHP file processing: Step-by-step guide for reading and writing In web development, file processing is a common task, whether it is reading files uploaded by users or writing the results to files for subsequent use. Understand how to use PHP Document processing is very important. This article will provide a simple guide to introduce the basic steps of reading and writing files in PHP, and attach code examples for reference. File reading in PHP, you can use the fopen() function to open a file and return a file resource (file
