Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.io.BufferedReader;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
- import java.util.Scanner;
- import org.eclipse.rdf4j.model.BNode;
- import org.eclipse.rdf4j.model.ValueFactory;
- import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
- import org.eclipse.rdf4j.model.util.ModelBuilder;
- import org.eclipse.rdf4j.model.vocabulary.RDF;
- import org.eclipse.rdf4j.model.vocabulary.RDFS;
- import org.eclipse.rdf4j.rio.RDFFormat;
- import org.eclipse.rdf4j.rio.Rio;
- import org.teiid.adminapi.Admin;
- import org.teiid.adminapi.Model;
- import org.teiid.adminapi.VDB;
- import org.teiid.adminapi.VDB.ConnectionType;
- import org.teiid.adminapi.impl.ModelMetaData;
- import org.teiid.adminapi.impl.VDBMetaData;
- import org.teiid.adminapi.impl.VDBMetadataParser;
- import org.teiid.adminapi.jboss.AdminFactory;
- import org.teiid.jdbc.TeiidDriver;
- import main.AdminUtil;
- public class Main {
- // Declaration of static variable
- private static Admin admin;
- @SuppressWarnings("resource")
- public static void main(String[] args) throws Exception {
- /*
- * Creating the cube structure and input it to the data structure
- */
- // Variable declaration
- String fileName;
- String[] columns;
- Map<String,String> columnDataTypes = new HashMap<String,String>();
- List<Measure> measures = new ArrayList<Measure>();
- Measure measure = new Measure(null,null,null);
- List<Dimension> dimensions = new ArrayList<Dimension>();
- Dimension dimension = new Dimension(null);
- String dimensionName;
- Hierarchy hierarchy = new Hierarchy(null);
- String hierarchyName;
- Level level = new Level(null);
- String levelName;
- Scanner reader = new Scanner(System.in);
- // Defining columns' data types.
- System.out.print("Input the path to the CSV file: ");
- reader = new Scanner(System.in);
- String filePath = reader.nextLine();
- String temp[] = filePath.split("/");
- fileName = temp[temp.length-1].substring(0, temp[temp.length-1].length()-4);
- BufferedReader br = new BufferedReader(new FileReader(filePath));
- columns = br.readLine().toLowerCase().split(",");
- for(int i=0;i<columns.length;i++) {
- System.out.println("Choose the data type of the column "+columns[i]);
- System.out.println("1 for number (decimal), 2 for string");
- System.out.print("DataType chosen: ");
- String tempDataType = reader.next();
- if(tempDataType.equals("1")) {
- columnDataTypes.put(columns[i].toLowerCase(), "decimal");
- } else if(tempDataType.equals("2")) {
- columnDataTypes.put(columns[i].toLowerCase(), "string");
- }
- }
- br.close();
- /*
- * End of creating turtle file process
- */
- /*
- * Creating VDB
- */
- // Creating the VDB using VDBMetaData Class
- VDBMetaData vdbMD = new VDBMetaData();
- vdbMD.setName(fileName);
- vdbMD.setDescription(fileName+" vdb description");
- vdbMD.setVersion(1);
- vdbMD.setConnectionType(ConnectionType.BY_VERSION);
- // Model definition
- // The first model is the physical, for the connection to the physical CSV file
- ModelMetaData physicalModel = new ModelMetaData();
- physicalModel.setName("physicalModel");
- physicalModel.setModelType(Model.Type.PHYSICAL);
- // Setting the properties of the physical model
- physicalModel.addSourceMapping("text-connector", "file", "java:/textconnector-file");
- // Adding new HashMap, changing from decimal to bigdecimal for Teiid RDB compatibility
- Map<String,String> teiidDataTypes = new HashMap<String,String>();
- for(Map.Entry<String, String> columnDataType : columnDataTypes.entrySet()) {
- if(columnDataType.getValue().equals("decimal")){
- teiidDataTypes.put(columnDataType.getKey(), "bigdecimal");
- }else {
- teiidDataTypes.put(columnDataType.getKey(), "string");
- }
- }
- // The second model is the virtual one, for creating the virtual relational database from the CSV file
- ModelMetaData virtualModel = new ModelMetaData();
- virtualModel.setName("virtualModel");
- virtualModel.setModelType(Model.Type.VIRTUAL);
- String teiidDDLView = "CREATE VIEW "+fileName+" (\n";
- int tempIterator = 0;
- for(Map.Entry<String, String> teiidDataType : teiidDataTypes.entrySet()) {
- System.out.println("tempIterator="+tempIterator);
- System.out.println("size="+Integer.toString(teiidDataTypes.size()-1));
- teiidDDLView = teiidDDLView.concat(" "+teiidDataType.getKey()+" "+teiidDataType.getValue());
- if(tempIterator!=teiidDataTypes.size()-1) {
- teiidDDLView = teiidDDLView.concat(",");
- }
- teiidDDLView = teiidDDLView.concat("\n");
- tempIterator++;
- }
- teiidDDLView = teiidDDLView.concat(" ) AS \n");
- teiidDDLView = teiidDDLView.concat(" SELECT ");
- tempIterator = 0;
- for(Map.Entry<String, String> teiidDataType : teiidDataTypes.entrySet()) {
- if(tempIterator!=teiidDataTypes.size()-1) {
- teiidDDLView = teiidDDLView.concat("file."+teiidDataType.getKey()+", ");
- }else {
- teiidDDLView = teiidDDLView.concat("file."+teiidDataType.getKey()+"\n");
- }
- tempIterator++;
- }
- teiidDDLView = teiidDDLView.concat(" FROM (EXEC physicalModel.getTextFiles('"+fileName+".csv')) AS f, \n");
- teiidDDLView = teiidDDLView.concat(" TEXTTABLE(f.file COLUMNS ");
- tempIterator=0;
- for(Map.Entry<String, String> teiidDataType : teiidDataTypes.entrySet()) {
- if(tempIterator!=teiidDataTypes.size()-1) {
- teiidDDLView = teiidDDLView.concat(teiidDataType.getKey()+" "+teiidDataType.getValue()+", ");
- }else if(tempIterator==teiidDataTypes.size()-1){
- teiidDDLView = teiidDDLView.concat(teiidDataType.getKey()+" "+teiidDataType.getValue()+" ");
- }
- tempIterator++;
- }
- teiidDDLView = teiidDDLView.concat("HEADER) AS file;");
- virtualModel.addSourceMetadata("DDL",teiidDDLView);
- System.out.println(teiidDDLView);
- vdbMD.addModel(physicalModel);
- vdbMD.addModel(virtualModel);
- // Marshalling
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- VDBMetadataParser.marshell(vdbMD, outStream);
- /*
- * Creating connection and deployment
- */
- Properties connectionProps = new Properties();
- // (!!!) pathnya diambil dari filepath
- connectionProps.setProperty("ParentDirectory", filePath.substring(filePath.lastIndexOf("/")+1));
- connectionProps.setProperty("AllowParentPaths", "true");
- connectionProps.setProperty("class-name", "org.teiid.resource.adapter.file.FileManagedConnectionFactory");
- admin = AdminFactory.getInstance().createAdmin("localhost", AdminUtil.MANAGEMENT_PORT , "admin", "admin".toCharArray());
- AdminUtil.createDataSource(admin, "textconnector-file", "file", connectionProps);
- // nama xmlnya belum diambil dari filename
- admin.deploy(fileName+"-vdb.xml", new ByteArrayInputStream(outStream.toByteArray()));
- /*
- * End of creating connection and deployment
- */
- // VDB Status Test
- VDB vdbTest = admin.getVDB("sales", "1");
- System.out.println(vdbTest.getStatus());
- // Connection Test
- Connection conn = TeiidDriver.getInstance().connect("jdbc:teiid:sales.1@mm://localhost:31000;user=usernew;password=user1664!", null);
- System.out.println("status="+conn.isValid(0));
- Statement stmt = conn.createStatement();
- ResultSet resultSet = stmt.executeQuery("select * from sales");
- ResultSetMetaData rsmd = resultSet.getMetaData();
- int columnsNumber = rsmd.getColumnCount();
- for (int i=1; i<=columnsNumber; i++) {
- System.out.print(rsmd.getColumnName(i) + " ");
- }
- System.out.println("");
- while(resultSet.next()) {
- for (int i=1; i<=columnsNumber; i++) {
- if (i>1) System.out.print(",");
- // System.out.print(resultSet.getString(i));
- System.out.print(resultSet.getObject(i));
- }
- System.out.println("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement