hbase表导入导出数据
1.使用 HBase Shell 导入导出数据
导出数据
假设你有一个 HBase 表 my_table,你可以使用 hbase shell 导出数据到一个文件中。
hbase org.apache.hadoop.hbase.mapreduce.Export my_table /user/hadoop/exported_data
导入数据
你可以使用 hbase shell 将数据从文件导入到 HBase 表中。
hbase org.apache.hadoop.hbase.mapreduce.Import my_table /user/hadoop/exported_data
2.使用 HBase Java API 导入导出数据
导出数据
下面是一个使用 HBase Java API 导出数据的示例。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableInputFormat;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
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.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import java.io.IOException;
public class HBaseExport {
public static class HBaseExportMapper extends TableMapper<ImmutableBytesWritable, Text> {
private static final byte[] FAMILY = Bytes.toBytes("cf");
private static final byte[] QUALIFIER = Bytes.toBytes("column1");
@Override
protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
String rowKey = Bytes.toString(key.get());
String valueStr = Bytes.toString(value.getValue(FAMILY, QUALIFIER));
context.write(key, new Text(rowKey + "\t" + valueStr));
}
}
public static void main(String[] args) throws Exception {
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "your_zookeeper_quorum");
config.set("hbase.zookeeper.property.clientPort", "2181");
Job job = Job.getInstance(config, "HBase Export");
job.setJarByClass(HBaseExport.class);
TableMapReduceUtil.initTableMapperJob(
"my_table", // 表名
new Scan(), // 扫描配置
HBaseExportMapper.class, // Mapper 类
ImmutableBytesWritable.class, // 输出 key 类型
Text.class, // 输出 value 类型
job);
FileOutputFormat.setOutputPath(job, new Path("/user/hadoop/exported_data"));
job.setOutputFormatClass(TextOutputFormat.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
导入数据
下面是一个使用 HBase Java API 导入数据的示例。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import java.io.IOException;
public class HBaseImport {
public static class HBaseImportMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {
private static final byte[] FAMILY = Bytes.toBytes("cf");
private static final byte[] QUALIFIER = Bytes.toBytes("column1");
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] parts = value.toString().split("\t");
if (parts.length == 2) {
String rowKey = parts[0];
String valueStr = parts[1];
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(FAMILY, QUALIFIER, Bytes.toBytes(valueStr));
context.write(new ImmutableBytesWritable(Bytes.toBytes(rowKey)), put);
}
}
}
public static void main(String[] args) throws Exception {
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "your_zookeeper_quorum");
config.set("hbase.zookeeper.property.clientPort", "2181");
Job job = Job.getInstance(config, "HBase Import");
job.setJarByClass(HBaseImport.class);
FileInputFormat.addInputPath(job, new Path("/user/hadoop/exported_data"));
job.setInputFormatClass(TextInputFormat.class);
TableMapReduceUtil.initTableReducerJob(
"my_table", // 表名
null, // Reducer 类,这里不需要 Reducer
job);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}