资源简介
java从本地读文件并上传Hbase
代码片段和文件信息
/**
* Created by Jiao on 2017/4/3.
*/
import java.io.IOException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.text.NumberFormat;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HbaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HbaseAdmin;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Put;
public class project {
public static void main(String[] args) throws IOException {
//writeback to hbase
Configuration Hbase_CONFIG = new Configuration();
// Hbase_CONFIG.set(“hbase.zookeeper.quorum“ “192.168.0.104“);
String tableName = “HB_MEM_“;
String family=“cf“;
HbaseAdmin hbaseAdmin = new HbaseAdmin(Hbase_CONFIG);
if (hbaseAdmin.tableExists(tableName)) { //check
hbaseAdmin.disableTable(tableName);
hbaseAdmin.deleteTable(tableName);
System.out.println(tableName + “ is existdetele....“);
}
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
HColumnDescriptor cf= new HColumnDescriptor(family);
htd.addFamily(cf);
hbaseAdmin.createTable(htd);
hbaseAdmin.close();
HTable Hbasetable = new HTable(Hbase_CONFIGTableName.valueOf(tableName));
String filePath = “/root/input_2“;
File file=new File(filePath);
InputStreamReader in_stream = new InputStreamReader(new FileInputStream(file));
BufferedReader in = new BufferedReader(in_stream);
String s;
int i=0;
while ((s=in.readLine())!=null ) {
String[] words = s.split(“ “);
String key = words[0];
String value=words[1];
Put put = new Put(key.getBytes());
put.add(family.getBytes() “value“.getBytes() value.getBytes());
System.out.println(“Save to Hbase! key:“+key+“ “+“value:“+value);
Hbasetable.put(put);
}
Hbasetable.close();
System.out.println(“put successful!!!“);
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2580373 2017-05-22 09:14 project\input_2
文件 2575 2017-05-22 10:23 project\project.java
目录 0 2017-05-22 10:23 project
----------- --------- ---------- ----- ----
2582948 3
- 上一篇:网络爬虫 Java实现原理
- 下一篇:三边测量法java实现
评论
共有 条评论