Can anyone post here an example of how to use the MySQLJDBCDataModel in Apache Mahout including the instantiating of a DataSource ?
I want to use the DataModel to generate Recommendations. A Databasetable (lets call it ratings) with user_id, task_id and rating already exists i just need to know how to access this information without creating my own implementation of DataModel.
Thanks in advance!
If you don't want to use JNDI:
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerName("my_database_host");
dataSource.setUser("my_user");
dataSource.setPassword("my_password");
dataSource.setDatabaseName("my_database_name");
JDBCDataModel dataModel = new MySQLJDBCDataModel(
dataSource, "my_prefs_table", "my_user_column",
"my_item_column", "my_pref_value_column", "my_timestamp_column");
Have a look in MySQLJDBCDataModel for details...
The class has a constructor that takes the name of the DataSource, or the DataSource. You just pass it the name (and optionally override the names of the table, columns, etc.) -- what else do you need here?
If you want to know how to look something up in JNDI, then it's as follows, but again this is done for you:
Context context = new InitialContext();
DataSource dataSource =
(DataSource) context.lookup("java:comp/env/" + dataSourceName);
context.close();
(You probably want to use ReloadFromJDBCDataModel too here.)
Related
I have been facing an issue with Web API 2.2 with Web API OData controller. I am doing a PoC in which I need to display the JSON output from my service with different column names than that of corresponding Model props (DB table columns).
(For ex: 'CompanyName' from Customer table should appear as 'cName' in JSON output of my service.)
I am using DB first approach with Northwind database, created a Model with ADO.NET EF and created a controller using OData EF. (all default no code changes so far)
Now, I have tried to get different names using
1) Data Contract and Data Member -> specifying directly on Model class (yes, auto generated one)
2) JsonProperty -> specifying directly on Model class (yes, auto generated one)
3) DTOs [it works but I don't want use DTOs]
Unfortunately, first 2 approaches are not working for me (not sure what I'm missing here) and DTOs I'm trying to avoid.
I'm stuck on this all my day today, appreciate if you can point me to a right approach.
Note: Instead of OData controller if I use regular Web API controller, all works.
I realize this is old, and I'm not sure which version of OData you are using but the simple answer is, you have to specify all of this information in the model builder.
Here's an example.
var builder = new ODataConventionModelBuilder();
var entity = builder.EntitySet<Model>("models").EntityType;
entity.Name = "model";
entity.Property(p => p.Id).Name = "id";
entity.Property(p => p.Name).Name = "name";
entity.Property(p => p.Description).Name = "description";
Good luck!
Similar to how linqpad works, when you just add a database connection, and you automatically get a context to work with.
I did literally like 0 work to get this context.
How do I do something similar in my code?
Say for one of my projects I just want to load up a database
var dataContext = new DataContext(myConnection);
//linqpad has like a typed context, which i'd like
var customer = dataContext.Customers.Where(x => x.Id == 4);
without having to have the Customer class already created and mapped.
How does linqpad do this?
I'm guessing it's some sort of derived data context, but how does it handle the creating of all the models with the correct properties and what not?
Any pointers would be fabulous
LINQPad "cheats" and builds a typed data context behind the scenes using Reflection.Emit. It makes it appear as though something dynamic is going on, but really, it's not that different to adding a "LINQ to SQL Classes" item in like Visual Studio (or using SqlMetal as Stephen suggests).
I'm developing Web Service that has access to database via JDBC. I'm using DAO pattern. I've implemented all necessary methods: findAll, add, update, delete. But I got confused with update method. It has Object as input parameter. But how does he know which field needs to be updated. For example, I need to update field 'name' I use query 'update table set name='smth where id=2' but if I need to update 'surname'?? what is the best practice to tell update method what actually to update?
thank you
You'll need to change your method signature to include a Map of column names and values.
public interface FooDao<K, V> {
// other methods here, of course.
public void update(V target, Map<String, Object> parameters);
}
Have a look at the Spring JDBC template for a nice example of how to design and implement such a thing.
I am new to LINQ to SQL, but have done a lot of database development in the past.
The software I just started working on uses:
// MyDataContext is a sub class of DataContext, that is generated with SqlMetal
MyDataContext db = new MyDataContext (connectionString);
db.CreateDatabase();
to create the database when it is first run.
I need to add some indexes to the tables....
How can I tell the DataContext what indexes I want?
Otherwise how do I control this?
(I could use a sql script, but I like the ideal that db.CreateDatabase will always create a database that matches the data access code)
(For better, or worse the software has full access to the database server and our software often create databases on the fly to store result of model runs etc, so please don’t tell me we should not be creating databases from code)
I seem not to be the only person hitting limts on DataContext.CreateDatabase() see also http://csainty.blogspot.com/2008/02/linq-to-sql-be-careful-of.html
As far as I know the DataContext.CreateDatabase method can only create primary keys.
When you look at the DBML directly, you will see that there are no elements for defining an index. Therefore it is, IMHO, save to assume that CreateDatabase cannot do it.
So the only way I can think of for creating indexes "automatically" is by first calling DataContext.CreateDatabase and then calling DataContext.ExecuteCommand to add the indexes to the tables that were just created.
You can execute SQL Command on DatabaseCreated method.
public partial class DatabaseModelsDataContext : System.Data.Linq.DataContext
{
partial void OnCreated ()
{
var cmdText = #"
IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'IX_LeafKey')
DROP INDEX IX_MyTableColumn
ON [mydb].[dbo].[Leaf];
CREATE INDEX IX_MyTableColumn
ON [mydb].[dbo].[MyTable] ([column]) ;";
ExecuteCommand(cmdText);
}
}
every example I seen shows how to do a update query in linq to sql by doing this.
// grab entity you want to update
entity.UserId = "123"; // update the fields you want to update.
entity.Name = "bob";
Dbcontext.SubmitChanges();
I am wondering can you juse pass in a new object and have it figure it out?
Like could I do this?
Enity myEntity = new Entity();
myEntity.UserId = "123";
myEntity.Name = bob:
// grab entity record
// shove record ito the found record
// it figured out what to update and what no to update
Depending on what exactly you want to do you either need the InsertOnSubmit method, or the Attach method of the respective table (i.e. dbContext.Entities). InsertOnSubmit is used to add a record, while Attach can be used if you want to affect an UPDATE without having to first SELECT the record (you already know the primary key value)
In the case you have the dbContext available and ready, just add InsertOnSubmit:
Entity myEntity = new Entity();
myEntity.UserId = "123";
myEntity.Name = bob:
Dbcontext.InsertOnSubmit(myEntity);
Dbcontext.SubmitChanges();
As the name of the method implies, this will insert your new entity into the database on calling SubmitChanges.
Marc
If you want to do this for performance reasons then you shouldn't worry about it. Linq to Sql will cache objects locally so that just grabbing an entity by ID to modify some fields is very cheap.
It's possible to attach and persist it to the database, however you may want to set a field to check for concurrency (ie LastModified).
If you are going to use the Attach method on the data context, you need to set the primary/composite keys before you attach the entity (so you don't trigger INotifyPropertyChanging, INotifyPropertyChanged events).