栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA学习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA学习

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class ExampleForHbase{
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
 

    public static void main(String[] args)throws IOException{
        //创建一个表,表名为SC,列族为student,course,grade
        createTable("SC",new String[]{"student","course","grade"});
//        //insert dataset into SC
//        readFile("/home/hadoop/桌面/course.txt","course");
//        readFile("/home/hadoop/桌面/student.txt","student");
//        readFile("/home/hadoop/桌面/grade.txt","grade");
        //search data from SC
        getData("SC", "13998", "student", "");
        getData("SC", "13998", "course", "");
        getData("SC", "13998", "grade", "");
 
    }
  public static void readFile(String filePath,String fileName) {
      try
      {
          String encoding = "GBK";
          File file = new File(filePath);
          if (file.isFile() && file.exists())
          { // file exist
              InputStreamReader read = new InputStreamReader(
                      new FileInputStream(file), encoding);
              BufferedReader bufferedReader = new BufferedReader(read);
              String lineTxt = null;
              int i=1;
              while ((lineTxt = bufferedReader.readLine()) != null)
              {
            	  String Key = String.valueOf(i);
            	  insertRow("SC", Key, fileName, "", lineTxt);
            	  i = Integer.parseInt(Key);
            	  i++;
              }
              System.out.println("insert"+fileName+"success!");
              bufferedReader.close();
              read.close();
          }
          else
          {
              System.out.println("cant find file");
          }
      }
      catch (Exception e)
      {
          System.out.println("error!");
          e.printStackTrace();
      }
    }
    //建立连接
    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //关闭连接
    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void createTable(String myTableName,String[] colFamily) throws IOException {
 
        init();
        TableName tableName = TableName.valueOf(myTableName);
 
        if(admin.tableExists(tableName)){
            System.out.println("talbe is exists!");
        }else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for(String str:colFamily){
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
            System.out.println("create table success");
        }
        close();
    }

    public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
        table.put(put);
        table.close();
        close();
    }
    public static void getData(String tableName,String rowKey,String colFamily,String col)throws  IOException{
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(colFamily.getBytes(),col.getBytes());
        Result result = table.get(get);
        showCell(result);
        table.close();
        close();
    }
    public static void showCell(Result result){
        Cell[] cells = result.rawCells();
        for(Cell cell:cells){
            System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
            System.out.println("Timetamp:"+cell.getTimestamp()+" ");
            System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
            System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
            System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
        }
    }
}

数据集

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995749.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号